diff --git a/src/Plugin.php b/src/Plugin.php index 3539a2c3..e656acbb 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -25,6 +25,7 @@ use Symfony\Component\Process\Exception\LogicException; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Exception\RuntimeException; +use Symfony\Component\Process\PhpExecutableFinder; /** * PHP_CodeSniffer standard installation manager. @@ -238,22 +239,76 @@ private function saveInstalledPaths() ); } - $this->io->write($configMessage); + // Prepare message in case of failure + $failMessage = sprintf( + 'Failed to set PHP CodeSniffer %s Config', + self::PHPCS_CONFIG_KEY + ); - $this->processExecutor->execute( - sprintf( - 'phpcs %s', - implode(' ', $arguments) - ), - $configResult, - $this->composer->getConfig()->get('bin-dir') + // Determine the path to the main PHPCS file. + $phpcsPath = $this->getPHPCodeSnifferInstallPath(); + if (file_exists($phpcsPath . '/bin/phpcs') === true) { + // PHPCS 3.x. + $phpcsExecutable = './bin/phpcs'; + } else { + // PHPCS 2.x. + $phpcsExecutable = './scripts/phpcs'; + } + + // Okay, lets rock! + $command = vsprintf( + '%s %s %s', + array( + 'php executable' => $this->getPhpExecCommand(), + 'phpcs executable' => $phpcsExecutable, + 'arguments' => implode(' ', $arguments) + ) ); + $exitCode = $this->processExecutor->execute($command, $configResult, $phpcsPath); + + if ($exitCode === 0) { + $this->io->write($configMessage); + } else { + $this->io->write($failMessage); + } + if ($this->io->isVerbose() && !empty($configResult)) { $this->io->write(sprintf('%s', $configResult)); } } + /** + * Get the path to the current PHP version being used. + * + * Duplicate of the same in the EventDispatcher class in Composer itself. + */ + protected function getPhpExecCommand() + { + $finder = new PhpExecutableFinder(); + + $phpPath = $finder->find(false); + + if ($phpPath === false) { + throw new \RuntimeException('Failed to locate PHP binary to execute ' . $phpPath); + } + + $phpArgs = $finder->findArguments(); + $phpArgs = $phpArgs + ? ' ' . implode(' ', $phpArgs) + : '' + ; + + $command = ProcessExecutor::escape($phpPath) . + $phpArgs . + ' -d allow_url_fopen=' . ProcessExecutor::escape(ini_get('allow_url_fopen')) . + ' -d disable_functions=' . ProcessExecutor::escape(ini_get('disable_functions')) . + ' -d memory_limit=' . ProcessExecutor::escape(ini_get('memory_limit')) + ; + + return $command; + } + /** * Iterate trough all known paths and check if they are still valid. *