1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tpaksu\XdebugToggle\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
class XdebugToggle extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The complete line containing "*_extension=*xdebug*" |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $extensionLine; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Extension active status |
22
|
|
|
* |
23
|
|
|
* @var boolean |
24
|
|
|
*/ |
25
|
|
|
protected $extensionStatus; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The configuration written in php.ini for XDebug |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $extensionSettings; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Path of the Loaded INI file |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $iniPath; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Debug mode active flag |
43
|
|
|
* |
44
|
|
|
* @var boolean |
45
|
|
|
*/ |
46
|
|
|
protected $debug; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The command signature |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $signature = 'xdebug {status : "on" or "off" to enable/disable XDebug}'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The command description |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $description = 'Enables or disables XDebug extension'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Class constructor |
64
|
|
|
*/ |
65
|
|
|
public function __construct() |
66
|
|
|
{ |
67
|
|
|
parent::__construct(); |
68
|
|
|
$this->debug = false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The method that handles the command |
73
|
|
|
*/ |
74
|
|
|
public function handle() |
75
|
|
|
{ |
76
|
|
|
// Define custom format for bold text |
77
|
|
|
$style = new OutputFormatterStyle('default', 'default', array('bold')); |
78
|
|
|
$this->output->getFormatter()->setStyle('bold', $style); |
79
|
|
|
|
80
|
|
|
// Get the verbosity level to set debug mode flag |
81
|
|
|
$verbosityLevel = $this->getOutput()->getVerbosity(); |
82
|
|
|
if ($verbosityLevel > OutputInterface::VERBOSITY_DEBUG) { |
83
|
|
|
$this->debug = true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Get XDebug desired status from the command line arguments |
87
|
|
|
$desiredStatus = strval($this->argument("status")); |
88
|
|
|
|
89
|
|
|
if ($this->debug) { |
90
|
|
|
echo "Desired Status: " . ($desiredStatus) . "\n"; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// validate desired XDebug status |
94
|
|
|
if (!in_array($desiredStatus, ["on", "off"])) { |
95
|
|
|
$this->line("Status should be \"on\" or \"off\". Other values are not accepted.", "fg=red;bold"); |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Retrieve the INI path to the global variable |
100
|
|
|
$this->getIniPath(); |
101
|
|
|
|
102
|
|
|
// If we can't retrieve the loaded INI path, bail out |
103
|
|
|
if ($this->iniPath === "") { |
104
|
|
|
$this->line("Can't get php.ini file path from phpinfo() output. |
105
|
|
|
Make sure that the function is allowed inside your php.ini configuration.", "bold"); |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Get the XDebug extension information from the INI file |
110
|
|
|
$this->getXDebugStatus(); |
111
|
|
|
|
112
|
|
|
// prepare variables for comparison and output |
113
|
|
|
$currentStatus = $this->extensionStatus ? "on" : "off"; |
114
|
|
|
$styledStatus = $this->extensionStatus ? "<fg=green;bold>on" : "<fg=red;bold>off"; |
115
|
|
|
|
116
|
|
|
// print current status to the user |
117
|
|
|
$this->line("<fg=yellow>Current XDebug Status: $styledStatus</>"); |
118
|
|
|
|
119
|
|
|
// if the desired status and current status are the same, we don't need to alter anything |
120
|
|
|
// inform the user and exit |
121
|
|
|
if ($currentStatus === $desiredStatus) { |
122
|
|
|
$this->line("<fg=green>Already at the desired state. No action has been taken.</>"); |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// we need to alter the status to the new one. Do it! |
127
|
|
|
$this->setXDebugStatus($desiredStatus); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Sets the new XDebug extension status |
132
|
|
|
* |
133
|
|
|
* @param string $status Whether the extension should be active or not |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
private function setXDebugStatus($status) |
138
|
|
|
{ |
139
|
|
|
// inform the user about the current operation |
140
|
|
|
$this->line("<bold>Setting status to \"$status\"</bold>"); |
141
|
|
|
|
142
|
|
|
// read the ini file |
143
|
|
|
$contents = file_get_contents($this->iniPath); |
144
|
|
|
|
145
|
|
|
if ($this->debug) { |
146
|
|
|
echo "status: $status\n"; |
147
|
|
|
echo "line: " . $this->extensionLine . "\n"; |
148
|
|
|
echo "new: " . trim($this->extensionLine, ";") . "\n"; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// replace the "zend_extension=*xdebug.*" line with the active/passive equivalent |
152
|
|
|
switch ($status) { |
153
|
|
|
case 'on': |
154
|
|
|
$contents = str_replace($this->extensionLine, trim($this->extensionLine, ";"), $contents); |
155
|
|
|
break; |
156
|
|
|
|
157
|
|
|
case 'off': |
158
|
|
|
$contents = str_replace($this->extensionLine, ";" . $this->extensionLine, $contents); |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// rewrite the php.ini file |
163
|
|
|
file_put_contents($this->iniPath, $contents); |
164
|
|
|
|
165
|
|
|
// restart the service to put the changes in effect |
166
|
|
|
$this->restartServices(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Retrieves the INI path from php.ini file |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
private function getIniPath() |
175
|
|
|
{ |
176
|
|
|
$this->iniPath = php_ini_loaded_file() ?? ""; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Gets the XDebug status and related configuration from the loaded php.ini file |
181
|
|
|
*/ |
182
|
|
|
private function getXDebugStatus() |
183
|
|
|
{ |
184
|
|
|
// get the extension status |
185
|
|
|
$this->getExtensionStatus(); |
186
|
|
|
|
187
|
|
|
// get extemsion settings |
188
|
|
|
$this->getExtensionSettings(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Reads the extension status from PHP ini file |
193
|
|
|
* |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
|
private function getExtensionStatus() |
197
|
|
|
{ |
198
|
|
|
// read the extension line from file, |
199
|
|
|
// can't use parse_ini_file here because the keyed array overwrites "extension" lines and keeps the last one |
200
|
|
|
$this->extensionLine = collect(file_get_contents($this->iniPath)) |
201
|
|
|
->explode("\n") |
202
|
|
|
->filter(function ($line) { |
203
|
|
|
return Str::contains($line, "extension=") && Str::contains($line, "xdebug"); |
204
|
|
|
}) |
205
|
|
|
->first(); |
206
|
|
|
|
207
|
|
|
$this->extensionLine = trim($this->extensionLine ?? ""); |
208
|
|
|
|
209
|
|
|
if ($this->debug) { |
210
|
|
|
echo "line: " . $this->extensionLine . "\n"; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (strlen($this->extensionLine) > 0) { |
214
|
|
|
$this->extensionStatus = $this->extensionLine[0] == ";" ? false : true; |
215
|
|
|
} else { |
216
|
|
|
$this->extensionStatus = false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if ($this->debug) { |
220
|
|
|
echo "ext.status: " . $this->extensionStatus . "\n"; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Reads the extension settings from PHP ini file |
226
|
|
|
* |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
|
|
private function getExtensionSettings() |
230
|
|
|
{ |
231
|
|
|
$settings = collect(parse_ini_file($this->iniPath))->filter(function ($setting, $key) { |
232
|
|
|
return Str::startsWith($key, "xdebug."); |
233
|
|
|
}); |
234
|
|
|
$this->extensionSettings = $settings->toArray(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Restarts the services that takes the modification into effect |
239
|
|
|
* |
240
|
|
|
* @return void |
241
|
|
|
*/ |
242
|
|
|
private function restartServices() |
243
|
|
|
{ |
244
|
|
|
/** |
245
|
|
|
* Define a global outputter to display command output to user |
246
|
|
|
* |
247
|
|
|
* @param string $type The type of the output |
248
|
|
|
* @param string $data The output |
249
|
|
|
* |
250
|
|
|
* @return void |
251
|
|
|
*/ |
252
|
|
|
$output = function ($type, $data) { |
253
|
|
|
$this->info($data); |
254
|
|
|
}; |
255
|
|
|
// run the command(s) needed to restart the service |
256
|
|
|
(new Process([env("XDEBUG_SERVICE_RESTART_COMMAND", "valet restart nginx")]))->run($output); |
257
|
|
|
// display the new extension status |
258
|
|
|
(new Process(["php --ri xdebug"]))->run($output); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|