1 | <?php |
||
27 | class PuliRunner |
||
28 | { |
||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $puli; |
||
33 | |||
34 | /** |
||
35 | * Creates a new runner. |
||
36 | * |
||
37 | * @param string|null $binDir The path to Composer's "bin-dir". |
||
38 | */ |
||
39 | 3 | public function __construct($binDir = null) |
|
40 | { |
||
41 | 3 | $phpFinder = new PhpExecutableFinder(); |
|
42 | |||
43 | 3 | if (!($php = $phpFinder->find())) { |
|
44 | throw new RuntimeException('The "php" command could not be found.'); |
||
45 | } |
||
46 | |||
47 | 3 | $finder = new ExecutableFinder(); |
|
48 | |||
49 | // Search: |
||
50 | // 1. in the current working directory |
||
51 | // 2. in Composer's "bin-dir" |
||
52 | // 3. in the system path |
||
53 | 3 | $searchPath = array_merge(array(getcwd()), (array) $binDir); |
|
54 | |||
55 | // Search "puli.phar" in the PATH and the current directory |
||
56 | 3 | if (!($puli = $this->find('puli.phar', $searchPath, $finder))) { |
|
57 | // Search "puli" in the PATH and Composer's "bin-dir" |
||
58 | 3 | if (!($puli = $this->find('puli', $searchPath, $finder))) { |
|
59 | throw new RuntimeException('The "puli"/"puli.phar" command could not be found.'); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | // Fix slashes |
||
64 | 3 | $php = strtr($php, '\\', '/'); |
|
65 | 3 | $puli = strtr($puli, '\\', '/'); |
|
66 | |||
67 | 3 | $content = file_get_contents($puli, null, null, -1, 18); |
|
68 | |||
69 | 3 | if ($content === '#!/usr/bin/env php' || 0 === strpos($content, '<?php')) { |
|
70 | 2 | $this->puli = ProcessUtils::escapeArgument($php).' '.ProcessUtils::escapeArgument($puli); |
|
71 | } else { |
||
72 | 1 | $this->puli = ProcessUtils::escapeArgument($puli); |
|
73 | } |
||
74 | 3 | } |
|
75 | |||
76 | /** |
||
77 | * Returns the command used to execute Puli. |
||
78 | * |
||
79 | * @return string The "puli" command. |
||
80 | */ |
||
81 | 3 | public function getPuliCommand() |
|
85 | |||
86 | /** |
||
87 | * Runs a Puli command. |
||
88 | * |
||
89 | * @param string $command The Puli command to run. |
||
90 | * @param string[] $args Arguments to quote and insert into the command. |
||
91 | * For each key "key" in this array, the placeholder |
||
92 | * "%key%" should be present in the command string. |
||
93 | * |
||
94 | * @return string The lines of the output. |
||
95 | */ |
||
96 | public function run($command, array $args = array()) |
||
118 | |||
119 | 3 | private function find($name, array $dirs, ExecutableFinder $finder) |
|
141 | } |
||
142 |