/** * {@inheritdoc} */ public function retrieve($uri) { if (isset($this->mappings[$uri])) { $uri = $this->mappings[$uri]; if (Path::isLocal($uri)) { $uri = 'file://' . ($this->baseDir ? Path::makeAbsolute($uri, $this->baseDir) : $uri); } $this->lastUsedRetriever = $this->filesystemRetriever; return $this->filesystemRetriever->retrieve($uri); } $this->lastUsedRetriever = $this->fallbackRetriever; return $this->fallbackRetriever->retrieve($uri); }
public static function filterReferences($content, $callback) { return CssUtils::filterReferences($content, function ($matches) use($callback) { // The referenced path is a repository path // e.g. "/webmozart/puli/images/bg.png" $referencedPath = $matches['url']; // Ignore empty URLs if ('' === $referencedPath) { return $matches[0]; } // Ignore non-local paths if (!Path::isLocal($referencedPath)) { return $matches[0]; } // Ignore "data:" URLs if (0 === strpos($referencedPath, 'data:')) { return $matches[0]; } // If the referenced path is not absolute, resolve it relative to // the directory of the source file if (!Path::isAbsolute($referencedPath)) { $referencedPath = Path::makeAbsolute($referencedPath, $repoDir); } // The referenced asset must be known if (!array_key_exists($referencedPath, $pathMap)) { throw new AssetException(sprintf('The asset "%s" referenced in "%s" could not be found.', $matches['url'], $repoPath)); } // The target path of the referenced file must be set if (!$pathMap[$referencedPath]) { throw new AssetException(sprintf('The referenced path "%s" in "%s" cannot be resolved, because ' . 'the target path of "%s" is not set.', $matches['url'], $repoPath, $matches['url'])); } // The target path of the source file must be set if (!$targetPath) { throw new AssetException(sprintf('The referenced path "%s" in "%s" cannot be resolved, because ' . 'the target path of "%s" is not set.', $matches['url'], $repoPath, $repoPath)); } // Get the relative path from the source directory to the reference // e.g. "/css/style.css" + "/images/bg.png" = "../images/bg.png" $relativePath = Path::makeRelative($pathMap[$referencedPath], $targetDir); return str_replace($matches['url'], $relativePath, $matches[0]); }); }
/** * Filters an asset just before it's dumped. * * @param AssetInterface $asset An asset */ public function filterDump(AssetInterface $asset) { if (!$asset instanceof PuliAsset) { return; } $pathMap = array(); // Get a map of repository paths to target paths // e.g. "/webmozart/puli/images/bg.png" => "/images/bg.png" foreach ($this->am->getNames() as $name) { $this->extractTargetPaths($this->am->get($name), $pathMap); } // Remember the repository dir of the current resource $repoPath = $asset->getSourcePath(); $repoDir = Path::getDirectory($repoPath); // Get the target directory of the current resource // e.g. "css" $targetPath = $asset->getTargetPath(); $targetDir = Path::getDirectory($targetPath); // Convert to an absolute path so that we can create a proper // relative path later on // e.g. "/css" if (!Path::isAbsolute($targetDir)) { $targetDir = '/' . $targetDir; } $content = CssUtils::filterReferences($asset->getContent(), function ($matches) use($pathMap, $repoDir, $repoPath, $targetDir, $targetPath) { // The referenced path is a repository path // e.g. "/webmozart/puli/images/bg.png" $referencedPath = $matches['url']; // Ignore empty URLs if ('' === $referencedPath) { return $matches[0]; } // Ignore non-local paths if (!Path::isLocal($referencedPath)) { return $matches[0]; } // Ignore "data:" URLs if (0 === strpos($referencedPath, 'data:')) { return $matches[0]; } // If the referenced path is not absolute, resolve it relative to // the directory of the source file if (!Path::isAbsolute($referencedPath)) { $referencedPath = Path::makeAbsolute($referencedPath, $repoDir); } // The referenced asset must be known if (!array_key_exists($referencedPath, $pathMap)) { throw new AssetException(sprintf('The asset "%s" referenced in "%s" could not be found.', $referencedPath, $repoPath)); } // The target path of the referenced file must be set if (!$pathMap[$referencedPath]) { throw new AssetException(sprintf('The referenced path "%s" in "%s" cannot be resolved, because ' . 'the target path of "%s" is not set.', $matches['url'], $repoPath, $matches['url'])); } // The target path of the source file must be set if (!$targetPath) { throw new AssetException(sprintf('The referenced path "%s" in "%s" cannot be resolved, because ' . 'the target path of "%s" is not set.', $matches['url'], $repoPath, $repoPath)); } // Get the relative path from the source directory to the reference // e.g. "/css/style.css" + "/images/bg.png" = "../images/bg.png" $relativePath = Path::makeRelative($pathMap[$referencedPath], $targetDir); return str_replace($matches['url'], $relativePath, $matches[0]); }); $asset->setContent($content); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The path must be a string. Got: array */ public function testIsLocalFailsIfInvalidPath() { Path::isLocal(array()); }