logger = $logger; $this->quiet = $quiet; $this->verbose = $verbose; $this->lastOutput = []; $this->rootDir = $rootDir; } /** * Executes shell commands. * * @param array $args * * @return bool Indicates success */ public function executeCommand($args = []) { $this->lastOutput = []; $command = call_user_func_array('sprintf', $args); $this->logger->logDebug($command); if ($this->quiet) { $this->logger->log('Executing: ' . $command); } $status = 0; $descriptorSpec = [ 0 => ["pipe", "r"], // stdin 1 => ["pipe", "w"], // stdout 2 => ["pipe", "w"], // stderr ]; $pipes = []; $process = proc_open($command, $descriptorSpec, $pipes, $this->buildPath, null); if (is_resource($process)) { fclose($pipes[0]); $this->lastOutput = stream_get_contents($pipes[1]); $this->lastError = stream_get_contents($pipes[2]); fclose($pipes[1]); fclose($pipes[2]); $status = proc_close($process); } $this->lastOutput = array_filter(explode(PHP_EOL, $this->lastOutput)); $shouldOutput = ($this->logExecOutput && ($this->verbose || $status != 0)); if ($shouldOutput && !empty($this->lastOutput)) { $this->logger->log($this->lastOutput); } if (!empty($this->lastError)) { $this->logger->log("\033[0;31m" . $this->lastError . "\033[0m", LogLevel::ERROR); } $rtn = false; if ($status == 0) { $rtn = true; } return $rtn; } /** * Returns the output from the last command run. */ public function getLastOutput() { return implode(PHP_EOL, $this->lastOutput); } /** * Returns the stderr output from the last command run. */ public function getLastError() { return $this->lastError; } /** * Find a binary required by a plugin. * @param string $binary * @param bool $quiet * @return null|string */ public function findBinary($binary, $quiet = false) { $composerBin = $this->getComposerBinDir(realpath($this->buildPath)); if (is_string($binary)) { $binary = [$binary]; } foreach ($binary as $bin) { $this->logger->log(Lang::get('looking_for_binary', $bin), LogLevel::DEBUG); if (is_dir($composerBin) && is_file($composerBin . DIRECTORY_SEPARATOR . $bin)) { $this->logger->log(Lang::get('found_in_path', $composerBin, $bin), LogLevel::DEBUG); return $composerBin . DIRECTORY_SEPARATOR . $bin; } if (is_file($this->rootDir . $bin)) { $this->logger->log(Lang::get('found_in_path', 'root', $bin), LogLevel::DEBUG); return $this->rootDir . $bin; } if (is_file($this->rootDir . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . $bin)) { $this->logger->log(Lang::get('found_in_path', 'vendor/bin', $bin), LogLevel::DEBUG); return $this->rootDir . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . $bin; } $findCmdResult = $this->findGlobalBinary($bin); if (is_file($findCmdResult)) { $this->logger->log(Lang::get('found_in_path', '', $bin), LogLevel::DEBUG); return $findCmdResult; } } if ($quiet) { return null; } throw new Exception(Lang::get('could_not_find', implode('/', $binary))); } /** * Find a binary which is installed globally on the system * @param string $binary * @return null|string */ abstract protected function findGlobalBinary($binary); /** * Try to load the composer.json file in the building project * If the bin-dir is configured, return the full path to it * @param string $path Current build path * @return string|null */ public function getComposerBinDir($path) { if (is_dir($path)) { $composer = $path . DIRECTORY_SEPARATOR . 'composer.json'; if (is_file($composer)) { $json = json_decode(file_get_contents($composer)); if (isset($json->config->{"bin-dir"})) { return $path . DIRECTORY_SEPARATOR . $json->config->{"bin-dir"}; } elseif (is_dir($path . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'bin')) { return $path . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'bin'; } } } return null; } /** * Set the buildPath property. * @param string $path */ public function setBuildPath($path) { $this->buildPath = $path; } }