浏览代码

Support @php prefix for scripts to reuse the current PHP interpreter, fixes #5957

Jordi Boggiano 8 年之前
父节点
当前提交
b0000617cc
共有 2 个文件被更改,包括 30 次插入1 次删除
  1. 20 0
      doc/articles/scripts.md
  2. 10 1
      src/Composer/EventDispatcher/EventDispatcher.php

+ 20 - 0
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.

+ 10 - 1
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('<error>Script %s handling the %s event returned with error code '.$exitCode.'</error>', $callable, $event->getName()));
 
@@ -460,7 +469,7 @@ class EventDispatcher
      */
     protected function isComposerScript($callable)
     {
-        return '@' === substr($callable, 0, 1);
+        return '@composer ' === substr($callable, 0, 10);
     }
 
     /**