/** * Filters an asset after it has been loaded. * * @param \Assetic\Asset\AssetInterface $asset * @return void */ public function filterLoad(AssetInterface $asset) { $max_nesting_level = ini_get('xdebug.max_nesting_level'); $memory_limit = ini_get('memory_limit'); if ($max_nesting_level && $max_nesting_level < 200) { ini_set('xdebug.max_nesting_level', 200); } if ($memory_limit && $memory_limit < 256) { ini_set('memory_limit', '256M'); } $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); $dirs = array(); $lc = new \Less_Parser(array('compress' => true)); if ($root && $path) { $dirs[] = dirname($root . '/' . $path); } foreach ($this->loadPaths as $loadPath) { $dirs[] = $loadPath; } $lc->SetImportDirs($dirs); $url = parse_url($this->getRequest()->getUriForPath('')); $absolutePath = str_replace(public_path(), '', $root); if (isset($url['path'])) { $absolutePath = $url['path'] . $absolutePath; } $lc->parseFile($root . '/' . $path, $absolutePath); $asset->setContent($lc->getCss()); }
/** * Sets the by-config generated imports on the asset. * * {@inheritDoc} */ public function filterLoad(AssetInterface $asset) { $assetRoot = $asset->getSourceRoot(); $assetPath = $asset->getSourcePath(); $assetImportDir = dirname($assetRoot . '/' . $assetPath); $importDir = $this->config->getBootstrapPath() . '/less'; $this->setupLoadPaths($assetImportDir); // Make sure we _always_ have the bootstrap import dir. if ($importDir !== $assetImportDir) { $this->lessFilter->addLoadPath($importDir); } $variables = array_merge($this->extractVariables($importDir . '/variables.less'), $this->config->getVariables()); $variablesString = ''; foreach ($variables as $key => $value) { $variablesString .= "@{$key}:{$value};" . PHP_EOL; } if ('bootstrap.less' === $assetPath) { $imports = $this->filterImportFiles(array_unique(array_merge($this->extractImports($importDir . '/bootstrap.less'), $this->extractImports($importDir . '/responsive.less'), $this->config->getCustomComponents()))); $assetContent = $variablesString . $imports; $asset->setContent($assetContent); } else { $asset->setContent($variablesString . $asset->getContent()); } $this->lessFilter->filterLoad($asset); }
/** * Resolves an URL from an asset. * * @param AssetInterface $asset the asset containing the URL * @param string $url url read in file * * @return string an URL, a filepath */ public static function resolveUrl(AssetInterface $asset, $url) { // given URL is absolute URL if (false !== strpos($url, '://')) { return $url; } // source directory of the asset $root = dirname($asset->getSourceRoot() . '/' . $asset->getTargetPath()); // path directory where asset is being copied $path = dirname($asset->getTargetPath()); if ('.' === $path) { $image = $url; } else { $image = $path . '/' . $url; } if (null !== $root) { $image = $root . '/' . $url; } // cleanup local URLs if (false === strpos($image, '://')) { $image = self::removeQueryString($image); $image = self::removeAnchor($image); return self::resolveUps($image); } return $image; }
public function filterDump(AssetInterface $asset) { $content = ""; $files = array(); $extraFiles = array(); $absolutePath = $asset->getSourceRoot() . '/' . $asset->getSourcePath(); $this->parser->mime = $this->parser->mimeType($absolutePath); if ($this->parser->mime === 'javascripts') { $extraFiles = $this->parser->get("javascript_files", array()); } if ($this->parser->mime === 'stylesheets') { $extraFiles = $this->parser->get("stylesheet_files", array()); } $absoluteFilePaths = $this->parser->getFilesArrayFromDirectives($absolutePath); if ($absoluteFilePaths) { $absoluteFilePaths = $extraFiles + $absoluteFilePaths; } foreach ($absoluteFilePaths as $absoluteFilePath) { $files[] = $this->generator->file($absoluteFilePath, false); } if (!$absoluteFilePaths) { $files[] = $this->generator->file($absolutePath, false); } $global_filters = $this->parser->get("sprockets_filters.{$this->parser->mime}", array()); $collection = new AssetCollection($files, $global_filters); $asset->setContent($collection->dump()); }
/** * @param AssetInterface $asset */ public function filterDump(AssetInterface $asset) { $sourceBase = $asset->getSourceRoot(); $sourcePath = $asset->getSourcePath(); $assetRoot = $this->assetRoot; if (null === $sourcePath) { return; } $content = $this->filterReferences($asset->getContent(), function ($matches) use($sourceBase, $sourcePath, $assetRoot) { // its not a relative path if (false !== strpos($matches['url'], '://') || 0 === strpos($matches['url'], '//') || 0 === strpos($matches['url'], 'data:') || isset($matches['url'][0]) && '/' == $matches['url'][0]) { return $matches[0]; } $url = $matches['url']; if (false !== ($pos = strpos($url, '?'))) { $url = substr($url, 0, $pos); } $sourceAsset = dirname($sourceBase . '/' . $sourcePath) . '/' . $url; if (!is_file($sourceAsset)) { return $matches[0]; } $mimeType = MimeTypeGuesser::getInstance()->guess($sourceAsset); $destRelativePath = substr($mimeType, 0, strpos($mimeType, '/')) . '/' . basename($url); $destAsset = $assetRoot . '/' . $destRelativePath; if (!is_dir(dirname($destAsset))) { mkdir(dirname($destAsset), 0777, true); } copy($sourceAsset, $destAsset); return str_replace($matches['url'], '../' . $destRelativePath, $matches[0]); }); $asset->setContent($content); }
/** * Performs the asset dump. * * @param AssetInterface $asset An asset * @param OutputInterface $stdout The command output * * @throws RuntimeException If there is a problem writing the asset */ private function doDump(AssetInterface $asset, OutputInterface $stdout) { $combinations = VarUtils::getCombinations($asset->getVars(), $this->getContainer()->getParameter('assetic.variables')); foreach ($combinations as $combination) { $asset->setValues($combination); // resolve the target path $target = rtrim($this->basePath, '/') . '/' . $asset->getTargetPath(); $target = str_replace('_controller/', '', $target); $target = VarUtils::resolve($target, $asset->getVars(), $asset->getValues()); if (!is_dir($dir = dirname($target))) { $stdout->writeln(sprintf('<comment>%s</comment> <info>[dir+]</info> %s', date('H:i:s'), $dir)); if (false === @mkdir($dir, 0777, true)) { throw new \RuntimeException('Unable to create directory ' . $dir); } } $stdout->writeln(sprintf('<comment>%s</comment> <info>[file+]</info> %s', date('H:i:s'), $target)); if (OutputInterface::VERBOSITY_VERBOSE <= $stdout->getVerbosity()) { if ($asset instanceof AssetCollectionInterface) { foreach ($asset as $leaf) { $root = $leaf->getSourceRoot(); $path = $leaf->getSourcePath(); $stdout->writeln(sprintf(' <comment>%s/%s</comment>', $root ?: '[unknown root]', $path ?: '[unknown path]')); } } else { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); $stdout->writeln(sprintf(' <comment>%s/%s</comment>', $root ?: '[unknown root]', $path ?: '[unknown path]')); } } if (false === @file_put_contents($target, $asset->dump())) { throw new \RuntimeException('Unable to write file ' . $target); } } }
/** * Update a given hash with the sha1 hash of an individual asset * * @param AssetInterface $asset * @param $hash */ protected function hashAsset(AssetInterface $asset, $hash) { $sourcePath = $asset->getSourcePath(); $sourceRoot = $asset->getSourceRoot(); $data = $sourcePath && $sourceRoot && file_exists($sourceRoot . "/" . $sourcePath) ? hash_file('sha1', $sourceRoot . "/" . $sourcePath) : $sourcePath; hash_update($hash, $data); }
public function filterLoad(AssetInterface $asset) { $parser = new Less_Parser(); // CSS Rewriter will take care of this $parser->SetOption('relativeUrls', false); $parser->parseFile($asset->getSourceRoot() . '/' . $asset->getSourcePath()); $asset->setContent($parser->getCss()); }
public function filterDump(AssetInterface $asset) { $this->root = $asset->getSourceRoot() . '/'; $this->file = $asset->getSourcePath(); $this->base = $this->getRelativePath($this->paths, $this->root); $content = $this->filterReferences($asset->getContent(), array($this, 'url_matcher')); $asset->setContent($content); }
public function filterDump(AssetInterface $asset) { $sourceBase = $asset->getSourceRoot(); $sourcePath = $asset->getSourcePath(); $targetPath = $asset->getTargetPath(); if (null === $sourcePath || null === $targetPath || $sourcePath == $targetPath) { return; } // learn how to get from the target back to the source if (false !== strpos($sourceBase, '://')) { list($scheme, $url) = explode('://', $sourceBase . '/' . $sourcePath, 2); list($host, $path) = explode('/', $url, 2); $host = $scheme . '://' . $host . '/'; $path = false === strpos($path, '/') ? '' : dirname($path); $path .= '/'; } else { // assume source and target are on the same host $host = ''; // pop entries off the target until it fits in the source if ('.' == dirname($sourcePath)) { $path = str_repeat('../', substr_count($targetPath, '/')); } elseif ('.' == ($targetDir = dirname($targetPath))) { $path = dirname($sourcePath) . '/'; } else { while (0 !== strpos($sourcePath, $targetDir)) { if (false !== ($pos = strrpos($targetDir, '/'))) { $targetDir = substr($targetDir, 0, $pos); } else { $targetDir = ''; break; } } $path = '/'; $path .= ltrim(substr(dirname($sourcePath) . '/', strlen($targetDir)), '/'); } } $content = $this->filterReferences($asset->getContent(), function ($matches) use($host, $path) { if (false !== strpos($matches['url'], '://') || 0 === strpos($matches['url'], '//') || 0 === strpos($matches['url'], 'data:')) { // absolute or protocol-relative or data uri return $matches[0]; } if ('/' == $matches['url'][0]) { // root relative return str_replace($matches['url'], $host . $matches['url'], $matches[0]); } // document relative $url = $matches['url']; $parts = array(); foreach (explode('/', $host . $path . $url) as $part) { if ('..' === $part && count($parts) && '..' !== end($parts)) { array_pop($parts); } $parts[] = $part; } return str_replace($matches['url'], implode('/', $parts), $matches[0]); }); $asset->setContent($content); }
public function filterLoad(AssetInterface $asset) { $importFilter = $this->importFilter; $sourceRoot = $asset->getSourceRoot(); $sourcePath = $asset->getSourcePath(); $callback = function ($matches) use($importFilter, $sourceRoot, $sourcePath) { if (!$matches['url']) { return $matches[0]; } if (null === $sourceRoot) { return $matches[0]; } $importRoot = $sourceRoot; if (false !== strpos($matches['url'], '://')) { // absolute list($importScheme, $tmp) = explode('://', $matches['url'], 2); list($importHost, $importPath) = explode('/', $tmp, 2); $importRoot = $importScheme . '://' . $importHost; } elseif (0 === strpos($matches['url'], '//')) { // protocol-relative list($importHost, $importPath) = explode('/', substr($matches['url'], 2), 2); $importHost = '//' . $importHost; } elseif ('/' == $matches['url'][0]) { // root-relative $importPath = substr($matches['url'], 1); } elseif (null !== $sourcePath) { // document-relative $importPath = $matches['url']; if ('.' != ($sourceDir = dirname($sourcePath))) { $importPath = $sourceDir . '/' . $importPath; } } else { return $matches[0]; } // ignore other imports if ('css' != pathinfo($importPath, PATHINFO_EXTENSION)) { return $matches[0]; } $importSource = $importRoot . '/' . $importPath; if (false !== strpos($importSource, '://') || 0 === strpos($importSource, '//')) { $import = new HttpAsset($importSource, array($importFilter), true); } elseif (!file_exists($importSource)) { // ignore not found imports return $matches[0]; } else { $import = new FileAsset($importSource, array($importFilter), $importRoot, $importPath); } $import->setTargetPath($sourcePath); return $import->dump(); }; $content = $asset->getContent(); $lastHash = md5($content); do { $content = $this->filterImports($content, $callback); $hash = md5($content); } while ($lastHash != $hash && ($lastHash = $hash)); $asset->setContent($content); }
/** * Filters an asset after it has been loaded. * * @param AssetInterface $asset An asset */ public function filterLoad(AssetInterface $asset) { $parser = clone $this->parser; $parser->SetOptions($this->options); if ($this->str_endswith($asset->getSourcePath(), ['.less'])) { $parser->parseFile($asset->getSourceRoot() . DIRECTORY_SEPARATOR . $asset->getSourcePath()); $asset->setContent($parser->getCss()); } }
/** * {@inheritdoc} */ public function getSourceRoot() { if ($this->innerAsset) { return $this->innerAsset->getSourceRoot(); } // Not accurate, but this method is called by AssetCollectionIterator // before setValues() is called, so we need to return something return null; }
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); }
/** * Filters an asset after it has been loaded. * * @param AssetInterface $asset An asset */ public function filterLoad(AssetInterface $asset) { if ($this->str_endswith($asset->getSourcePath(), ['.less'])) { $file = Less_Cache::Get([$asset->getSourceRoot() . DIRECTORY_SEPARATOR . $asset->getSourcePath() => '/'], $this->options); if (is_readable($this->options['cache_dir'] . DIRECTORY_SEPARATOR . $file)) { $asset->setContent(file_get_contents($this->options['cache_dir'] . DIRECTORY_SEPARATOR . $file)); } } }
/** * Preprocesses a sass asset file * * @param AssetInterface $asset */ public function filterLoad(AssetInterface $asset) { $assetSourceBase = $asset->getSourceRoot(); $assetSourcePath = $asset->getSourcePath(); $assetAbsolutePath = pathinfo("{$assetSourceBase}/{$assetSourcePath}", PATHINFO_DIRNAME); $filePath = realpath($assetSourcePath); $kernelRootDir = $this->getKernel()->getRootDir(); $content = $this->rewriteSassContent($asset->getContent(), $assetAbsolutePath); $asset->setContent($content); }
public function filterLoad(AssetInterface $asset) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); $lc = new \lessc(); if ($root && $path) { $lc->importDir = dirname($root . '/' . $path); } $asset->setContent($lc->parse($asset->getContent())); }
/** * @param \Assetic\Asset\AssetInterface $asset */ public function filterLoad(AssetInterface $asset) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); $filename = realpath($root . '/' . $path); if (file_exists($filename)) { touch($filename); } parent::filterLoad($asset); }
/** * Filters an asset after it has been loaded. * * @param AssetInterface $asset An asset */ public function filterLoad(AssetInterface $asset) { if ($this->str_endswith($asset->getSourcePath(), ['.js'])) { $moduleName = basename($asset->getSourcePath(), '.js'); $delivery = $this->delivery->create(['includes' => [$asset->getSourceRoot()]]); $delivery->addModule($moduleName); $delivery->setMainModule($moduleName); $asset->setContent($delivery->getOutput()); } }
public function filterLoad(AssetInterface $asset) { $content = $asset->getContent(); $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); if (!empty($content)) { $content = \CoffeeScript\Compiler::compile($asset->getContent(), array('filename' => $path)); } $asset->setContent($content); }
public function filterLoad(AssetInterface $asset) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); $lc = new \scssc(); if ($root && $path) { $lc->addImportPath(dirname($root . '/' . $path)); } $asset->setContent($lc->compile($asset->getContent())); }
public function filterLoad(AssetInterface $asset) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); $pce = new CssEmbed(); if ($root && $path) { $pce->setRootDir(dirname($root . '/' . $path)); } $asset->setContent($pce->embedString($asset->getContent())); }
public function filterLoad(AssetInterface $asset) { $parser = new Less_Parser(); // CSS Rewriter will take care of this $parser->SetOption('relativeUrls', false); $parser->parseFile($asset->getSourceRoot() . '/' . $asset->getSourcePath()); // Set the LESS variables after parsing to override them $parser->ModifyVars($this->presets); $asset->setContent($parser->getCss()); }
/** * Filters an asset after it has been loaded. * * @param \Assetic\Asset\AssetInterface $asset * @return void */ public function filterLoad(AssetInterface $asset) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); $compiler = new \SassParser($this->presets); if ($root and $path) { $compiler->load_paths = array_merge($compiler->load_paths, array($root . '/' . $path)); } $compiler->load_paths = array_merge($compiler->load_paths, $this->importPaths); $asset->setContent($compiler->toCss($asset->getContent(), false)); }
/** * @param mixed[] $reference * @param AssetInterface $asset * * @return string */ public function rewrite(array $reference, AssetInterface $asset) { if (!$this->isRewritable($reference['url'], $asset->getSourcePath(), $asset->getTargetPath())) { return $reference[0]; } $absoluteSourcePath = $this->getAssetPath(dirname($this->createAssetPath($asset->getSourceRoot(), $asset->getSourcePath())), parse_url($reference['url'])['path']); $webTargetPath = $this->createAssetPath($this->rewriteDirectory, $this->namer->name($absoluteSourcePath, $asset)); $absoluteTargetPath = $this->webDirectory . $webTargetPath; $this->filesystem->{$this->strategy}($absoluteSourcePath, $absoluteTargetPath, true); return str_replace($reference['url'], $this->packages->getUrl($webTargetPath), $reference[0]); }
public function filterDump(AssetInterface $asset) { $sourceBase = $asset->getSourceRoot(); $sourcePath = $asset->getSourcePath(); $targetPath = $asset->getTargetPath(); if (null === $sourcePath || null === $targetPath || $sourcePath == $targetPath) { return; } $content = \Minify_CSS_UriRewriter::rewrite($asset->getContent(), $sourceBase); $asset->setContent($content); }
public function filterDump(AssetInterface $asset) { $relativePath = $this->getRelativePath($this->basePath, $asset->getSourceRoot() . '/'); $filename = pathinfo($asset->getSourcePath(), PATHINFO_FILENAME); $content = str_replace('"', '\\"', $asset->getContent()); $content = str_replace(PHP_EOL, "", $content); $jst = 'JST = (typeof JST === "undefined") ? JST = {} : JST;' . PHP_EOL; $jst .= 'JST["' . $relativePath . $filename . '"] = "'; $jst .= $content; $jst .= '";' . PHP_EOL; $asset->setContent($jst); }
public function filterLoad(AssetInterface $asset) { static $format = <<<'EOF' var less = require('less'); var sys = require(process.binding('natives').util ? 'util' : 'sys'); new(less.Parser)(%s).parse(%s, function(e, tree) { if (e) { less.writeError(e); process.exit(2); } try { sys.print(tree.toCSS(%s)); } 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); } foreach ($this->loadPaths as $loadPath) { $parserOptions['paths'][] = $loadPath; } // tree options $treeOptions = array(); if (null !== $this->compress) { $treeOptions['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_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 FilterException::fromProcess($proc)->setInput($asset->getContent()); } $asset->setContent($proc->getOutput()); }
public function filterLoad(AssetInterface $asset) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); $lc = new \lessc(); if ($root && $path) { $lc->importDir = dirname($root . '/' . $path); } foreach ($this->loadPaths as $loadPath) { $lc->addImportDir($loadPath); } $asset->setContent($lc->parse($asset->getContent(), $this->presets)); }
/** * Filters an asset after it has been loaded. * * @param Assetic\Asset\AssetInterface $asset */ public function filterLoad(AssetInterface $asset) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); $presets = $this->presets; // If we have a root and path, set the 'file' // preset now, which aids in debugging should // something go wrong. if ($root and $path) { $presets = array_merge($presets, array('file' => $root . '/' . $path)); } $asset->setContent(Compiler::compile($asset->getContent(), $presets)); }