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@0
|
22 private $suffixes = array('.exe', '.bat', '.cmd', '.com');
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Replaces default suffixes of executable.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @param array $suffixes
|
Chris@0
|
28 */
|
Chris@0
|
29 public function setSuffixes(array $suffixes)
|
Chris@0
|
30 {
|
Chris@0
|
31 $this->suffixes = $suffixes;
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Adds new possible suffix to check for executable.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param string $suffix
|
Chris@0
|
38 */
|
Chris@0
|
39 public function addSuffix($suffix)
|
Chris@0
|
40 {
|
Chris@0
|
41 $this->suffixes[] = $suffix;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Finds an executable by name.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @param string $name The executable name (without the extension)
|
Chris@0
|
48 * @param string $default The default to return if no executable is found
|
Chris@0
|
49 * @param array $extraDirs Additional dirs to check into
|
Chris@0
|
50 *
|
Chris@0
|
51 * @return string The executable path or default value
|
Chris@0
|
52 */
|
Chris@0
|
53 public function find($name, $default = null, array $extraDirs = array())
|
Chris@0
|
54 {
|
Chris@0
|
55 if (ini_get('open_basedir')) {
|
Chris@0
|
56 $searchPath = explode(PATH_SEPARATOR, ini_get('open_basedir'));
|
Chris@0
|
57 $dirs = array();
|
Chris@0
|
58 foreach ($searchPath as $path) {
|
Chris@0
|
59 // Silencing against https://bugs.php.net/69240
|
Chris@0
|
60 if (@is_dir($path)) {
|
Chris@0
|
61 $dirs[] = $path;
|
Chris@0
|
62 } else {
|
Chris@0
|
63 if (basename($path) == $name && @is_executable($path)) {
|
Chris@0
|
64 return $path;
|
Chris@0
|
65 }
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68 } else {
|
Chris@0
|
69 $dirs = array_merge(
|
Chris@0
|
70 explode(PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
|
Chris@0
|
71 $extraDirs
|
Chris@0
|
72 );
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 $suffixes = array('');
|
Chris@0
|
76 if ('\\' === DIRECTORY_SEPARATOR) {
|
Chris@0
|
77 $pathExt = getenv('PATHEXT');
|
Chris@0
|
78 $suffixes = array_merge($suffixes, $pathExt ? explode(PATH_SEPARATOR, $pathExt) : $this->suffixes);
|
Chris@0
|
79 }
|
Chris@0
|
80 foreach ($suffixes as $suffix) {
|
Chris@0
|
81 foreach ($dirs as $dir) {
|
Chris@0
|
82 if (@is_file($file = $dir.DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === DIRECTORY_SEPARATOR || is_executable($file))) {
|
Chris@0
|
83 return $file;
|
Chris@0
|
84 }
|
Chris@0
|
85 }
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 return $default;
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|