/** * Rebuild and rotate the specified index(es). * * @param array|string $indexes The index(es) to rotate. */ public function rotate($indexes) { $pb = new ProcessBuilder(); $pb->inheritEnvironmentVariables()->add($this->bin)->add('--rotate'); if (is_array($indexes)) { foreach ($indexes as &$label) { if (isset($this->indexes[$label])) { $pb->add($this->indexes[$label]['index_name']); } } } elseif (is_string($indexes)) { if (isset($this->indexes[$indexes])) { $pb->add($this->indexes[$indexes]['index_name']); } } else { throw new \RuntimeException(sprintf('Indexes can only be an array or string, %s given.', gettype($indexes))); } /** * FIXME: Throw an error if no valid indexes were provided? */ $indexer = $pb->getProcess(); $code = $indexer->run(); if (($errStart = strpos($indexer->getOutput(), 'FATAL:')) !== false) { if (($errEnd = strpos($indexer->getOutput(), "\n", $errStart)) !== false) { $errMsg = substr($indexer->getOutput(), $errStart, $errEnd); } else { $errMsg = substr($indexer->getOutput(), $errStart); } throw new \RuntimeException(sprintf('Error rotating indexes: "%s".', rtrim($errMsg))); } }
public function filterDump(AssetInterface $asset) { $pb = new ProcessBuilder(); $pb->inheritEnvironmentVariables()->add($this->pngoutBin); if (null !== $this->color) { $pb->add('-c' . $this->color); } if (null !== $this->filter) { $pb->add('-f' . $this->filter); } if (null !== $this->strategy) { $pb->add('-s' . $this->strategy); } if (null !== $this->blockSplitThreshold) { $pb->add('-b' . $this->blockSplitThreshold); } $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_pngout')); file_put_contents($input, $asset->getContent()); $output = tempnam(sys_get_temp_dir(), 'assetic_pngout'); unlink($output); $pb->add($output .= '.png'); $proc = $pb->getProcess(); $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) { $pb = new ProcessBuilder(); $pb->add($this->nodePath)->add($this->coffeePath)->add('-sc')->setInput($asset->getContent()); $proc = $pb->getProcess(); $code = $proc->run(); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
/** * @test */ public function shouldNotReplaceExplicitlySetVars() { $snapshot = $_ENV; $_ENV = array('foo' => 'bar'); $expected = array('foo' => 'baz'); $pb = new ProcessBuilder(); $pb->setEnv('foo', 'baz')->inheritEnvironmentVariables()->add('foo'); $proc = $pb->getProcess(); $this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() copies $_ENV'); $_ENV = $snapshot; }
public function filterLoad(AssetInterface $asset) { $input = tempnam(sys_get_temp_dir(), 'assetic_coffeescript'); file_put_contents($input, $asset->getContent()); $pb = new ProcessBuilder(array($this->nodePath, $this->coffeePath, '-cp', $input)); $proc = $pb->getProcess(); $code = $proc->run(); unlink($input); 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; } $pb = new ProcessBuilder(); // node.js configuration if (0 < count($this->nodePaths)) { $pb->setEnv('NODE_PATH', implode(':', $this->nodePaths)); } $pb->add($this->nodeBin)->add($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 = $pb->getProcess(); $code = $proc->run(); unlink($input); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
public function filterLoad(AssetInterface $asset) { $pb = new ProcessBuilder(); $pb->inheritEnvironmentVariables()->add($this->sassPath); $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); if ($root && $path) { $pb->add('--load-path')->add(dirname($root . '/' . $path)); } if ($this->unixNewlines) { $pb->add('--unix-newlines'); } if (true === $this->scss || null === $this->scss && 'scss' == pathinfo($path, PATHINFO_EXTENSION)) { $pb->add('--scss'); } if ($this->style) { $pb->add('--style')->add($this->style); } if ($this->quiet) { $pb->add('--quiet'); } if ($this->debugInfo) { $pb->add('--debug-info'); } if ($this->lineNumbers) { $pb->add('--line-numbers'); } foreach ($this->loadPaths as $loadPath) { $pb->add('--load-path')->add($loadPath); } if ($this->cacheLocation) { $pb->add('--cache-location')->add($this->cacheLocation); } if ($this->noCache) { $pb->add('--no-cache'); } if ($this->compass) { $pb->add('--compass'); } // input $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_sass')); file_put_contents($input, $asset->getContent()); $proc = $pb->getProcess(); $code = $proc->run(); unlink($input); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
public function filterDump(AssetInterface $asset) { $pb = new ProcessBuilder(); $pb->inheritEnvironmentVariables()->add($this->jpegtranBin); if ($this->optimize) { $pb->add('-optimize'); } if ($this->copy) { $pb->add('-copy')->add($this->copy); } if ($this->progressive) { $pb->add('-progressive'); } if (null !== $this->restart) { $pb->add('-restart')->add($this->restart); } $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_jpegtran')); file_put_contents($input, $asset->getContent()); $proc = $pb->getProcess(); $code = $proc->run(); unlink($input); 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()) { $pb = new ProcessBuilder(); $pb->inheritEnvironmentVariables()->add($this->javaPath)->add('-jar')->add($this->jarPath); foreach ($options as $option) { $pb->add($option); } if (null !== $this->charset) { $pb->add('--charset')->add($this->charset); } if (null !== $this->lineBreak) { $pb->add('--line-break')->add($this->lineBreak); } // input and output files $tempDir = realpath(sys_get_temp_dir()); $hash = substr(sha1(time() . rand(11111, 99999)), 0, 7); $input = $tempDir . DIRECTORY_SEPARATOR . $hash . '.' . $type; $output = $tempDir . DIRECTORY_SEPARATOR . $hash . '-min.' . $type; file_put_contents($input, $content); $pb->add('-o')->add($output)->add($input); $proc = $pb->getProcess(); $code = $proc->run(); unlink($input); if (0 < $code) { if (file_exists($output)) { unlink($output); } throw new \RuntimeException($proc->getErrorOutput()); } elseif (!file_exists($output)) { throw new \RuntimeException('Error creating output file.'); } $retval = file_get_contents($output); unlink($output); return $retval; }
public function filterDump(AssetInterface $asset) { $pb = new ProcessBuilder(); $pb->add($this->jpegoptimBin); if ($this->stripAll) { $pb->add('--strip-all'); } $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_jpegoptim')); file_put_contents($input, $asset->getContent()); $proc = $pb->getProcess(); $proc->run(); if (false !== strpos($proc->getOutput(), 'ERROR')) { unlink($input); throw new \RuntimeException($proc->getOutput()); } $asset->setContent(file_get_contents($input)); unlink($input); }
/** * {@inheritdoc} */ public function filterLoad(AssetInterface $asset) { static $format = <<<'EOF' var stylus = require('stylus'); var sys = require(process.binding('natives').util ? 'util' : 'sys'); stylus(%s, %s).render(function(e, css){ if (e) { throw e; } sys.print(css); process.exit(0); }); EOF; $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); // parser options $parserOptions = array(); if ($root && $path) { $parserOptions['paths'] = array(dirname($root . '/' . $path)); $parserOptions['filename'] = basename($path); } if (null !== $this->compress) { $parserOptions['compress'] = $this->compress; } $pb = new ProcessBuilder(); $pb->inheritEnvironmentVariables(); // node.js configuration if (0 < count($this->nodePaths)) { $pb->setEnv('NODE_PATH', implode(':', $this->nodePaths)); } $pb->add($this->nodeBin)->add($input = tempnam(sys_get_temp_dir(), 'assetic_stylus')); file_put_contents($input, sprintf($format, json_encode($asset->getContent()), json_encode($parserOptions))); $proc = $pb->getProcess(); $code = $proc->run(); unlink($input); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
public function filterDump(AssetInterface $asset) { $pb = new ProcessBuilder(); $pb->inheritEnvironmentVariables()->add($this->javaPath)->add('-jar')->add($this->jarPath); if (null !== $this->charset) { $pb->add('--charset')->add($this->charset); } if ($this->mhtml) { $pb->add('--mhtml'); } if (null !== $this->mhtmlRoot) { $pb->add('--mhtmlroot')->add($this->mhtmlRoot); } // automatically define root if not already defined if (null === $this->root) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); if ($root && $path) { $pb->add('--root')->add(dirname($root . '/' . $path)); } } else { $pb->add('--root')->add($this->root); } if ($this->skipMissing) { $pb->add('--skip-missing'); } if (null !== $this->maxUriLength) { $pb->add('--max-uri-length')->add($this->maxUriLength); } if (null !== $this->maxImageSize) { $pb->add('--max-image-size')->add($this->maxImageSize); } // input $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_cssembed')); file_put_contents($input, $asset->getContent()); $proc = $pb->getProcess(); $code = $proc->run(); unlink($input); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
public function filterDump(AssetInterface $asset) { $pb = new ProcessBuilder(array($this->optipngBin)); if (null !== $this->level) { $pb->add('-o')->add($this->level); } $pb->add('-out')->add($output = tempnam(sys_get_temp_dir(), 'assetic_optipng')); unlink($output); $pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_optipng')); file_put_contents($input, $asset->getContent()); $proc = $pb->getProcess(); $code = $proc->run(); if (0 < $code) { unlink($input); throw new \RuntimeException($proc->getErrorOutput()); } $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)); $pb = new ProcessBuilder(); $pb->add($this->rubyBin)->add($input); $proc = $pb->getProcess(); $code = $proc->run(); unlink($tmpAsset); unlink($input); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } $asset->setContent($proc->getOutput()); }
public function filterDump(AssetInterface $asset) { $cleanup = array(); $pb = new ProcessBuilder(array($this->javaPath, '-jar', $this->jarPath)); if (null !== $this->compilationLevel) { $pb->add('--compilation_level')->add($this->compilationLevel); } if (null !== $this->jsExterns) { $cleanup[] = $externs = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler'); file_put_contents($externs, $this->jsExterns); $pb->add('--externs')->add($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)); $pb->add('--externs')->add($externs); } if (null !== $this->excludeDefaultExterns) { $pb->add('--use_only_custom_externs'); } if (null !== $this->formatting) { $pb->add('--formatting')->add($this->formatting); } if (null !== $this->useClosureLibrary) { $pb->add('--manage_closure_dependencies'); } if (null !== $this->warningLevel) { $pb->add('--warning_level')->add($this->warningLevel); } $pb->add('--js')->add($cleanup[] = $input = tempnam(sys_get_temp_dir(), 'assetic_google_closure_compiler')); file_put_contents($input, $asset->getContent()); $proc = $pb->getProcess(); $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()) { $pb = new ProcessBuilder(); $pb->add($this->javaPath)->add('-jar')->add($this->jarPath)->add('--type')->add($type); foreach ($options as $option) { $pb->add($option); } if (null !== $this->charset) { $pb->add('--charset')->add($this->charset); } if (null !== $this->lineBreak) { $pb->add('--line-break')->add($this->lineBreak); } $pb->setInput($content); $proc = $pb->getProcess(); $code = $proc->run(); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } return $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()) { $pb = new ProcessBuilder(); $pb ->inheritEnvironmentVariables() ->add($this->javaPath) ->add('-jar') ->add($this->jarPath) ->add('--type') ->add($type) ; foreach ($options as $option) { $pb->add($option); } if (null !== $this->charset) { $pb->add('--charset')->add($this->charset); } if (null !== $this->lineBreak) { $pb->add('--line-break')->add($this->lineBreak); } $input = tempnam(sys_get_temp_dir(), 'assetic_yui_compressor'); file_put_contents($input, $content); $pb->add($input); $proc = $pb->getProcess(); $code = $proc->run(); unlink($input); if (0 < $code) { throw new \RuntimeException($proc->getErrorOutput()); } return $proc->getOutput(); }
public function filterLoad(AssetInterface $asset) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); if ($root && $path) { $this->loadPaths[] = dirname($root . '/' . $path); } // compass does not seems to handle symlink, so we use realpath() $tempDir = realpath(sys_get_temp_dir()); $pb = new ProcessBuilder(array($this->compassPath, 'compile', $tempDir)); $pb->inheritEnvironmentVariables(); if ($this->force) { $pb->add('--force'); } if ($this->style) { $pb->add('--output-style')->add($this->style); } if ($this->quiet) { $pb->add('--quiet'); } if ($this->noLineComments) { $pb->add('--no-line-comments'); } // these two options are not passed into the config file // because like this, compass adapts this to be xxx_dir or xxx_path // whether it's an absolute path or not if ($this->imagesDir) { $pb->add('--images-dir')->add($this->imagesDir); } if ($this->javascriptsDir) { $pb->add('--javascripts-dir')->add($this->javascriptsDir); } // options in config file $optionsConfig = array(); if (!empty($this->loadPaths)) { $optionsConfig['additional_import_paths'] = $this->loadPaths; } if ($this->unixNewlines) { $optionsConfig['sass_options']['unix_newlines'] = true; } if ($this->debugInfo) { $optionsConfig['sass_options']['debug_info'] = true; } if ($this->cacheLocation) { $optionsConfig['sass_options']['cache_location'] = $this->cacheLocation; } if ($this->noCache) { $optionsConfig['sass_options']['no_cache'] = true; } if ($this->httpPath) { $optionsConfig['http_path'] = $this->httpPath; } if ($this->httpImagesPath) { $optionsConfig['http_images_path'] = $this->httpImagesPath; } if ($this->httpJavascriptsPath) { $optionsConfig['http_javascripts_path'] = $this->httpJavascriptsPath; } // options in configuration file if (count($optionsConfig)) { $config = array(); foreach ($this->plugins as $plugin) { $config[] = sprintf("require '%s'", addcslashes($plugin, '\\')); } foreach ($optionsConfig as $name => $value) { if (!is_array($value)) { $config[] = sprintf('%s = "%s"', $name, addcslashes($value, '\\')); } elseif (!empty($value)) { $config[] = sprintf('%s = %s', $name, $this->formatArrayToRuby($value)); } } $configFile = tempnam($tempDir, 'assetic_compass'); file_put_contents($configFile, implode("\n", $config) . "\n"); $pb->add('--config')->add($configFile); } $pb->add('--sass-dir')->add('')->add('--css-dir')->add(''); // compass choose the type (sass or scss from the filename) if (null !== $this->scss) { $type = $this->scss ? 'scss' : 'sass'; } elseif ($path) { // FIXME: what if the extension is something else? $type = pathinfo($path, PATHINFO_EXTENSION); } else { $type = 'scss'; } $tempName = tempnam($tempDir, 'assetic_compass'); unlink($tempName); // FIXME: don't use tempnam() here // input $pb->add($input = $tempName . '.' . $type); file_put_contents($input, $asset->getContent()); // output $output = $tempName . '.css'; // it's not really usefull but... https://github.com/chriseppstein/compass/issues/376 $pb->setEnv('HOME', sys_get_temp_dir()); $proc = $pb->getProcess(); $code = $proc->run(); if (0 < $code) { unlink($input); if (isset($configFile)) { unlink($configFile); } throw FilterException::fromProcess($proc)->setInput($asset->getContent()); } $asset->setContent(file_get_contents($output)); unlink($input); unlink($output); if (isset($configFile)) { unlink($configFile); } }