1 | <?php |
||
44 | abstract class Binary |
||
45 | { |
||
46 | /** |
||
47 | * The file system path to a VCS binary |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $path; |
||
52 | |||
53 | /** |
||
54 | * Ensures that the given arguments is a valid VCS binary |
||
55 | * |
||
56 | * @param Binary|string|null $binary The VCS binary |
||
57 | * @return static |
||
58 | * @throws \InvalidArgumentException If $binary is not a valid VCS binary |
||
59 | */ |
||
60 | 197 | public static function ensure($binary) |
|
76 | |||
77 | |||
78 | /** |
||
79 | * Checks if the current system is Windows |
||
80 | * |
||
81 | * @return boolean True if we're on a Windows machine |
||
82 | */ |
||
83 | 175 | protected static function isWindows() |
|
87 | |||
88 | /** |
||
89 | * Creates a VCS binary interface |
||
90 | * |
||
91 | * @param string $path The path to the VCS binary |
||
92 | * @throws \InvalidArgumentException If no VCS binary is found |
||
93 | */ |
||
94 | 191 | public function __construct($path) |
|
101 | |||
102 | /** |
||
103 | * Create a call to the VCS binary for later execution |
||
104 | * |
||
105 | * @param string $path The full path to the VCS repository |
||
106 | * @param string $command The VCS command, e.g. show, commit or add |
||
107 | * @param array $arguments The command arguments |
||
108 | * @return Call |
||
109 | */ |
||
110 | 175 | public function createCall($path, $command, array $arguments) |
|
126 | |||
127 | /** |
||
128 | * The call factory |
||
129 | * |
||
130 | * @param string $cmd The command string to be executed |
||
131 | * @param string $path The working directory |
||
132 | * @return Call |
||
133 | */ |
||
134 | 175 | protected function doCreateCall($cmd, $path) |
|
138 | |||
139 | /** |
||
140 | * Creates the command string to be executed |
||
141 | * |
||
142 | * @param string $binary The path to the binary |
||
143 | * @param string $command The VCS command |
||
144 | * @param array $args The list of command line arguments (sanitized) |
||
145 | * @param array $files The list of files to be added to the command line call |
||
146 | * @return string The command string to be executed |
||
147 | */ |
||
148 | 175 | protected function createCallCommand($binary, $command, array $args, array $files) |
|
156 | |||
157 | /** |
||
158 | * Sanitizes a command line argument |
||
159 | * |
||
160 | * @param string $key The argument key |
||
161 | * @param string $value The argument value (can be empty) |
||
162 | * @return string |
||
163 | */ |
||
164 | 160 | protected function sanitizeCommandArgument($key, $value) |
|
180 | |||
181 | /** |
||
182 | * Sanitizes a list of command line arguments and splits them into args and files |
||
183 | * |
||
184 | * @param array $arguments The list of arguments |
||
185 | * @return array An array with (args, files) |
||
186 | */ |
||
187 | 175 | protected function sanitizeCommandArguments(array $arguments) |
|
213 | |||
214 | /** |
||
215 | * Extracts the CLI call parameters from the arguments to a magic method call |
||
216 | * |
||
217 | * @param string $method The VCS command, e.g. show, commit or add |
||
218 | * @param array $arguments The command arguments with the path to the VCS |
||
219 | * repository being the first argument |
||
220 | * @return array An array with (path, method, args, stdIn) |
||
221 | * @throws \InvalidArgumentException If the method is called with less than one argument |
||
222 | */ |
||
223 | 153 | protected function extractCallParametersFromMagicCall($method, array $arguments) |
|
224 | { |
||
225 | 153 | if (count($arguments) < 1) { |
|
226 | throw new \InvalidArgumentException(sprintf( |
||
227 | '"%s" must be called with at least one argument denoting the path', $method |
||
228 | )); |
||
229 | } |
||
230 | |||
231 | 153 | $path = array_shift($arguments); |
|
232 | 153 | $args = array(); |
|
233 | 153 | $stdIn = null; |
|
234 | |||
235 | 153 | if (count($arguments) > 0) { |
|
236 | 153 | $args = array_shift($arguments); |
|
237 | 153 | if (!is_array($args)) { |
|
238 | 1 | $args = array($args); |
|
239 | 1 | } |
|
240 | |||
241 | 153 | if (count($arguments) > 0) { |
|
242 | 6 | $stdIn = array_shift($arguments); |
|
243 | 6 | if (!is_string($stdIn)) { |
|
244 | $stdIn = null; |
||
245 | } |
||
246 | 6 | } |
|
247 | 153 | } |
|
248 | 153 | return array($path, $method, $args, $stdIn); |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * Method overloading - allows calling VCS commands directly as class methods |
||
253 | * |
||
254 | * @param string $method The VCS command, e.g. show, commit or add |
||
255 | * @param array $arguments The command arguments with the path to the VCS |
||
256 | * repository being the first argument |
||
257 | * @return CallResult |
||
258 | */ |
||
259 | 153 | public function __call($method, array $arguments) |
|
266 | } |
||
267 | |||
268 |