diff --git a/composer.json b/composer.json index 0202ccb..663a2fd 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,9 @@ "symfony/console": "~4.2", "psr/log": "~1.1" }, + "suggest": { + "ext-xdebug": "*" + }, "require-dev": { "phpunit/phpunit": "7.5.*" }, diff --git a/src/Console/Commands/BuiltInWebServer.php b/src/Console/Commands/BuiltInWebServer.php index b9d68df..442ea39 100644 --- a/src/Console/Commands/BuiltInWebServer.php +++ b/src/Console/Commands/BuiltInWebServer.php @@ -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; @@ -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()); diff --git a/tests/Console/Commands/BuiltInWebServerTest.php b/tests/Console/Commands/BuiltInWebServerTest.php index 999c2b4..dc42e71 100644 --- a/tests/Console/Commands/BuiltInWebServerTest.php +++ b/tests/Console/Commands/BuiltInWebServerTest.php @@ -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'], [[], '|__'], @@ -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'], ]; }