Pimcore\Tool\Console::getExecutable PHP Метод

getExecutable() публичный статический Метод

public static getExecutable ( $name, boolean $throwException = false ) : boolean | mixed | string
$name
$throwException boolean
Результат boolean | mixed | string
    public static function getExecutable($name, $throwException = false)
    {
        if (isset(self::$executableCache[$name])) {
            return self::$executableCache[$name];
        }
        $pathVariable = Config::getSystemConfig()->general->path_variable;
        $paths = [];
        if ($pathVariable) {
            $paths = explode(PATH_SEPARATOR, $pathVariable);
        }
        array_push($paths, "");
        // allow custom setup routines for certain programs
        $customSetupMethod = "setup" . ucfirst($name);
        if (method_exists(__CLASS__, $customSetupMethod)) {
            self::$customSetupMethod();
        }
        // allow custom check routines for certain programs
        $customCheckMethod = "check" . ucfirst($name);
        if (!method_exists(__CLASS__, $customCheckMethod)) {
            $customCheckMethod = "checkDummy";
        }
        foreach ($paths as $path) {
            foreach (["--help", "-h", "-help"] as $option) {
                try {
                    $path = rtrim($path, "/\\ ");
                    if ($path) {
                        $executablePath = $path . DIRECTORY_SEPARATOR . $name;
                    } else {
                        $executablePath = $name;
                    }
                    $process = new Process($executablePath . " " . $option);
                    $process->run();
                    if ($process->isSuccessful() || self::$customCheckMethod($process)) {
                        if (empty($path) && self::getSystemEnvironment() == "unix") {
                            // get the full qualified path, seems to solve a lot of problems :)
                            // if not using the full path, timeout, nohup and nice will fail
                            $fullQualifiedPath = shell_exec("which " . $executablePath);
                            $fullQualifiedPath = trim($fullQualifiedPath);
                            if ($fullQualifiedPath) {
                                $executablePath = $fullQualifiedPath;
                            }
                        }
                        self::$executableCache[$name] = $executablePath;
                        return $executablePath;
                    }
                } catch (\Exception $e) {
                }
            }
        }
        self::$executableCache[$name] = false;
        if ($throwException) {
            throw new \Exception("No '{$name}' executable found, please install the application or add it to the PATH (in system settings or to your PATH environment variable");
        }
        return false;
    }

Usage Example

Пример #1
0
 /**
  * Install package using 'composer require'.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int
  * @throws \Exception
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $pidFile = $input->getOption('pid');
     $require = $input->getOption('require');
     try {
         /**
          * TODO consider direct/programmatic usage of composer/composer package
          * @see https://github.com/composer/composer/issues/1906#issuecomment-51632453
          */
         $composerPath = Console::getExecutable('composer');
         if (!$composerPath) {
             $composerPath = Console::getExecutable('composer.phar');
         }
         if (!$composerPath) {
             throw new \Exception('composer executable not found');
         }
         $command = implode(' ', [$composerPath, 'require -d', PIMCORE_DOCUMENT_ROOT, $require, '2>&1']);
         $output->writeln(sprintf("Installing package <b>%s</b>\n", $require));
         $process = new Process($command);
         $process->setTimeout(0);
         $return = $process->run(function ($type, $data) use($output) {
             $output->write($data);
         });
         if ($return !== 0) {
             $this->writeError('Process finished with code: ' . $return);
         } else {
             $output->writeln('<br><b style="color: #0a0;">Package installed successfully.</b>');
         }
     } catch (\Exception $e) {
         $this->writeError($e->getMessage());
     }
     @unlink($pidFile);
 }
All Usage Examples Of Pimcore\Tool\Console::getExecutable