Compiles the route.

@return CompiledRoute A CompiledRoute instance

@throws \LogicException If the Route cannot be compiled because the
path or host pattern is invalid

@see RouteCompiler which is responsible for the compilation process
512784
public function compile()
    {
        if (null !== $this->compiled) {
            return $this->compiled;
        }

        $class = $this->getOption('compiler_class');

        return $this->compiled = $class::compile($this);
    }
551322
public function compileRegex()
    {
        $route = $this->route->getUri();

        if (strpos($route, '?')) {
            $route = preg_replace('@\/{([\w]+)\?}@', '(?:/{$1})?', $route);
        }

        $route = preg_replace('/{([a-z0-9_-]+)}/i', '(?P<$1>[^/]+)', $route);

        if(substr($route, -1) === '/') {
            $route .= '?';
        }

        return "%^{$route}$%s";
    }
1348607
public function baseRoute()
    {
        if ($this->baseRoute === null) {
            $this->baseRoute = $this->baseUrl();
        }

        $link   = $this->baseRoute;
        $locale = $this->lang();
        if ($locale !== null) {
            $translator = $this->translator();
            $origLocale = $translator->getLocale();
            $translator->setLocale($this->lang());
            $link = $this->renderTemplate((string)$link);
            $translator->setLocale($origLocale);
        }

        return rtrim((string)$link, '/').'/';
    }
251047
public static function generate(
        $compiledRouterPath,
        callable $routeDefinitionsCallback,
        callable $routeCollectionFactory = null,
        callable $routerCompilerFactory = null,
        $forceCompile = false
    ) {
        if (!$forceCompile) {
            $compiledRouter = @include $compiledRouterPath;

            if ($compiledRouter) {
                return $compiledRouter;
            }
        }

        /** @var RouteCollection $routes */
        $routes = $routeCollectionFactory ? $routeCollectionFactory() : new RouteCollection(new RouteParser());

        /** @var RouterCompiler $routeParser */
        $routerCompiler = $routerCompilerFactory ? $routerCompilerFactory() : new TreeBasedRouterCompiler();

        $routeDefinitionsCallback($routes);

        file_put_contents($compiledRouterPath, $routerCompiler->compileRouter($routes));

        return require $compiledRouterPath;
    }
702109
public function compileTargetPath()
    {
        $targetPath = str_replace(':www_root/', WWW_ROOT, $this->target_path);
        $targetPath = preg_replace('/\\\/', '/', $targetPath);

        return $targetPath;
    }
764381
public function build(): CompileScriptRequest
	{
		$instance = new CompileScriptRequest();
		if ($this->expression === null) {
			throw new BuilderException('Property [expression] is required.');
		}
		$instance->expression = $this->expression;
		if ($this->sourceURL === null) {
			throw new BuilderException('Property [sourceURL] is required.');
		}
		$instance->sourceURL = $this->sourceURL;
		if ($this->persistScript === null) {
			throw new BuilderException('Property [persistScript] is required.');
		}
		$instance->persistScript = $this->persistScript;
		$instance->executionContextId = $this->executionContextId;
		return $instance;
	}
835911
public function getRoute()
    {
        foreach ($this->routes as $route) {

            // Is the route array valid
            if (false == ($cleansed = $this->isValidRoute($route))) {
                continue;
            }

            $route = $cleansed;

            // Does request method match
            if (!$this->methodMatches($route)) {
                continue;
            }

            if (false === $this->isAllowedByIp($route)) {
                continue;
            }

            if (false === $this->isAllowedByRole($route)) {
                // @codeCoverageIgnoreStart
                continue;
                // @codeCoverageIgnoreEnd
            }

            if (false != ($match = $this->isDirectMatch($route))) {
                return $match;
            }

            if (false != ($match = $this->isParameterisedMatch($route))) {
                return $match;
            }
        }

        $e = new RouteNotFoundException('Route for path not found.');
        $e->setAdditionalData('Path: ', $this->path);
        throw $e;
    }
227495
protected function requestHandler(): RequestHandlerInterface
    {
        if (!$this->hasContainer()) {
            throw new RouteException("Unable to configure route pipeline without associated container.");
        }

        if ($this->target instanceof TargetInterface) {
            try {
                return $this->target->getHandler($this->container, $this->matches);
            } catch (TargetException $e) {
                throw new RouteException("Invalid target resolution", $e->getCode(), $e);
            }
        }

        if ($this->target instanceof RequestHandlerInterface) {
            return $this->target;
        }

        try {
            if (is_object($this->target) || is_array($this->target)) {
                $target = $this->target;
            } else {
                $target = $this->container->get($this->target);
            }

            if ($target instanceof RequestHandlerInterface) {
                return $target;
            }

            return new CallableHandler(
                $target,
                $this->container->get(ResponseFactoryInterface::class)
            );
        } catch (ContainerExceptionInterface $e) {
            throw new RouteException($e->getMessage(), $e->getCode(), $e);
        }
    }
801339
public function getCreateRoute()
    {
        if (null === $this->createRoute) {
            $route = rtrim($this->getBaseRoute(), '/') . '/' . $this->getCreateSuffix();
            $this->setCreateRoute($route);
        }

        return $this->createRoute;
    }
873761
public function prepareForSerialization()
    {
        if ($this->action['uses'] instanceof Closure) {
            throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
        }

        $this->compileRoute();

        unset($this->router, $this->container);
    }
1233479
public function getRouteMatch()
    {
        if (null === $this->routeMatch) {
            /** @noinspection PhpUndefinedMethodInspection */
            $this->routeMatch = $this->getEvent()->getRouteMatch();
        }

        return $this->routeMatch;
    }
944558
public function beforeCompile(): void
	{
		$builder = $this->getContainerBuilder();

		if ($builder->getByType(Application::class) === null) {
			throw new LogicException(sprintf('Service of type "%s" is needed. Please register it.', Application::class));
		}

		if ($builder->getByType(EventDispatcherInterface::class) === null) {
			throw new LogicException(sprintf('Service of type "%s" is needed. Please register it.', EventDispatcherInterface::class));
		}

		$dispatcher = $builder->getDefinition($builder->getByType(EventDispatcherInterface::class));

		$application = $builder->getDefinition($builder->getByType(Application::class));

		$application->addSetup('?->onStartup[] = function() {?->dispatch(?, new ?(...func_get_args()));}', [
			'@self',
			$dispatcher,
			ApplicationEvents::ON_STARTUP,
			new PhpLiteral(StartupEvent::class),
		]);

		$application->addSetup('?->onError[] = function() {?->dispatch(?, new ?(...func_get_args()));}', [
			'@self',
			$dispatcher,
			ApplicationEvents::ON_ERROR,
			new PhpLiteral(ErrorEvent::class),
		]);

		$application->addSetup('?->onPresenter[] = function() {?->dispatch(?, new ?(...func_get_args()));}', [
			'@self',
			$dispatcher,
			ApplicationEvents::ON_PRESENTER,
			new PhpLiteral(PresenterEvent::class),
		]);

		$application->addSetup('?->onPresenter[] = function($application, $presenter) {if(!property_exists($presenter, "onStartup")){return;} $presenter->onStartup[] = function() {?->dispatch(?, new ?(...func_get_args()));};}', [
			'@self',
			$dispatcher,
			ApplicationEvents::ON_PRESENTER_STARTUP,
			new PhpLiteral(PresenterStartupEvent::class),
		]);

		$application->addSetup('?->onPresenter[] = function($application, $presenter) {if(!property_exists($presenter, "onShutdown")){return;} $presenter->onShutdown[] = function() {?->dispatch(?, new ?(...func_get_args()));};}', [
			'@self',
			$dispatcher,
			ApplicationEvents::ON_PRESENTER_SHUTDOWN,
			new PhpLiteral(PresenterShutdownEvent::class),
		]);

		$application->addSetup('?->onRequest[] = function() {?->dispatch(?, new ?(...func_get_args()));}', [
			'@self',
			$dispatcher,
			ApplicationEvents::ON_REQUEST,
			new PhpLiteral(RequestEvent::class),
		]);

		$application->addSetup('?->onResponse[] = function() {?->dispatch(?, new ?(...func_get_args()));}', [
			'@self',
			$dispatcher,
			ApplicationEvents::ON_RESPONSE,
			new PhpLiteral(ResponseEvent::class),
		]);

		$application->addSetup('?->onShutdown[] = function() {?->dispatch(?, new ?(...func_get_args()));}', [
			'@self',
			$dispatcher,
			ApplicationEvents::ON_SHUTDOWN,
			new PhpLiteral(ShutdownEvent::class),
		]);
	}
147733
protected function buildProcess()
    {
        if (!\count($this->arguments)) {
            throw new LogicException('You must add command arguments before the process can build.');
        }

        $process = new Process($this->arguments, $this->workingDirectory);

        $process->setCommandLine($process->getCommandLine());

        return $process;
    }
188922
public function compile()
    {
        $loader = $this->loaderFactory->newInstance();        
        $cache = $this->cacheFactory->produce();
        
        if(true === $cache->expired()) {
            $config = $this->loadConfig($loader);
            $cache->write($config);
        } else {
            $config = $cache->get();
        }
        
        return $config;
    }
642144
public function run()
    {
        try {

            $controller = $this->route->getControllerName();
            $action     = $this->route->getActionName();

            if (!class_exists($controller)) {
                throw new \DomainException('Controller does not exists! (controller: ' . $controller . ')');
            }

            $controller = new $controller($this->route);

            if (!($controller instanceof ControllerInterface)) {
                throw new \LogicException('Controller does not implement Controller Interface! (controller: ' . get_class($controller) . ')');
            }

        } catch (\DomainException $exception) {
            $this->handleException($exception, 404);

            return;
        } catch (\Exception $exception) {
            $this->handleException($exception, 500);

            return;
        }

        try {

            if (!method_exists($controller, $action)) {
                throw new \DomainException('Action controller does not exists! (' . get_class($controller) . '::' . $action);
            }

            $controller->runBefore();
            $response = $controller->$action();
            $controller->runAfter();

            if (!($response instanceof Response\ResponseInterface)) {
                throw new \Exception('Controller does not return a template object !');
            }

            $response->send();
        } catch (\Exception $exception) {
            $controller->handleException($exception);
        }
    }
1101448
public static function instance()
    {
        if (null === self::$_instance) {
            $routeCollector = new RouteCollector(
                new \FastRoute\RouteParser\Std,
                new \FastRoute\DataGenerator\GroupCountBased
            );
            self::$_instance = $routeCollector;
        }
        return self::$_instance;
    }
1243298
public function run(): void
    {
        if ($this->sapi === null) {
            throw new LogicException('SAPI not set.');
        }

        $container = null;

        try {
            $coreData = $this->getCoreData();

            // match route from `Request` to handler, route container configurators/middleware, etc
            list($matchCode, $allowedMethods, $handlerParams, $handler,
                $routeMiddleware, $routeConfigurators, $requestFactory) = $this->initRouter($coreData)
                ->match($this->sapi->getMethod(), $this->sapi->getUri()->getPath());

            // configure container
            $container = $this->createContainerInstance();
            $globalConfigurators = BaseCoreData::getGlobalConfiguratorsFromData($coreData);
            $this->configureContainer($container, $globalConfigurators, $routeConfigurators);

            // build pipeline for handling `Request`: global middleware -> route middleware -> handler (e.g. controller)

            // select terminal handler
            switch ($matchCode) {
                case RouterInterface::MATCH_FOUND:
                    $handler = $this->createHandler($handler, $handlerParams, $container);
                    break;
                case RouterInterface::MATCH_METHOD_NOT_ALLOWED:
                    $handler = $this->createMethodNotAllowedTerminalHandler($allowedMethods);
                    break;
                default:
                    assert($matchCode === RouterInterface::MATCH_NOT_FOUND);
                    $handler = $this->createNotFoundTerminalHandler();
                    break;
            }

            $globalMiddleware = BaseCoreData::getGlobalMiddlewareFromData($coreData);
            $hasMiddleware    = empty($globalMiddleware) === false || empty($routeMiddleware) === false;

            $handler = $hasMiddleware === true ?
                $this->addMiddlewareChain($handler, $container, $globalMiddleware, $routeMiddleware) : $handler;

            $request =
                $requestFactory === null && $hasMiddleware === false && $matchCode === RouterInterface::MATCH_FOUND ?
                null :
                $this->createRequest($this->sapi, $container, $requestFactory ?? static::getDefaultRequestFactory());

            // Execute the pipeline by sending `Request` down all middleware (global then route's then
            // terminal handler in `Controller` and back) and then send `Response` to SAPI
            $this->sapi->handleResponse($this->handleRequest($handler, $request));
        } catch (Throwable $throwable) {
            $this->sapi->handleResponse($this->handleThrowable($throwable, $container));
        }
    }