public static function shell($commands, array $opts = []) { //$cwd = null, array $env = null, $input = null, $timeout = 60, array $options = array() if (is_array($commands)) { $procs = []; foreach ($commands as $command) { $procs[] = static::shell($command, $opts); } return $procs; } $process = new Process($commands); $options = array_replace(['type' => 'sync', 'cwd' => null, 'env' => null, 'timeout' => 60, 'callback' => null, 'output' => true], $opts); $options['cwd'] !== null && $process->setWorkingDirectory($options['cwd']); $options['env'] !== null && $process->setEnv($options['env']); is_int($options['timeout']) && $process->setTimeout($options['timeout']); if ($options['output'] === true) { $process->enableOutput(); } else { $process->disableOutput(); } $type = $options['type']; if ($type === 'sync') { $process->run($options['callback']); } elseif ($type === 'async') { $process->start(); } return $process; }
/** * @param $command */ public function execute($command) { $cwd = getcwd(); chdir($this->currentWorkingDirectory); $process = new Process($command); $process->enableOutput(); $process->run(); if (!$process->isSuccessful()) { chdir($cwd); throw new ProcessException($process->getErrorOutput()); } chdir($cwd); }
/** * Get the current git user information. We need this to extract * the user name and email to put into the generated files * @return mixed */ private function getCurrentGitUser() { $p = new Process('git config --list'); $p->enableOutput(); $result = []; try { $p->mustRun(); $lines = explode("\n", trim($p->getOutput())); foreach ($lines as $line) { $ar = explode('=', trim($line)); if (count($ar) === 2) { $result[$ar[0]] = $ar[1]; } } return $result; } catch (\Exception $e) { $user = get_current_user(); return array('user.name' => $user, 'user.email' => $user); } }