/** * @param BinaryInterface $binary * @param array $options * * @throws ProcessFailedException * * @return BinaryInterface */ public function processWithConfiguration(BinaryInterface $binary, array $options) { $type = strtolower($binary->getMimeType()); if (!in_array($type, array('image/jpeg', 'image/jpg'))) { return $binary; } $pb = new ProcessBuilder(array($this->mozjpegBin)); // Places emphasis on DC $pb->add('-quant-table'); $pb->add(2); $transformQuality = array_key_exists('quality', $options) ? $options['quality'] : $this->quality; if ($transformQuality !== null) { $pb->add('-quality'); $pb->add($transformQuality); } $pb->add('-optimise'); // Favor stdin/stdout so we don't waste time creating a new file. $pb->setInput($binary->getContent()); $proc = $pb->getProcess(); $proc->run(); if (false !== strpos($proc->getOutput(), 'ERROR') || 0 !== $proc->getExitCode()) { throw new ProcessFailedException($proc); } $result = new Binary($proc->getOutput(), $binary->getMimeType(), $binary->getFormat()); return $result; }
function execute($argv = array()) { $builder = new ProcessBuilder(); $builder->add($this->executable); if (@$argv[0] instanceof Response) { $builder->setInput(array_shift($argv)->getOutput()); } else { if (is_array(@$argv[0])) { $flags = array_shift($argv); foreach ($flags as $flag => $value) { $builder->add((strlen($flag) > 1 ? "--" : "-") . $flag); $value === true or $builder->add($value); } } } foreach ($argv as $a) { $builder->add($a); } $process = $builder->getProcess(); $process->run(); if (!$process->isSuccessful()) { $status = $process->getExitCode(); $commandLine = $process->getCommandLine(); throw new ErrorException("Command [{$commandLine}] failed with status [{$status}].", $status, $process->getErrorOutput()); } return new Response($process->getOutput(), $process->getErrorOutput()); }
/** * {@inheritdoc} */ public function generate($uri, $alias = null) { $html = $this->htmlFetcher->fetch($uri); $builder = new ProcessBuilder(); $builder->setPrefix($this->criticalBin); $builder->setArguments(['--base=' . realpath(__DIR__ . '/../.tmp'), '--width=' . $this->width, '--height=' . $this->height, '--minify']); foreach ($this->css as $css) { $builder->add('--css=' . $css); } foreach ($this->ignore as $ignore) { $builder->add('--ignore=' . $ignore); } $builder->setInput($html); $process = $builder->getProcess(); $process->run(); if (!$process->isSuccessful()) { throw new CssGeneratorException(sprintf('Error processing URI [%s]. This is probably caused by ' . 'the Critical npm package. Checklist: 1) `critical_bin`' . ' is correct, 2) `css` paths are correct 3) run `npm ' . 'install` again.', $uri)); } return $this->storage->writeCss(is_null($alias) ? $uri : $alias, $process->getOutput()); }
/** * @param BinaryInterface $binary * @param array $options * * @throws ProcessFailedException * * @return BinaryInterface */ public function processWithConfiguration(BinaryInterface $binary, array $options) { $type = strtolower($binary->getMimeType()); if (!in_array($type, array('image/png'))) { return $binary; } $pb = new ProcessBuilder(array($this->pngquantBin)); // Specify quality. $tranformQuality = array_key_exists('quality', $options) ? $options['quality'] : $this->quality; $pb->add('--quality'); $pb->add($tranformQuality); // Read to/from stdout to save resources. $pb->add('-'); $pb->setInput($binary->getContent()); $proc = $pb->getProcess(); $proc->run(); // 98 and 99 are "quality too low" to compress current current image which, while isn't ideal, is not a failure if (!in_array($proc->getExitCode(), array(0, 98, 99))) { throw new ProcessFailedException($proc); } $result = new Binary($proc->getOutput(), $binary->getMimeType(), $binary->getFormat()); return $result; }
public function highlight($block, $language) { // Build the options $builder = new ProcessBuilder(); // Input the block to the bin $builder->setPrefix("{$this->bin}"); $builder->setInput($block); // Set the language and desired output $builder->add("-l{$language}")->add("-f{$this->output}"); // Set some other options foreach ($this->defaultOpts as $argument => $value) { $builder->add("-P{$argument}={$value}"); } // Run the process $process = $builder->getProcess(); $process->run(); // Check the output if (!$process->isSuccessful()) { throw new \RuntimeException($process->getErrorOutput()); } // Return the result return $process->getOutput(); }
protected function getProcessBuilder($cmd, array $extras = array(), $input = null) { $builder = new ProcessBuilder(); $builder->add($cmd); $builder->setTimeout($this->options['process-timeout']); foreach ($this->arguments as $argument) { try { $builder->add($this->getFormattedParameter($argument, $this->getArgument($argument))); } catch (\InvalidArgumentException $ex) { //nothing here } } foreach ($extras as $key => $value) { try { $builder->add($this->getFormattedParameter($key, $value)); } catch (\InvalidArgumentException $ex) { //nothing here } } if (!is_null($input)) { $builder->setInput($input); } return $builder; }
/** * Sets the input of the process. * * @param string $input The input as a string * * @return ProcessBuilderProxyInterface * * @throws InvalidArgumentException In case the argument is invalid * * Passing an object as an input is deprecated since version 2.5 and will be removed in 3.0. */ public function setInput(string $input) : ProcessBuilderProxyInterface { $this->processBuilder->setInput($input); return $this; }
/** * @return Process */ private function getProcess() { $processBuilder = new ProcessBuilder([$this->executable, 'a', '-si', '-so', '-an', '-txz', '-m0=lzma2', '-mx=9', '-mfb=64', '-md=32m']); $processBuilder->setInput($this->data); return $processBuilder->getProcess(); }
public function compileFile(AssetInfo $assetInfo) { $assetPath = $assetInfo->getPath(); $localPath = $this->getAssetPath($assetPath); if (!file_exists($localPath)) { return null; } $publicPath = $this->getPublicAssetPath($assetPath); $targetPath = 'cache/scss/' . substr($publicPath, 0, strrpos($publicPath, '.')); if ('.css' !== substr($targetPath, -4)) { $targetPath .= '.css'; } $needsCompilation = true; $sourceMTime = filemtime($localPath); $dir = dirname($localPath); if ($this->filesystem->has('web/' . $targetPath)) { $fh = $this->filesystem->handle('web/' . $targetPath); if ($fh) { $firstLine = fgets($fh); $info = substr($firstLine, strlen('/* compiled at '), -3); $spacePosition = strpos($info, ' '); $lastSourceMTime = 0; $dependencies = []; if ($spacePosition > 0) { $lastSourceMTime = (int) substr($info, 0, $spacePosition); $dependencies = trim(substr($info, $spacePosition + 1)); if ($dependencies) { $dependencies = explode(',', trim(substr($info, $spacePosition + 1))); } else { $dependencies = []; } } else { //old format without dependencies $lastSourceMTime = (int) $info; } $needsCompilation = $lastSourceMTime !== $sourceMTime; if (!$needsCompilation) { //check dependencies foreach ($dependencies as $dependency) { list($path, $depLastMTime) = explode(':', $dependency); $depLastMTime = (int) $depLastMTime; if (!file_exists($dir . '/' . $path)) { //depended file does not exist anymore, so we need to recompile $needsCompilation = true; break; } $depSourceMTime = filemtime($dir . '/' . $path); if ($depLastMTime !== $depSourceMTime) { $needsCompilation = true; break; } } } } } if ($needsCompilation) { //resolve all dependencies $dependencies = []; $this->resolveDependencies($localPath, $dependencies); $processBuilder = new ProcessBuilder(); $processBuilder->setInput(file_get_contents($localPath))->add('sass')->add('--scss')->add('--no-cache')->add('--unix-newlines')->add('--load-path')->add(dirname($localPath))->add($localPath)->enableOutput(); $process = $processBuilder->getProcess(); $process->start(); while ($process->isRunning()) { } if (127 === $process->getExitCode()) { throw new \RuntimeException('sass binary not found. Please install sass first and make its in $PATH. ' . $process->getExitCodeText()); } if (0 !== $process->getExitCode()) { throw new \RuntimeException(sprintf("Error during scss compilation of %s:\n%s\n%s\n%s", $assetPath, $process->getExitCodeText(), $process->getErrorOutput(), $process->getOutput())); } $compiled = $process->getOutput(); $compiled = $this->replaceRelativePaths($publicPath, $targetPath, $compiled); $dependencies = implode(',', $dependencies); $info = "{$sourceMTime} {$dependencies}"; $compiled = "/* compiled at {$info} */\n" . $compiled; $this->filesystem->write('web/' . $targetPath, $compiled); } $assetInfo = new AssetInfo(); $assetInfo->setPath($targetPath); $assetInfo->setContentType('text/css'); return $assetInfo; }