PHP Class Webmozart\Glob\Glob

This class implements an Ant-like version of PHP's glob() function. The wildcard "*" matches any number of characters except directory separators. The double wildcard "**" matches any number of characters, including directory separators. Use {@link glob()} to glob the filesystem for paths: php foreach (Glob::glob('/project/**.twig') as $path) { do something... } Use {@link match()} to match a file path against a glob: php if (Glob::match('/project/views/index.html.twig', '/project/**.twig')) { path matches } You can also filter an array of paths for all paths that match your glob with {@link filter()}: php $filteredPaths = Glob::filter($paths, '/project/**.twig'); Internally, the methods described above convert the glob into a regular expression that is then matched against the matched paths. If you need to match many paths against the same glob, you should convert the glob manually and use {@link preg_match()} to test the paths: php $staticPrefix = Glob::getStaticPrefix('/project/**.twig'); $regEx = Glob::toRegEx('/project/**.twig'); if (0 !== strpos($path, $staticPrefix)) { no match } if (!preg_match($regEx, $path)) { no match } The method {@link getStaticPrefix()} returns the part of the glob up to the first wildcard "*". You should always test whether a path has this prefix before calling the much more expensive {@link preg_match()}.
Since: 1.0
Author: Bernhard Schussek ([email protected])
Show file Open project: webmozart/glob Class Usage Examples

Public Methods

Method Description
filter ( array $paths, string $glob, integer $flags = self::FILTER_VALUE ) : string[] Filters an array for paths matching a glob.
getBasePath ( string $glob, integer $flags ) : string Returns the base path of a glob.
getStaticPrefix ( string $glob, integer $flags ) : string Returns the static prefix of a glob.
glob ( string $glob, integer $flags ) : string[] Globs the file system paths matching the glob.
isDynamic ( string $glob ) : boolean Returns whether the glob contains a dynamic part.
match ( string $path, string $glob, integer $flags ) : boolean Matches a path against a glob.
toRegEx ( string $glob, integer $flags, $delimiter = '~' ) : string Converts a glob to a regular expression.

Private Methods

Method Description
__construct ( )

Method Details

filter() public static method

The filtered array is returned. This array preserves the keys of the passed array. php $filteredPaths = Glob::filter($paths, '/project/**.twig');
public static filter ( array $paths, string $glob, integer $flags = self::FILTER_VALUE ) : string[]
$paths array A list of paths.
$glob string 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.
$flags integer A bitwise combination of the flag constants in this class.
return string[] The paths matching the glob indexed by their original keys.

getBasePath() public static method

This method returns the most specific directory that contains all files matched by the glob. If this directory does not exist on the file system, it's not necessary to execute the glob algorithm. More specifically, the "base path" is the longest path trailed by a "/" on the left of the first wildcard "*". If the glob does not contain wildcards, the directory name of the glob is returned. php Glob::getBasePath('/css/*.css'); => /css Glob::getBasePath('/css/style.css'); => /css Glob::getBasePath('/css/st*.css'); => /css Glob::getBasePath('/*.css'); => /
public static getBasePath ( string $glob, integer $flags ) : string
$glob string 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.
$flags integer A bitwise combination of the flag constants in this class.
return string The base path of the glob.

getStaticPrefix() public static method

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.
public static getStaticPrefix ( string $glob, integer $flags ) : string
$glob string 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.
$flags integer A bitwise combination of the flag constants in this class.
return string The static prefix of the glob.

glob() public static method

The glob may contain the wildcard "*". This wildcard matches any number of characters, *including* directory separators. php foreach (Glob::glob('/project/**.twig') as $path) { do something... }
public static glob ( string $glob, integer $flags ) : string[]
$glob string 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.
$flags integer A bitwise combination of the flag constants in this class.
return string[] The matching paths. The keys of the array are incrementing integers.

isDynamic() public static method

The glob contains a dynamic part if it contains an unescaped "*" or "{" character.
public static isDynamic ( string $glob ) : boolean
$glob string The glob to test.
return boolean Returns `true` if the glob contains a dynamic part and `false` otherwise.

match() public static method

php if (Glob::match('/project/views/index.html.twig', '/project/**.twig')) { path matches }
public static match ( string $path, string $glob, integer $flags ) : boolean
$path string The path to match.
$glob string 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.
$flags integer A bitwise combination of the flag constants in this class.
return boolean Returns `true` if the path is matched by the glob.

toRegEx() public static method

Use this method if you need to match many paths against a glob: php $staticPrefix = Glob::getStaticPrefix('/project/**.twig'); $regEx = Glob::toRegEx('/project/**.twig'); if (0 !== strpos($path, $staticPrefix)) { no match } if (!preg_match($regEx, $path)) { no match } You should always test whether a path contains the static prefix of the glob returned by {@link getStaticPrefix()} to reduce the number of calls to the expensive {@link preg_match()}.
public static toRegEx ( string $glob, integer $flags, $delimiter = '~' ) : string
$glob string 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.
$flags integer A bitwise combination of the flag constants in this class.
return string The regular expression for matching the glob.