Skip to content

Commit

Permalink
Xdebug3 support in built in webserver (#25)
Browse files Browse the repository at this point in the history
* support xdebug3

* fix xdebug version detection and test

* use php_binary constant

* simplify code comparison

* TestableBuiltInWebServer always returns code 0, so only test that xdebug3 is enabled

* fix quoting issue with _runCommand through bash
  • Loading branch information
TomK authored Dec 14, 2020
1 parent f73e951 commit 58c10a2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"symfony/console": "~4.2",
"psr/log": "~1.1"
},
"suggest": {
"ext-xdebug": "*"
},
"require-dev": {
"phpunit/phpunit": "7.5.*"
},
Expand Down
44 changes: 37 additions & 7 deletions src/Console/Commands/BuiltInWebServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ protected function _runCommand($command)
{
// Use bash to execute if available,
// enables CTRL+C to also kill spawned process (cygwin issue)
$command = "bash -c '$command'";
$command = addcslashes($command, "'");
$command = "bash -c $'$command'";
}
$method($command, $exitCode);
return $exitCode;
Expand All @@ -143,14 +144,43 @@ protected function _buildCommand(OutputInterface $output)
$output->write($this->host === '0.0.0.0' ? '127.0.0.1' : $this->host);
$output->writeln(':' . $this->port);

$phpCommand = 'php';
$phpCommand = PHP_BINARY;
if($this->debug)
{
$phpCommand .= ' -d zend_extension=xdebug.so';
$phpCommand .= ' -d xdebug.remote_enable=1';
$phpCommand .= ' -d xdebug.remote_autostart=1';
$phpCommand .= ' -d xdebug.remote_connect_back=1';
$phpCommand .= ' -d xdebug.idekey=' . $this->debugIdeKey;
// check for xdebug, this must be checked in a new process in case this was launched with different options
$xdebugLoaded = $this->_runCommand($phpCommand . ' -r "exit(extension_loaded(\'xdebug\')?0:1);"');
if($xdebugLoaded !== 0)
{
$ext = ' -d zend_extension=xdebug';
$xdebugLoaded = $this->_runCommand($phpCommand . $ext . ' -r "exit(extension_loaded(\'xdebug\')?0:1);"');
if($xdebugLoaded === 0)
{
$phpCommand .= $ext;
}
}
if($xdebugLoaded === 0)
{
$v3 = $this->_runCommand(
$phpCommand . ' -r "exit(version_compare(phpversion(\'xdebug\'), \'3.0.0\', \'>=\')?0:1);"'
);
if($v3 === 0)
{
$phpCommand .= ' -d xdebug.mode=debug';
$phpCommand .= ' -d xdebug.start_with_request=1';
$phpCommand .= ' -d xdebug.discover_client_host=1';
}
else
{
$phpCommand .= ' -d xdebug.remote_enable=1';
$phpCommand .= ' -d xdebug.remote_autostart=1';
$phpCommand .= ' -d xdebug.remote_connect_back=1';
}
$phpCommand .= ' -d xdebug.idekey=' . $this->debugIdeKey;
}
else
{
$output->writeln(['', "\tXDebug extension not installed", ""]);
}
}

$projectRoot = trim($this->getContext()->getProjectRoot());
Expand Down
9 changes: 5 additions & 4 deletions tests/Console/Commands/BuiltInWebServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public function testCommand(array $options, $passthru, $negate = false)

public function optionsProvider()
{
$pre = 'Raw Command: php ';
$debugCommand = '-d zend_extension=xdebug.so -d xdebug.remote_enable=1 -d xdebug.remote_autostart=1 -d xdebug.remote_connect_back=1 -d xdebug.idekey=';
$debugCommand = '-d xdebug.mode=debug -d xdebug.start_with_request=1 -d xdebug.discover_client_host=1 -d xdebug.idekey=';
$pre = 'Raw Command: ' . PHP_BINARY . ' ';

return [
[[], $pre . '-S 127.0.0.1:8888 -t public/index.php'],
[[], '|__'],
Expand All @@ -46,8 +47,8 @@ public function optionsProvider()
[['--host' => '0.0.0.0'], $pre . '-S 0.0.0.0:8888 -t public/index.php'],
[['-c' => 'framework'], $pre . '-S framework.cubex-local.com:8888 -t public/index.php'],
[['--router' => 'index.exec'], $pre . '-S 127.0.0.1:8888 -t index.exec'],
[['-d' => true], $pre . $debugCommand . 'PHPSTORM -S 0.0.0.0:8888 -t public/index.php'],
[['-d' => true, '-idekey' => 'TEST'], $pre . $debugCommand . 'TEST -S 0.0.0.0:8888 -t public/index.php'],
[['-d' => true], $debugCommand . 'PHPSTORM -S 0.0.0.0:8888 -t public/index.php'],
[['-d' => true, '-idekey' => 'TEST'], $debugCommand . 'TEST -S 0.0.0.0:8888 -t public/index.php'],
];
}

Expand Down

0 comments on commit 58c10a2

Please sign in to comment.