*/ class CssRewriteFilter implements FilterInterface { public function filterLoad(AssetInterface $asset) { } public function filterDump(AssetInterface $asset) { $sourceUrl = $asset->getSourceUrl(); $targetUrl = $asset->getTargetUrl(); if (null === $sourceUrl || null === $targetUrl || $sourceUrl == $targetUrl) { return; } // learn how to get from the target back to the source if (false !== strpos($sourceUrl, '://')) { // the source is absolute, this should be easy $parts = parse_url($sourceUrl); $host = $parts['scheme'].'://'.$parts['host']; $path = dirname($parts['path']).'/'; } else { // assume source and target are on the same host $host = ''; // pop entries off the target until it fits in the source $path = ''; $targetDir = dirname($targetUrl); while (0 !== strpos($sourceUrl, $targetDir)) { if (false !== $pos = strrpos($targetDir, '/')) { $targetDir = substr($targetDir, 0, $pos); $path .= '../'; } else { throw new \RuntimeException(sprintf('Unable to calculate relative path from "%s" to "%s"', $targetUrl, $sourceUrl)); } } $path .= substr(dirname($sourceUrl).'/', strlen($targetDir) + 1); } $callback = function($matches) use($host, $path) { if (false !== strpos($matches['url'], '://')) { // absolute return $matches[0]; } if ('/' == $matches['url'][0]) { // root relative return str_replace($matches['url'], $host.$matches['url'], $matches[0]); } // document relative $url = $matches['url']; while (0 === strpos($url, '../') && 2 <= substr_count($path, '/')) { $path = substr($path, 0, strrpos(rtrim($path, '/'), '/') + 1); $url = substr($url, 3); } return str_replace($matches['url'], $host.$path.$url, $matches[0]); }; $content = $asset->getContent(); $content = preg_replace_callback('/url\((["\']?)(?.*)(\\1)\)/', $callback, $content); $content = preg_replace_callback('/import (["\'])(?.*)(\\1)/', $callback, $content); $asset->setContent($content); } }