From b0000617cc7bc481d839eb3c59d81bbcbaef34a4 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 11 Dec 2016 16:14:36 +0100 Subject: [PATCH] Support @php prefix for scripts to reuse the current PHP interpreter, fixes #5957 --- doc/articles/scripts.md | 20 +++++++++++++++++++ .../EventDispatcher/EventDispatcher.php | 11 +++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/doc/articles/scripts.md b/doc/articles/scripts.md index 66d84b8c2..c916ed017 100644 --- a/doc/articles/scripts.md +++ b/doc/articles/scripts.md @@ -249,3 +249,23 @@ resolve to whatever composer.phar is currently being used: One limitation of this is that you can not call multiple composer commands in a row like `@composer install && @composer foo`. You must split them up in a JSON array of commands. + +## Executing PHP scripts + +To execute PHP scripts, you can use `@php` which will automatically +resolve to whatever php process is currently being used: + +```json +{ + "scripts": { + "test": [ + "@php script.php", + "phpunit" + ] + } +} +``` + +One limitation of this is that you can not call multiple commands in +a row like `@php install && @php foo`. You must split them up in a +JSON array of commands. diff --git a/src/Composer/EventDispatcher/EventDispatcher.php b/src/Composer/EventDispatcher/EventDispatcher.php index 4d773e46a..396f21731 100644 --- a/src/Composer/EventDispatcher/EventDispatcher.php +++ b/src/Composer/EventDispatcher/EventDispatcher.php @@ -233,6 +233,15 @@ class EventDispatcher } } + if (substr($exec, 0, 5) === '@php ') { + $finder = new PhpExecutableFinder(); + $phpPath = $finder->find(); + if (!$phpPath) { + throw new \RuntimeException('Failed to locate PHP binary to execute "'.$exec.'"'); + } + $exec = $phpPath . ' ' . substr($exec, 5); + } + if (0 !== ($exitCode = $this->process->execute($exec))) { $this->io->writeError(sprintf('Script %s handling the %s event returned with error code '.$exitCode.'', $callable, $event->getName())); @@ -460,7 +469,7 @@ class EventDispatcher */ protected function isComposerScript($callable) { - return '@' === substr($callable, 0, 1); + return '@composer ' === substr($callable, 0, 10); } /**