public function filterLoad(AssetInterface $asset) { static $manifest = <<<EOF name: Application%s sources: [source.js] EOF; $hash = substr(sha1(time() . rand(11111, 99999)), 0, 7); $package = FilesystemUtils::getTemporaryDirectory() . '/assetic_packager_' . $hash; mkdir($package); file_put_contents($package . '/package.yml', sprintf($manifest, $hash)); file_put_contents($package . '/source.js', $asset->getContent()); $packager = new \Packager(array_merge(array($package), $this->packages)); $content = $packager->build(array(), array(), array('Application' . $hash)); unlink($package . '/package.yml'); unlink($package . '/source.js'); rmdir($package); $asset->setContent($content); }
/** * 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 = $this->createProcessBuilder(array($this->javaPath)); if (null !== $this->stackSize) { $pb->add('-Xss' . $this->stackSize); } $pb->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 = FilesystemUtils::getTemporaryDirectory(); $input = tempnam($tempDir, 'assetic_yui_input'); $output = tempnam($tempDir, 'assetic_yui_output'); file_put_contents($input, $content); $pb->add('-o')->add($output)->add('--type')->add($type)->add($input); $proc = $pb->getProcess(); $code = $proc->run(); unlink($input); if (0 !== $code) { if (file_exists($output)) { unlink($output); } throw FilterException::fromProcess($proc)->setInput($content); } if (!file_exists($output)) { throw new \RuntimeException('Error creating output file.'); } $retval = file_get_contents($output); unlink($output); return $retval; }
public function __construct($sassPath = '/usr/bin/sass', $rubyPath = null) { $this->sassPath = $sassPath; $this->rubyPath = $rubyPath; $this->cacheLocation = FilesystemUtils::getTemporaryDirectory(); }
public function filterLoad(AssetInterface $asset) { $loadPaths = $this->loadPaths; if ($dir = $asset->getSourceDirectory()) { $loadPaths[] = $dir; } $tempDir = $this->cacheLocation ? $this->cacheLocation : FilesystemUtils::getTemporaryDirectory(); $compassProcessArgs = array($this->compassPath, 'compile', $tempDir); if (null !== $this->rubyPath) { $compassProcessArgs = array_merge(explode(' ', $this->rubyPath), $compassProcessArgs); } $pb = $this->createProcessBuilder($compassProcessArgs); if ($this->force) { $pb->add('--force'); } if ($this->style) { $pb->add('--output-style')->add($this->style); } if ($this->quiet) { $pb->add('--quiet'); } if ($this->boring) { $pb->add('--boring'); } if ($this->noLineComments) { $pb->add('--no-line-comments'); } // these three 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->relativeAssets) { $pb->add('--relative-assets'); } if ($this->javascriptsDir) { $pb->add('--javascripts-dir')->add($this->javascriptsDir); } if ($this->fontsDir) { $pb->add('--fonts-dir')->add($this->fontsDir); } // options in config file $optionsConfig = array(); if (!empty($loadPaths)) { $optionsConfig['additional_import_paths'] = $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->httpFontsPath) { $optionsConfig['http_fonts_path'] = $this->httpFontsPath; } if ($this->httpGeneratedImagesPath) { $optionsConfig['http_generated_images_path'] = $this->httpGeneratedImagesPath; } if ($this->generatedImagesPath) { $optionsConfig['generated_images_path'] = $this->generatedImagesPath; } 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 = $asset->getSourcePath()) { // 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 $input = $tempName . '.' . $type; // work-around for https://github.com/chriseppstein/compass/issues/748 if (defined('PHP_WINDOWS_VERSION_MAJOR')) { $input = str_replace('\\', '/', $input); } $pb->add($input); file_put_contents($input, $asset->getContent()); // output $output = $tempName . '.css'; if ($this->homeEnv) { // it's not really usefull but... https://github.com/chriseppstein/compass/issues/376 $pb->setEnv('HOME', FilesystemUtils::getTemporaryDirectory()); $this->mergeEnv($pb); } $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); } }