Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Glob.php

function Glob::toRegex

Returns a regexp which is the equivalent of the glob pattern.

2 calls to Glob::toRegex()
FilenameFilterIterator::toRegex in vendor/symfony/finder/Iterator/FilenameFilterIterator.php
Converts glob to regexp.
GenerateTheme::loadStarterKitConfig in core/lib/Drupal/Core/Command/GenerateTheme.php

File

vendor/symfony/finder/Glob.php, line 41

Class

Glob
Glob matches globbing patterns against text.

Namespace

Symfony\Component\Finder

Code

public static function toRegex(string $glob, bool $strictLeadingDot = true, bool $strictWildcardSlash = true, string $delimiter = '#') : string {
    $firstByte = true;
    $escaping = false;
    $inCurlies = 0;
    $regex = '';
    $sizeGlob = \strlen($glob);
    for ($i = 0; $i < $sizeGlob; ++$i) {
        $car = $glob[$i];
        if ($firstByte && $strictLeadingDot && '.' !== $car) {
            $regex .= '(?=[^\\.])';
        }
        $firstByte = '/' === $car;
        if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1] . $glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
            $car = '[^/]++/';
            if (!isset($glob[$i + 3])) {
                $car .= '?';
            }
            if ($strictLeadingDot) {
                $car = '(?=[^\\.])' . $car;
            }
            $car = '/(?:' . $car . ')*';
            $i += 2 + isset($glob[$i + 3]);
            if ('/' === $delimiter) {
                $car = str_replace('/', '\\/', $car);
            }
        }
        if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
            $regex .= "\\{$car}";
        }
        elseif ('*' === $car) {
            $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
        }
        elseif ('?' === $car) {
            $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
        }
        elseif ('{' === $car) {
            $regex .= $escaping ? '\\{' : '(';
            if (!$escaping) {
                ++$inCurlies;
            }
        }
        elseif ('}' === $car && $inCurlies) {
            $regex .= $escaping ? '}' : ')';
            if (!$escaping) {
                --$inCurlies;
            }
        }
        elseif (',' === $car && $inCurlies) {
            $regex .= $escaping ? ',' : '|';
        }
        elseif ('\\' === $car) {
            if ($escaping) {
                $regex .= '\\\\';
                $escaping = false;
            }
            else {
                $escaping = true;
            }
            continue;
        }
        else {
            $regex .= $car;
        }
        $escaping = false;
    }
    return $delimiter . '^' . $regex . '$' . $delimiter;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal