Returns whether a path is absolute.
public static isAbsolute ( string $path ) : boolean | ||
$path | string | A path string. |
return | boolean | Returns true if the path is absolute, false if it is relative or empty. |
/** * {@inheritdoc} * * @throws \InvalidArgumentException * @throws \RuntimeException * @throws \LogicException * @throws BadMethodCallException */ public function load($resource, $type = null) { $path = $this->locator->locate($resource); $this->container->addResource(new FileResource($path)); $file = new JsonFile($path); $content = $file->read(); $extension = pathinfo($resource, PATHINFO_FILENAME); if (array_key_exists('parameters', $content)) { foreach ($content['parameters'] as $name => $parameter) { $this->container->setParameter($name, $parameter); } unset($content['parameters']); } if (array_key_exists('imports', $content)) { foreach ($content['imports'] as $import) { $importFilename = $import; if (!Path::isAbsolute($importFilename)) { $importFilename = Path::join([dirname($path), $import]); } $this->import($importFilename, null, false, $file); } unset($content['imports']); } $this->container->loadFromExtension($extension, $content); }
/** * Constructor. * * @param string $pattern * @param string $uriPattern */ public function __construct($pattern, $uriPattern = '/**/*') { if (!Path::isAbsolute($pattern)) { $pattern = Path::join(getcwd(), $pattern); } $this->pattern = $pattern; $this->uriPattern = $uriPattern; }
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]); }); }
private function encodeFile($jsonData, $path) { if (!is_string($path) || !Path::isAbsolute($path)) { throw new IOException(sprintf('Cannot write "%s": Expected an absolute path.', $path)); } if (is_dir($path)) { throw new IOException(sprintf('Cannot write %s: Is a directory.', $path)); } $encoder = new JsonEncoder(); $encoder->setPrettyPrinting(true); $encoder->setEscapeSlash(false); $encoder->setTerminateWithLineFeed(true); $decoder = new JsonDecoder(); // We can't use realpath(), which doesn't work inside PHARs. // However, we want to display nice paths if the file is not found. $schema = $decoder->decodeFile(Path::canonicalize(__DIR__ . '/../../res/schema/package-schema-1.0.json')); $configSchema = $schema->properties->config; if (!is_dir($dir = Path::getDirectory($path))) { $filesystem = new Filesystem(); $filesystem->mkdir($dir); } $encoder->encodeFile($jsonData, $path, $configSchema); }
private function extractTargetPaths(AssetInterface $asset, &$array) { if ($asset instanceof PuliAsset) { $targetPath = $asset->getTargetPath(); // All relative paths are treated like absolute paths // Don't change empty paths so that we can throw an exception // later if ($targetPath && !Path::isAbsolute($targetPath)) { $targetPath = '/' . $targetPath; } $array[$asset->getSourcePath()] = $targetPath; } elseif ($asset instanceof AssetCollection) { foreach ($asset as $entry) { $this->extractTargetPaths($entry, $array); } } }
public static function absoluteSystemPath($value) { self::stringNotEmpty($value, 'The path must be a non-empty string. Got: %s'); self::true(Path::isAbsolute($value), sprintf('The path %s is not absolute.', $value)); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The path must be a string. Got: array */ public function testIsAbsoluteFailsIfInvalidPath() { Path::isAbsolute(array()); }
/** * Check if target is linked and links to the given source. * * @return bool */ public function targetLinksToSource() { if (!$this->targetIsLink()) { return FALSE; } $current_source = readlink($this->link); if (!Path::isAbsolute($current_source)) { $current_source = Path::makeAbsolute($current_source, dirname($this->link)); } return $current_source == $this->source; }
/** * Returns the static prefix of a glob. * * The "static prefix" is the part of the glob up to the first wildcard "*". * If the glob does not contain wildcards, the full glob is returned. * * @param string $glob The canonical glob. The glob should contain forward * slashes as directory separators only. It must not * contain any "." or ".." segments. Use the * "webmozart/path-util" utility to canonicalize globs * prior to calling this method. * @param int $flags A bitwise combination of the flag constants in this * class. * * @return string The static prefix of the glob. */ public static function getStaticPrefix($glob, $flags = 0) { if (!Path::isAbsolute($glob) && false === strpos($glob, '://')) { throw new InvalidArgumentException(sprintf('The glob "%s" is not absolute and not a URI.', $glob)); } $prefix = $glob; if ($flags & self::ESCAPE) { // Read backslashes together with the next (the escaped) character // up to the first non-escaped star/brace if (preg_match('~^(' . Symbol::BACKSLASH . '.|[^' . Symbol::BACKSLASH . Symbol::STAR . Symbol::L_BRACE . '])*~', $glob, $matches)) { $prefix = $matches[0]; } // Replace escaped characters by their unescaped equivalents $prefix = str_replace(array('\\\\', '\\*', '\\{', '\\}'), array('\\', '*', '{', '}'), $prefix); } else { $pos1 = strpos($glob, '*'); $pos2 = strpos($glob, '{'); if (false !== $pos1 && false !== $pos2) { $prefix = substr($glob, 0, min($pos1, $pos2)); } elseif (false !== $pos1) { $prefix = substr($glob, 0, $pos1); } elseif (false !== $pos2) { $prefix = substr($glob, 0, $pos2); } } return $prefix; }
/** * @param string $basePath * @param string $path * * @return string */ private function canonicalize($basePath, $path) { return Path::canonicalize(Path::isAbsolute($path) ? $path : sprintf('%s/%s', $basePath, $path)); }
/** * Returns the static prefix of a glob. * * The "static prefix" is the part of the glob up to the first wildcard "*". * If the glob does not contain wildcards, the full glob is returned. * * @param string $glob The canonical glob. The glob should contain forward * slashes as directory separators only. It must not * contain any "." or ".." segments. Use the * "webmozart/path-util" utility to canonicalize globs * prior to calling this method. * @param int $flags A bitwise combination of the flag constants in this * class. * * @return string The static prefix of the glob. */ public static function getStaticPrefix($glob, $flags = 0) { if (!Path::isAbsolute($glob) && false === strpos($glob, '://')) { throw new InvalidArgumentException(sprintf('The glob "%s" is not absolute and not a URI.', $glob)); } $prefix = ''; $length = strlen($glob); for ($i = 0; $i < $length; ++$i) { $c = $glob[$i]; switch ($c) { case '/': $prefix .= '/'; if (isset($glob[$i + 3]) && '**/' === $glob[$i + 1] . $glob[$i + 2] . $glob[$i + 3]) { break 2; } break; case '*': case '?': case '{': case '[': break 2; case '\\': if (isset($glob[$i + 1])) { switch ($glob[$i + 1]) { case '*': case '?': case '{': case '[': case '\\': $prefix .= $glob[$i + 1]; ++$i; break; default: $prefix .= '\\'; } } else { $prefix .= '\\'; } break; default: $prefix .= $c; break; } } return $prefix; }
/** * Converts an input to an asset using the given variable values. * * An "input" in Assetic's terminology is a reference string to an asset, * such as "css/*.css", "/webmozart/puli/style.css", * "@AcmeDemoBundle/Resources/css/style.css" etc. * * Contrary to {@link parseInput()}, this method converts an input to an * asset that contains variables and whose variable values are already * known. For example, if the input is "js/messages.{locale}.js" with the * variable "locale" and its value "en", an asset is created for the input * "js/messages.en.js" using the resolution logic described in * {@link PuliAssetFactory}. * * @param string $input An input string containing variables. * @param string|null $currentDir The Puli directory of the currently loaded * Twig template. This is `null` if the * template was not loaded through Puli. * @param string[] $roots The file system root directories to search * for relative inputs. * @param string[] $vars The variables that may occur in the input. * @param string[] $values A mapping of variable names to values. * * @return AssetInterface The created asset. * * @see parseInput() */ public function parseInputWithFixedValues($input, $currentDir, array $roots = array(), array $vars = array(), array $values = array()) { if (Path::isAbsolute($input)) { return $this->parseAbsoluteInput($input, $roots, $vars, $values); } return $this->parseRelativeInput($input, $currentDir, $roots, $vars, $values); }