-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_interactive.php
More file actions
193 lines (164 loc) Β· 5.53 KB
/
debug_interactive.php
File metadata and controls
193 lines (164 loc) Β· 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env php
<?php
/**
* Interactive Debug Wrapper for MODX CLI
*
* This wrapper allows dynamic debugging of any MODX CLI command without
* hardcoding arguments in VS Code launch.json. It provides an interactive
* prompt for command input when debugging.
*/
if (PHP_SAPI !== 'cli') {
echo 'Warning: MODX CLI should be invoked via the CLI version of PHP, not the '.PHP_SAPI.' SAPI'.PHP_EOL;
exit(1);
}
require __DIR__.'/src/bootstrap.php';
use MODX\CLI\Application;
use Symfony\Component\Console\Input\StringInput;
error_reporting(-1);
if (function_exists('ini_set')) {
@ini_set('display_errors', 1);
$memoryInBytes = function ($value) {
$unit = strtolower(substr($value, -1, 1));
$value = (int) $value;
switch($unit) {
case 'g':
$value *= 1024;
// no break (cumulative multiplier)
case 'm':
$value *= 1024;
// no break (cumulative multiplier)
case 'k':
$value *= 1024;
}
return $value;
};
$memoryLimit = trim(ini_get('memory_limit'));
// Increase memory_limit if it is lower than 512M
if ($memoryLimit != -1 && $memoryInBytes($memoryLimit) < 512 * 1024 * 1024) {
@ini_set('memory_limit', '512M');
}
unset($memoryInBytes, $memoryLimit);
}
/**
* Get command input dynamically
*/
function getCommandInput() {
// Check if arguments were passed directly (for non-interactive use)
global $argv;
if (count($argv) > 1) {
return array_slice($argv, 1);
}
// Check for environment variable (allows setting command in VS Code terminal)
$envCommand = getenv('MODX_DEBUG_COMMAND');
if ($envCommand) {
echo "π Debug Mode: Running command from environment: $envCommand\n";
return explode(' ', $envCommand);
}
// Interactive mode - prompt for command
echo "\n";
echo "π MODX CLI Interactive Debugger\n";
echo "================================\n";
echo "Set breakpoints in VS Code, then enter a command to debug.\n";
echo "Examples:\n";
echo " package:list\n";
echo " package:upgradeable\n";
echo " package:list-remote\n";
echo " tv:update --name=test --description='Test TV' 1\n";
echo " resource:create --pagetitle='Test Page' --content='Hello World'\n";
echo "\n";
// Read command from stdin
echo "Enter MODX CLI command: ";
$handle = fopen("php://stdin", "r");
$command = trim(fgets($handle));
fclose($handle);
if (empty($command)) {
echo "No command entered. Exiting.\n";
exit(0);
}
echo "π Debug Mode: Running command: $command\n";
echo "Set your breakpoints and step through the code!\n";
echo str_repeat("-", 50) . "\n";
// Parse command string into arguments
return parseCommandString($command);
}
/**
* Parse command string into arguments array
* Handles quoted arguments properly
*/
function parseCommandString($command) {
$args = [];
$current = '';
$inQuotes = false;
$quoteChar = '';
for ($i = 0; $i < strlen($command); $i++) {
$char = $command[$i];
if (($char === '"' || $char === "'") && !$inQuotes) {
$inQuotes = true;
$quoteChar = $char;
} elseif ($char === $quoteChar && $inQuotes) {
$inQuotes = false;
$quoteChar = '';
} elseif ($char === ' ' && !$inQuotes) {
if ($current !== '') {
$args[] = $current;
$current = '';
}
} else {
$current .= $char;
}
}
if ($current !== '') {
$args[] = $current;
}
return $args;
}
/**
* Display helpful debugging information
*/
function displayDebugInfo($args) {
echo "\nπ Debug Information:\n";
echo "Command: " . implode(' ', $args) . "\n";
echo "Arguments count: " . count($args) . "\n";
echo "Working directory: " . getcwd() . "\n";
echo "PHP version: " . PHP_VERSION . "\n";
if (extension_loaded('xdebug')) {
echo "Xdebug: β
Loaded (version " . phpversion('xdebug') . ")\n";
if (function_exists('xdebug_is_debugger_active')) {
echo "Debugger active: " . (xdebug_is_debugger_active() ? "β
Yes" : "β No") . "\n";
}
} else {
echo "Xdebug: β Not loaded\n";
}
echo str_repeat("-", 50) . "\n\n";
}
// Main execution
try {
// Get command arguments dynamically
$args = getCommandInput();
if (empty($args)) {
echo "No command specified. Exiting.\n";
exit(0);
}
// Display debug information
displayDebugInfo($args);
// Create StringInput from arguments
$inputString = implode(' ', $args);
$input = new StringInput($inputString);
// Run the command application with StringInput
$application = new Application();
$exitCode = $application->run($input);
echo "\nπ Debug session completed with exit code: $exitCode\n";
exit($exitCode);
} catch (Exception $e) {
echo "\nβ Error during debug session:\n";
echo "Message: " . $e->getMessage() . "\n";
echo "File: " . $e->getFile() . ":" . $e->getLine() . "\n";
echo "\nStack trace:\n" . $e->getTraceAsString() . "\n";
exit(1);
} catch (Throwable $e) {
echo "\nβ Fatal error during debug session:\n";
echo "Message: " . $e->getMessage() . "\n";
echo "File: " . $e->getFile() . ":" . $e->getLine() . "\n";
echo "\nStack trace:\n" . $e->getTraceAsString() . "\n";
exit(1);
}