/** * {@inheritDoc} */ public function getDependencies(AssetTarget $target) { $children = []; foreach ($target->files() as $file) { $imports = CssUtils::extractImports($file->contents()); if (empty($imports)) { continue; } $ext = $this->_settings['ext']; $extLength = strlen($ext); $deps = []; foreach ($imports as $name) { if (preg_match('/(.*)\\/\\*\\*\\/\\*$/', $name, $matches)) { $path = $this->_findFile($matches[1]); $relPathBegin = mb_strlen($path) - mb_strlen($matches[1]); $result = []; $this->_dirToArray($path, $relPathBegin, $result); $deps = array_merge($deps, $result); continue; } $pathinfo = pathinfo($name); $nameAlt = ''; if (substr($pathinfo['basename'], 0, 1) !== '_') { $name = $pathinfo['dirname'] . '/_' . $pathinfo['basename']; $nameAlt = $pathinfo['dirname'] . '/' . $pathinfo['basename']; } if ($ext !== substr($name, -$extLength)) { $name .= $ext; $nameAlt .= $ext; } $deps[] = $name; if ($nameAlt !== '') { $deps[] = $nameAlt; } } foreach ($deps as $import) { $path = $this->_findFile($import); try { $file = new Local($path); $newTarget = new AssetTarget('phony.css', [$file]); $children[] = $file; } catch (\Exception $e) { // Do nothing, we just skip missing files. // sometimes these are things like compass or bourbon // internals. $newTarget = false; } // Only recurse through non-css imports as css files are not // inlined by less/sass. if ($newTarget && $ext === substr($import, -$extLength)) { $children = array_merge($children, $this->getDependencies($newTarget)); } } } return $children; }
public function testExtractImports() { $css = <<<CSS @import 'first.css'; @import 'second.css'; @import "third.css"; @import '../../relative-path.css'; @import "http://example.com/dir/absolute-path.css"; CSS; $result = CssUtils::extractImports($css); $this->assertCount(5, $result); $expected = ['first.css', 'second.css', 'third.css', '../../relative-path.css', 'http://example.com/dir/absolute-path.css']; $this->assertEquals($expected, $result); }
/** * {@inheritDoc} */ public function getDependencies(AssetTarget $target) { $children = []; $hasPrefix = property_exists($this, 'optionalDependencyPrefix') && !empty($this->optionalDependencyPrefix); foreach ($target->files() as $file) { $imports = CssUtils::extractImports($file->contents()); if (empty($imports)) { continue; } $ext = $this->_settings['ext']; $extLength = strlen($ext); $deps = []; foreach ($imports as $name) { if ('.css' === substr($name, -4)) { // skip normal css imports continue; } if ($ext !== substr($name, -$extLength)) { $name .= $ext; } $deps[] = $name; if ($hasPrefix) { $deps[] = $this->_prependPrefixToFilename($name); } } foreach ($deps as $import) { $path = $this->_findFile($import); try { $file = new Local($path); $newTarget = new AssetTarget('phony.css', [$file]); $children[] = $file; } catch (\Exception $e) { // Do nothing, we just skip missing files. // sometimes these are things like compass or bourbon // internals. $newTarget = false; } // Only recurse through non-css imports as css files are not // inlined by less/sass. if ($newTarget && $ext === substr($import, -$extLength)) { $children = array_merge($children, $this->getDependencies($newTarget)); } } } return $children; }