Compiles a route string to a regular expression

@param string $route     The route string to compile
@return string
5046
protected function compileRoute($route)
    {
        // First escape all of the non-named param (non [block]s) for regex-chars
        $route = preg_replace_callback(
            static::ROUTE_ESCAPE_REGEX,
            function ($match) {
                return preg_quote($match[0]);
            },
            $route
        );

        // Get a local reference of the match types to pass into our closure
        $match_types = $this->match_types;

        // Now let's actually compile the path
        $route = preg_replace_callback(
            static::ROUTE_COMPILE_REGEX,
            function ($match) use ($match_types) {
                list(, $pre, $type, $param, $optional) = $match;

                if (isset($match_types[$type])) {
                    $type = $match_types[$type];
                }

                // Older versions of PCRE require the 'P' in (?P<named>)
                $pattern = '(?:'
                         . ($pre !== '' ? $pre : null)
                         . '('
                         . ($param !== '' ? "?P<$param>" : null)
                         . $type
                         . '))'
                         . ($optional !== '' ? '?' : null);

                return $pattern;
            },
            $route
        );

        $regex = "`^$route$`";

        // Check if our regular expression is valid
        $this->validateRegularExpression($regex);

        return $regex;
    }
150672
public function route($route, $prefix = null)
    {
        $this->route = substr($route, strlen($prefix));
        $parameters = [];

        // Go through predefined routes till a match is found
        foreach ($this->routeOrder as $routeName) {
            $routeDescription = $this->routes[$routeName];
            $parameters = $this->match($this->route, $routeDescription);
            if ($parameters !== false) {
                return [
                    'route' => $this->route,
                    'parameters' => $this->fillInDefaultParameters($routeDescription, $parameters),
                    'description' => $routeDescription
                ];
            }
        }
        return [
            'route' => $this->route,
            'parameters' => $parameters,
            'description' => $this->routes['default']
        ];
    }
1277784
public function valid($route)
    {
        //argument 1 must be a string
        Argument::i()->test(1, 'string');

        return isset($this->route[strtolower($route)]);
    }
1342409
public static function transRoute($route) {
        $app = app();
        $locale = \Sirgrimorum\CrudGenerator\CrudGenerator::setLocale();
        //$locale = $app->getLocale();
        $routes = explode(".", $route);
        $base = "";
        if (count($routes) > 1) {
            $base = \Sirgrimorum\CrudGenerator\CrudGenerator::transRouteModel($routes[0]);
            if ($base != "") {
                $base .= ".";
            }
            $route = $routes[1];
        }
        $transroute = $app->translator->trans('routes.actions.' . $route, [], $locale);
        if (stripos($transroute, ".") !== false) {
            $transroute = $app->translator->trans('crudgenerator::admin.routes.' . $route, [], $locale);
            if (stripos($transroute, ".") !== false) {
                $transroute = $route;
            }
        }
        //$route = $app->translator->trans('crudgenerator::admin.routes.' . $route);
        return $base . $transroute;
    }
1188241
private function mapRoute2Content($route)
    {
        // Look it up in the index
        list($keyIndex, $content) = $this->mapRoute2Index($route);
        $filtered = $this->loadFileContentPhaseOne($keyIndex);

        return [$keyIndex, $content, $filtered];
    }
11138
private function constructInternalRegularExpression($path, $routePattern)
    {
        if (empty($routePattern)) return false;

        $fullRoute = $path;

        if (substr($fullRoute, -1) == '/') {
            $fullRoute = substr($fullRoute, 0, -1);
        }

        $fullRoute .= $routePattern;

        $patternSegments = $this->urlSegmentsFromPath($fullRoute);

        $variableNumber = 0;
        $hasOptional = false;
        $lastSegmentRequired = true;

        foreach ($patternSegments as $pattern) {
            $variableNumber++;

            $isOptionalPattern = $this->stringIsWrapped($pattern, '[', ']');
            $isDynamicPattern = $this->stringIsWrapped($pattern, '{', '}');

            if (!$isDynamicPattern) {
                $validatorClass = new \Slab\Router\Validators\Value(); //@todo findClass

                $validatorClass->setChallenge($pattern);

                if ($isOptionalPattern)
                {
                    // Can't have optional value validators
                    //$validatorClass->setOptional(true);
                }
                else
                {
                    $this->requiredSegments++;
                }

                $this->validations[] = $validatorClass;

                continue;
            }

            if (!$isOptionalPattern && $hasOptional)
            {
                throw new \Exception("Route " . $path . " has required fields after optional fields.");
            }

            list($validatorClass, $variableName) = explode(':', $pattern);

            if ($validatorClass[0] != '\\')
            {
                $validatorClass = ucfirst($validatorClass);

                if (strcasecmp($validatorClass, 'String') == 0)
                {
                    $validatorClass = 'Text';
                }

                $validatorClassName = '\Slab\Router\Validators\\' . $validatorClass;
            }
            else
            {
                $validatorClassName = $validatorClass;
            }

            if (!class_exists($validatorClassName))
            {
                throw new \Exception("Route " . $path . " specified invalid validator class " . $validatorClass);
            }


            $validatorClass = new $validatorClassName;

            if (!$validatorClass instanceof \Slab\Router\Validators\ValidatorInterface)
            {
                throw new \Exception("Validator " . $validatorClass . " does not explicitly implement interface \Slab\Router\Validators\ValidatorInterface");
            }

            $this->validations[] = $validatorClass;

            $this->variableMapping[$variableNumber] = $variableName;

            if (!$isOptionalPattern)
            {
                ++$this->requiredSegments;
            }
        }

        return true;
    }
1108522
protected function parseRoute($route)
    {
        $parsedRoute = parse_url($route);

        if (isset($parsedRoute['query'])) {
            parse_str($parsedRoute['query'], $parsedRoute['params']);
        } else {
            $parsedRoute['params'] = [];
        }

        $resolvedRoute = new Resolver(new \common_http_Request($route));
        $this->propagate($resolvedRoute);
        $parsedRoute['extension']         = $resolvedRoute->getExtensionId();
        $parsedRoute['controller']        = $resolvedRoute->getControllerShortName();
        $parsedRoute['controller_class']  = $resolvedRoute->getControllerClass();
        $parsedRoute['action']            = $resolvedRoute->getMethodName();

        return $parsedRoute;
    }
1364623
public static function transRouteExternal($route) {
        $app = app();
        $locale = \Sirgrimorum\CrudGenerator\CrudGenerator::setLocale();
        //$locale = $app->getLocale();
        $transroute = $app->translator->trans($route, [], $locale);
        return $transroute;
    }
520855
private function createRegexp($uri, Route $route)
    {
        $patterns = [];
        foreach ($route->getParameterPatterns() as $key => $pattern) {
            $patterns['\\{' . $key . '\\}'] = '(' . $pattern . ')';
        }
        $uri = preg_quote($uri, '#');

        return strtr($uri, $patterns);
    }
863256
private function getParsingMode(string &$route) : int
    {
        // Explicit regex mode
        if (strpos($route, 'R>') === 0) {
            $route = str_replace('R>', '', $route);
            return self::MODE_REGEX;
        }
        // Explicit string mode
        if (strpos($route, 'S>') === 0) {
            $route = str_replace('S>', '', $route);
            return self::MODE_STRING;
        }

        // Use default mode
        return self::$mode;
    }
629514
function(route) {
      route = route.replace(escapeRegExp, '\\$&')
                   .replace(optionalParam, '(?:$1)?')
                   .replace(namedParam, '([^\/]+)')
                   .replace(splatParam, '(.*?)');
      return new RegExp('^' + route + '$');
    }
68167
public function get($route = null)
    {
        //argument 1 must be a string or null
        Argument::i()->test(1, 'string', 'null');
        
        //if route is null
        if (is_null($route)) {
            //return all routes
            return $this->route;
        }
        
        //if valid route
        if ($this->valid($route)) {
            //return route
            return $this->route[strtolower($route)];
        }
        
        //at this point it is not a route
        //return the same thing
        return $route;
    }
970717
protected static function dynamic($route)
    {
        $parms = [];
        $param = '';
        $open = false;
        //
        for ($i = 0; $i < Strings::length($route); $i++) {
            if ($open && $route[$i] != '}') {
                $param .= $route[$i];
            }
            //
            if ($route[$i] == '{' && !$open) {
                $open = true;
            } //
            elseif ($route[$i] == '}' && $open) {
                $open = !true;
                $parms[] = $param;
                $param = '';
            }
        }
        //
        return $parms;
    }
1130551
public static function parseRoute($route)
    {
        if (!empty($route)) {
            $url = [];
            $r = explode('&', $route);
            $url[0] = $r[0];
            unset($r[0]);
            foreach ($r as $part) {
                $part = explode('=', $part);
                $url[$part[0]] = isset($part[1]) ? $part[1] : '';
            }

            return $url;
        }

        return '#';
    }
853197
private function getRouteName(Route $route)
    {
        return is_null($name = $route->getName()) ? $route->uri() : $name;
    }
861046
private function getMethods(Route $route)
    {
        $methods = $route->getMethods();
        sort($methods);

        return implode('|', $methods) ?: 'ANY';
    }
532984
function routePath (route) {
  if (!route) return ''
  return route.path || (route.regexp && route.regexp.source) || ''
}