The callback receives the type of output (out or err) and
some bytes from the output in real-time. It allows to have feedback
from the independent process during execution.
The STDOUT and STDERR are also available after the process is finished
via the getOutput() and getErrorOutput() methods.
public function filterDump(AssetInterface $asset) { $options = array($this->jpegtranBin); if ($this->optimize) { $options[] = '-optimize'; } if ($this->copy) { $options[] = '-copy'; $options[] = $this->copy; } if ($this->progressive) { $options[] = '-progressive'; } if (null !== $this->restart) { $options[] = '-restart'; $options[] = $this->restart; } $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_jpegtran'); file_put_contents($input, $asset->getContent()); $proc = new Process(implode(' ', array_map('escapeshellarg', $options))); $code = $proc->run(); unlink($input); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
public function filterLoad(AssetInterface $asset) { $options = array($this->nodePath, $this->coffeePath, '-sc'); $proc = new Process(implode(' ', array_map('escapeshellarg', $options)), null, array(), $asset->getContent()); $code = $proc->run(); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
public function filterLoad(AssetInterface $asset) { static $format = <<<'EOF' var less = require('less'); var sys = require('sys'); new(less.Parser)(%s).parse(%s, function(e, tree) { if (e) { less.writeError(e); process.exit(2); } try { sys.print(tree.toCSS(%s)); process.exit(0); } catch (e) { less.writeError(e); process.exit(3); } }); EOF; $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); // parser options $parserOptions = array(); if ($root && $path) { $parserOptions['paths'] = array(dirname($root . '/' . $path)); $parserOptions['filename'] = basename($path); } // tree options $treeOptions = array(); if (null !== $this->compress) { $treeOptions['compress'] = $this->compress; } // node.js configuration $env = array(); if (0 < count($this->nodePaths)) { $env['NODE_PATH'] = implode(':', $this->nodePaths); } $options = array($this->nodeBin); $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_less'); file_put_contents($input, sprintf($format, json_encode($parserOptions), json_encode($asset->getContent()), json_encode($treeOptions))); $proc = new Process(implode(' ', array_map('escapeshellarg', $options)), null, $env); $code = $proc->run(); unlink($input); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
public function filterDump(AssetInterface $asset) { $options = array($this->jpegoptimBin); if ($this->stripAll) { $options[] = '--strip-all'; } $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_jpegoptim'); file_put_contents($input, $asset->getContent()); $proc = new Process(implode(' ', array_map('escapeshellarg', $options))); $proc->run(); if (false !== strpos($proc->getOutput(), 'ERROR')) { unlink($input); throw new \RuntimeException($proc->getOutput()); } $asset->setContent(file_get_contents($input)); unlink($input); }
public function filterDump(AssetInterface $asset) { $cleanup = array(); $options = array($this->javaPath, '-jar', $this->jarPath); if (null !== $this->compilationLevel) { $options[] = '--compilation_level'; $options[] = $this->compilationLevel; } if (null !== $this->jsExterns) { $cleanup[] = $externs = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler'); file_put_contents($externs, $this->jsExterns); $options[] = '--externs'; $options[] = $externs; } if (null !== $this->externsUrl) { $cleanup[] = $externs = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler'); file_put_contents($externs, file_get_contents($this->externsUrl)); $options[] = '--externs'; $options[] = $externs; } if (null !== $this->excludeDefaultExterns) { $options[] = '--use_only_custom_externs'; } if (null !== $this->formatting) { $options[] = '--formatting'; $options[] = $this->formatting; } if (null !== $this->useClosureLibrary) { $options[] = '--manage_closure_dependencies'; } if (null !== $this->warningLevel) { $options[] = '--warning_level'; $options[] = $this->warningLevel; } $options[] = '--js'; $options[] = $cleanup[] = $input = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler'); file_put_contents($input, $asset->getContent()); $proc = new Process(implode(' ', array_map('escapeshellarg', $options))); $code = $proc->run(); array_map('unlink', $cleanup); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
/** * Compresses a string. * * @param string $content The content to compress * @param string $type The type of content, either "js" or "css" * @param array $options An indexed array of additional options * * @return string The compressed content */ protected function compress($content, $type, $options = array()) { // prepend the start of the command $options = array_merge(array($this->javaPath, '-jar', $this->jarPath, '--type', $type), $options); if (null !== $this->charset) { $options[] = '--charset'; $options[] = $this->charset; } if (null !== $this->lineBreak) { $options[] = '--line-break'; $options[] = $this->lineBreak; } $proc = new Process(implode(' ', array_map('escapeshellarg', $options)), null, array(), $content); $code = $proc->run(); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } return $proc->getOutput(); }
public function filterDump(AssetInterface $asset) { $options = array($this->optipngBin); if (null !== $this->level) { $options[] = '-o'; $options[] = $this->level; } $options[] = '-out'; $options[] = $output = tempnam(sys_get_temp_dir(), 'assetic_optipng'); unlink($output); $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_optipng'); file_put_contents($input, $asset->getContent()); $proc = new Process(implode(' ', array_map('escapeshellarg', $options))); $code = $proc->run(); if (0 < $code) { unlink($input); throw new \RuntimeException($proc->getOutput()); } $asset->setContent(file_get_contents($output)); unlink($input); unlink($output); }
/** * Hack around a bit, get the job done. */ public function filterLoad(AssetInterface $asset) { static $format = <<<'EOF' #!/usr/bin/env ruby require %s %s options = { :load_path => [], :source_files => [%s], :expand_paths => false } %ssecretary = Sprockets::Secretary.new(options) secretary.install_assets if options[:asset_root] print secretary.concatenation EOF; $more = ''; foreach ($this->includeDirs as $directory) { $more .= 'options[:load_path] << ' . var_export($directory, true) . "\n"; } if (null !== $this->assetRoot) { $more .= 'options[:asset_root] = ' . var_export($this->assetRoot, true) . "\n"; } if ($more) { $more .= "\n"; } $tmpAsset = tempnam(sys_get_temp_dir(), 'assetic_sprockets'); file_put_contents($tmpAsset, $asset->getContent()); $input = tempnam(sys_get_temp_dir(), 'assetic_sprockets'); file_put_contents($input, sprintf($format, $this->sprocketsLib ? sprintf('File.join(%s, \'sprockets\')', var_export($this->sprocketsLib, true)) : '\'sprockets\'', $this->getHack($asset), var_export($tmpAsset, true), $more)); $proc = new Process(implode(' ', array_map('escapeshellarg', array($this->rubyBin, $input)))); $code = $proc->run(); unlink($tmpAsset); unlink($input); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
public function filterDump(AssetInterface $asset) { $options = array($this->javaPath, '-jar', $this->jarPath); if (null !== $this->charset) { $options[] = '--charset'; $options[] = $this->charset; } if ($this->mhtml) { $options[] = '--mhtml'; } if (null !== $this->mhtmlRoot) { $options[] = '--mhtmlroot'; $options[] = $this->mhtmlRoot; } // automatically define root if not already defined if (null === $this->root) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); if ($root && $path) { $options[] = '--root'; $options[] = dirname($root . '/' . $path); } } else { $options[] = '--root'; $options[] = $this->root; } if ($this->skipMissing) { $options[] = '--skip-missing'; } if (null !== $this->maxUriLength) { $options[] = '--max-uri-length'; $options[] = $this->maxUriLength; } if (null !== $this->maxImageSize) { $options[] = '--max-image-size'; $options[] = $this->maxImageSize; } // input $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_cssembed'); file_put_contents($input, $asset->getContent()); $proc = new Process(implode(' ', array_map('escapeshellarg', $options))); $code = $proc->run(); unlink($input); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
public function filterLoad(AssetInterface $asset) { $options = array($this->sassPath); $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); if ($root && $path) { $options[] = '--load-path'; $options[] = dirname($root . '/' . $path); } if ($this->unixNewlines) { $options[] = '--unix-newlines'; } if (true === $this->scss || null === $this->scss && 'scss' == pathinfo($path, PATHINFO_EXTENSION)) { $options[] = '--scss'; } if ($this->style) { $options[] = '--style'; $options[] = $this->style; } if ($this->quiet) { $options[] = '--quiet'; } if ($this->debugInfo) { $options[] = '--debug-info'; } if ($this->lineNumbers) { $options[] = '--line-numbers'; } foreach ($this->loadPaths as $loadPath) { $options[] = '--load-path'; $options[] = $loadPath; } if ($this->cacheLocation) { $options[] = '--cache-location'; $options[] = $this->cacheLocation; } if ($this->noCache) { $options[] = '--no-cache'; } if ($this->compass) { $options[] = '--compass'; } // input $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_sass'); file_put_contents($input, $asset->getContent()); // output $options[] = $output = tempnam(sys_get_temp_dir(), 'assetic_sass'); $proc = new Process(implode(' ', array_map('escapeshellarg', $options))); $code = $proc->run(); if (0 < $code) { unlink($input); throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent(file_get_contents($output)); unlink($input); unlink($output); }
public function filterDump(AssetInterface $asset) { $options = array($this->pngoutBin); if (null !== $this->color) { $options[] = '-c' . $this->color; } if (null !== $this->filter) { $options[] = '-f' . $this->filter; } if (null !== $this->strategy) { $options[] = '-s' . $this->strategy; } if (null !== $this->blockSplitThreshold) { $options[] = '-b' . $this->blockSplitThreshold; } $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_pngout'); file_put_contents($input, $asset->getContent()); $output = tempnam(sys_get_temp_dir(), 'assetic_pngout'); unlink($output); $options[] = $output .= '.png'; $proc = new Process($cmd = implode(' ', array_map('escapeshellarg', $options))); $code = $proc->run(); if (0 < $code) { unlink($input); throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent(file_get_contents($output)); unlink($input); unlink($output); }
public function filterLoad(AssetInterface $asset) { $options = array($this->sassPath); $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); if ($root && $path) { $options[] = '--load-path'; $options[] = dirname($root . '/' . $path); } if ($this->unixNewlines) { $options[] = '--unix-newlines'; } if (true === $this->scss || null === $this->scss && 'scss' == pathinfo($path, PATHINFO_EXTENSION)) { $options[] = '--scss'; } if ($this->style) { $options[] = '--style'; $options[] = $this->style; } if ($this->quiet) { $options[] = '--quiet'; } if ($this->debugInfo) { $options[] = '--debug-info'; } if ($this->lineNumbers) { $options[] = '--line-numbers'; } foreach ($this->loadPaths as $loadPath) { $options[] = '--load-path'; $options[] = $loadPath; } if ($this->cacheLocation) { $options[] = '--cache-location'; $options[] = $this->cacheLocation; } if ($this->noCache) { $options[] = '--no-cache'; } if ($this->compass) { $options[] = '--compass'; } // input // @Changed: to compile sass directly from the original file becuase when // using --debug-info, it will reference the original file in FireSass instead // of some random /tmp/file_ssewRWRo9 file :) // Bad code as I have manually put in the dir separator ... I'm sure there is a better way! $options[] = $input = $asset->getSourceRoot() . '/' . $asset->getSourcePath(); // Removed: file_put_contents($input, $asset->getContent()); // output $options[] = $output = tempnam(sys_get_temp_dir(), 'assetic_sass'); $proc = new Process(implode(' ', array_map('escapeshellarg', $options))); $code = $proc->run(); if (0 < $code) { unlink($input); throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent(file_get_contents($output)); // Removed: unlink($input); // Unless you want your original files to be deleted!!! unlink($output); }