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.
Esempio n. 1
0
 /**
  * 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);
 }
Esempio n. 2
0
 /**
  * Creates a new iterator.
  *
  * @param string   $glob          The canonical glob.
  * @param Iterator $innerIterator The filtered iterator.
  * @param int      $mode          A bitwise combination of the mode constants.
  * @param int      $flags         A bitwise combination of the flag constants
  *                                in {@link Glob}.
  */
 public function __construct($glob, Iterator $innerIterator, $mode = self::FILTER_VALUE, $flags = 0)
 {
     parent::__construct(Glob::toRegEx($glob, $flags), Glob::getStaticPrefix($glob, $flags), $innerIterator, $mode);
 }
Esempio n. 3
0
 /**
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage *.css
  */
 public function testToRegexFailsIfNotAbsolute()
 {
     Glob::toRegEx('*.css');
 }
 /**
  * {@inheritdoc}
  */
 protected function getReferencesForGlob($glob, $flags = 0)
 {
     if (!Glob::isDynamic($glob)) {
         return $this->getReferencesForPath($glob);
     }
     return $this->getReferencesForRegex(Glob::getStaticPrefix($glob), Glob::toRegEx($glob), $flags);
 }
Esempio n. 5
0
 /**
  * @param array $previous_files
  *
  * @return \Iterator
  */
 public function do_files(array $previous_files)
 {
     $this->log(__('Comparing files...', 'my-wp-backup'), 'debug');
     $excludes = array();
     foreach ($this['file_filters'] as $exclude) {
         $exclude = Path::makeAbsolute($exclude, MyWPBackup::$info['root_dir']);
         array_push($excludes, Glob::toRegEx($exclude));
     }
     $filtered =& $this->files['filtered'];
     $unchanged =& $this->files['unchanged'];
     $overwritten =& $this->files['overwritten'];
     $exclude_uploads = '1' !== $this['backup_uploads'];
     $wp_upload_dir = wp_upload_dir();
     $uploads_dir = $wp_upload_dir['basedir'];
     /**
      * @param \SplFileInfo $file
      * @return bool True if you need to recurse or if the item is acceptable
      */
     $filter = function ($file) use($excludes, $uploads_dir, $exclude_uploads, $previous_files, &$filtered, &$unchanged, &$overwritten) {
         $filePath = $file->getPathname();
         $relativePath = substr($filePath, strlen(MyWPBackup::$info['root_dir']));
         if ('.my-wp-backup' === $relativePath) {
             return false;
         }
         // Exclude backup directory.
         if (false !== strpos($filePath, MyWPBackup::$info['backup_dir'])) {
             $filtered[$relativePath] = true;
             return false;
         }
         if ($exclude_uploads && false !== strpos($filePath, $uploads_dir)) {
             $filtered[$relativePath] = true;
             return false;
         }
         foreach ($excludes as $exclude) {
             if (preg_match($exclude, $filePath)) {
                 $filtered[$relativePath] = true;
                 return false;
             }
         }
         if (isset($previous_files[$relativePath])) {
             if (hash_file('crc32b', $file) === $previous_files[$relativePath]) {
                 $unchanged[$relativePath] = true;
                 return false;
             } else {
                 $overwritten[$relativePath] = true;
                 return true;
             }
         }
         return true;
     };
     if ('1' === $this['backup_files']) {
         $base_iterator = new \RecursiveDirectoryIterator(MyWPBackup::$info['root_dir'], \RecursiveDirectoryIterator::SKIP_DOTS);
     } else {
         $base_iterator = new RecursiveArrayOnlyIterator(array(MyWPBackup::$info['root_dir'] . Database\ExportFile::FILENAME => new \SplFileInfo(MyWPBackup::$info['root_dir'] . Database\ExportFile::FILENAME)));
     }
     $this->files['iterator'] = new \RecursiveIteratorIterator(new RecursiveCallbackFilterIterator($base_iterator, $filter));
     $this->log(__('Ok.', 'my-wp-backup'), 'debug');
     return $this->files;
 }