N98\Util\Exec::run PHP Method

run() public static method

public static run ( string $command, string &$output = null, integer &$returnCode = null )
$command string
$output string
$returnCode integer
    public static function run($command, &$output = null, &$returnCode = null)
    {
        if (!self::allowed()) {
            $message = sprintf("No PHP exec(), can not execute command '%s'.", $command);
            throw new RuntimeException($message);
        }
        $command = $command . self::REDIRECT_STDERR_TO_STDOUT;
        exec($command, $outputArray, $returnCode);
        $output = self::parseCommandOutput($outputArray);
        if ($returnCode !== self::CODE_CLEAN_EXIT) {
            throw new RuntimeException(sprintf(sprintf("Exit status %d for command %s. Output was: %s", $returnCode, $command, $output)));
        }
    }

Usage Example

 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @throws RuntimeException
  * @return int|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $opener = '';
     if (OperatingSystem::isMacOs()) {
         $opener = 'open';
     } elseif (OperatingSystem::isWindows()) {
         $opener = 'start';
     } else {
         // Linux
         if (exec('which xde-open')) {
             $opener = 'xdg-open';
         } elseif (exec('which gnome-open')) {
             $opener = 'gnome-open';
         } elseif (exec('which kde-open')) {
             $opener = 'kde-open';
         }
     }
     if (empty($opener)) {
         throw new RuntimeException('No opener command like xde-open, gnome-open, kde-open was found.');
     }
     $this->detectMagento($output);
     if ($this->initMagento($output)) {
         $store = $this->getHelperSet()->get('parameter')->askStore($input, $output, 'store', true);
         if ($store->getId() == Store::DEFAULT_STORE_ID) {
             $url = $this->getBackendStoreUrl($store);
         } else {
             $url = $this->getFrontendStoreUrl($store);
         }
         $output->writeln('Opening URL <comment>' . $url . '</comment> in browser');
         Exec::run(escapeshellcmd($opener . ' ' . $url));
     }
 }
All Usage Examples Of N98\Util\Exec::run