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. |
Résultat | string | The base path of the glob. |
/** * Creates a new iterator. * * @param string $glob The glob pattern. * @param int $flags A bitwise combination of the flag constants in * {@link Glob}. */ public function __construct($glob, $flags = 0) { $basePath = Glob::getBasePath($glob, $flags); if (!Glob::isDynamic($glob) && file_exists($glob)) { // If the glob is a file path, return that path $innerIterator = new ArrayIterator(array($glob)); } elseif (is_dir($basePath)) { // Use the system's much more efficient glob() function where we can if (false === strpos($glob, '/**/') && false === strpos($glob, '://') && ('\\' !== DIRECTORY_SEPARATOR || false === strpos($glob, '[^'))) { $results = glob($glob, GLOB_BRACE); // $results may be empty or false if $glob is invalid if (empty($results)) { // Parse glob and provoke errors if invalid Glob::toRegEx($glob); // Otherwise return empty result set $innerIterator = new EmptyIterator(); } else { $innerIterator = new ArrayIterator($results); } } else { // Otherwise scan the glob's base directory for matches $innerIterator = new GlobFilterIterator($glob, new RecursiveIteratorIterator(new RecursiveDirectoryIterator($basePath, RecursiveDirectoryIterator::CURRENT_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST), GlobFilterIterator::FILTER_VALUE, $flags); } } else { // If the glob's base directory does not exist, return nothing $innerIterator = new EmptyIterator(); } parent::__construct($innerIterator); }
public function testIterationForGlob() { $glob = '/fixtures/**/*.css'; $basePath = Glob::getBasePath($glob); $it = new RecursiveDirectoryIterator($this->filesystem, $basePath, RecursiveDirectoryIterator::KEY_FOR_GLOB); $it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST); $it = new GlobFilterIterator($glob, $it, GlobFilterIterator::FILTER_KEY | GlobFilterIterator::KEY_AS_KEY); $expected = ['fixtures/base.css', 'fixtures/css/old/old_style.css', 'fixtures/css/reset.css', 'fixtures/css/style.css']; $this->assertIterator($expected, $it); $this->assertIteratorInForeach($expected, $it); }
/** * Creates the installation request. * * @param ResourceInstaller $installer The used resource installer. * @param InstallerDescriptor $installerDescriptor The descriptor of the * resource installer. * @param ResourceCollection $resources The resources to install. * @param AssetMapping $mapping The asset mapping. * @param Server $server The asset server. * @param string $rootDir The project's root directory. */ public function __construct(ResourceInstaller $installer, InstallerDescriptor $installerDescriptor, ResourceCollection $resources, AssetMapping $mapping, Server $server, $rootDir) { $glob = $mapping->getGlob(); $parameterValues = $server->getParameterValues(); $this->validateParameterValues($parameterValues, $installerDescriptor); $this->installer = $installer; $this->installerDescriptor = $installerDescriptor; $this->resources = $resources; $this->mapping = $mapping; $this->server = $server; $this->rootDir = $rootDir; $this->basePath = Glob::isDynamic($glob) ? Glob::getBasePath($glob) : $glob; $this->parameterValues = array_replace($installerDescriptor->getParameterValues(), $parameterValues); }
/** * Creates a new iterator. * * @param string $glob The glob pattern. * @param int $flags A bitwise combination of the flag constants in * {@link Glob}. */ public function __construct($glob, $flags = 0) { $basePath = Glob::getBasePath($glob); if (!Glob::isDynamic($glob) && file_exists($glob)) { // If the glob is a file path, return that path $innerIterator = new ArrayIterator(array($glob)); } elseif (is_dir($basePath)) { // Otherwise scan the glob's base directory for matches $innerIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($basePath, RecursiveDirectoryIterator::CURRENT_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST); } else { // If the glob's base directory does not exist, return nothing $innerIterator = new EmptyIterator(); } parent::__construct($glob, $innerIterator, self::FILTER_VALUE, $flags); }
/** * Constructor. * * @param FilesystemInterface $filesystem The filesystem to search * @param string $glob The glob pattern * @param int $flags A bitwise combination of the flag constants in {@see Glob} */ public function __construct(FilesystemInterface $filesystem, $glob, $flags = 0) { // Glob code requires absolute paths, so prefix path // with leading slash, but not before mount point if (strpos($glob, '://') > 0) { $glob = str_replace('://', ':///', $glob); } else { $glob = '/' . ltrim($glob, '/'); } if (!Glob::isDynamic($glob)) { // If the glob is a file path, return that path. $innerIterator = new \ArrayIterator([$glob => $filesystem->get($glob)]); } else { $basePath = Glob::getBasePath($glob); $innerIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($filesystem, $basePath, RecursiveDirectoryIterator::KEY_FOR_GLOB), RecursiveIteratorIterator::SELF_FIRST); } parent::__construct($glob, $innerIterator, static::FILTER_KEY, $flags); }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage *.css */ public function testGetBasePathFailsIfNotAbsolute() { Glob::getBasePath('*.css'); }
/** * Returns the uris associated with a source file. * * @param string $sourceFile * * @return array */ public function getUris($sourceFile) { $relativePath = substr($sourceFile, strlen(Glob::getBasePath($this->pattern))); return [Path::join(Glob::getBasePath($this->uriPattern), $relativePath)]; }
/** * Constructor. * * {@inheritdoc} */ public function __construct($pattern, $uriPattern = '/**/*') { parent::__construct($pattern, $uriPattern); $this->pipe(Middleware::basePath(Glob::getBasePath($this->uriPattern))); $this->endPoint(Middleware::readResponse(Glob::getBasePath($this->pattern))); }
/** * {@inheritdoc} */ protected function getReferencesForGlob($glob, $flags = 0) { if (!Glob::isDynamic($glob)) { return $this->getReferencesForPath($glob); } return $this->getReferencesForRegex(Glob::getBasePath($glob), Glob::toRegEx($glob), $flags); }