Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Process;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Generic executable finder.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
18 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
Chris@0
|
19 */
|
Chris@0
|
20 class ExecutableFinder
|
Chris@0
|
21 {
|
Chris@17
|
22 private $suffixes = ['.exe', '.bat', '.cmd', '.com'];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Replaces default suffixes of executable.
|
Chris@0
|
26 */
|
Chris@0
|
27 public function setSuffixes(array $suffixes)
|
Chris@0
|
28 {
|
Chris@0
|
29 $this->suffixes = $suffixes;
|
Chris@0
|
30 }
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Adds new possible suffix to check for executable.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @param string $suffix
|
Chris@0
|
36 */
|
Chris@0
|
37 public function addSuffix($suffix)
|
Chris@0
|
38 {
|
Chris@0
|
39 $this->suffixes[] = $suffix;
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Finds an executable by name.
|
Chris@0
|
44 *
|
Chris@17
|
45 * @param string $name The executable name (without the extension)
|
Chris@17
|
46 * @param string|null $default The default to return if no executable is found
|
Chris@17
|
47 * @param array $extraDirs Additional dirs to check into
|
Chris@0
|
48 *
|
Chris@17
|
49 * @return string|null The executable path or default value
|
Chris@0
|
50 */
|
Chris@17
|
51 public function find($name, $default = null, array $extraDirs = [])
|
Chris@0
|
52 {
|
Chris@0
|
53 if (ini_get('open_basedir')) {
|
Chris@18
|
54 $searchPath = array_merge(explode(PATH_SEPARATOR, ini_get('open_basedir')), $extraDirs);
|
Chris@17
|
55 $dirs = [];
|
Chris@0
|
56 foreach ($searchPath as $path) {
|
Chris@0
|
57 // Silencing against https://bugs.php.net/69240
|
Chris@0
|
58 if (@is_dir($path)) {
|
Chris@0
|
59 $dirs[] = $path;
|
Chris@0
|
60 } else {
|
Chris@0
|
61 if (basename($path) == $name && @is_executable($path)) {
|
Chris@0
|
62 return $path;
|
Chris@0
|
63 }
|
Chris@0
|
64 }
|
Chris@0
|
65 }
|
Chris@0
|
66 } else {
|
Chris@0
|
67 $dirs = array_merge(
|
Chris@0
|
68 explode(PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
|
Chris@0
|
69 $extraDirs
|
Chris@0
|
70 );
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@17
|
73 $suffixes = [''];
|
Chris@17
|
74 if ('\\' === \DIRECTORY_SEPARATOR) {
|
Chris@0
|
75 $pathExt = getenv('PATHEXT');
|
Chris@16
|
76 $suffixes = array_merge($pathExt ? explode(PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes);
|
Chris@0
|
77 }
|
Chris@0
|
78 foreach ($suffixes as $suffix) {
|
Chris@0
|
79 foreach ($dirs as $dir) {
|
Chris@17
|
80 if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
|
Chris@0
|
81 return $file;
|
Chris@0
|
82 }
|
Chris@0
|
83 }
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 return $default;
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|