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