Phlyty\App::registerRouteListener PHP Method

registerRouteListener() protected method

Register the route listener with the route event
protected registerRouteListener ( )
    protected function registerRouteListener()
    {
        if ($this->routeListenerRegistered) {
            return;
        }
        $closure = function ($e) {
            $method = $e->getParam('method');
            if (!isset($this->routesByMethod[$method])) {
                throw new Exception\PageNotFoundException();
            }
            $request = $e->getParam('request');
            $routes = $this->routesByMethod[$method];
            $baseUrl = $request->getBaseUrl();
            $baseUrlLength = strlen($baseUrl) ?: null;
            $requestUrlLength = strlen($request->getUri()->getPath());
            $pathLength = $baseUrlLength !== null ? $requestUrlLength - $baseUrlLength : null;
            foreach ($routes as $index => $route) {
                if ($index <= $this->routeIndex) {
                    // Skip over routes we've already looked at
                    continue;
                }
                if (($result = $route->route()->match($request, $baseUrlLength)) instanceof RouteMatch && ($pathLength === null || $result->getLength() === $pathLength)) {
                    $this->routeIndex = $index;
                    $this->params = $result;
                    $e->setRoute($route);
                    return $route;
                }
            }
            throw new Exception\PageNotFoundException();
        };
        $closure = $closure->bindTo($this);
        $this->events()->attach('route', $closure);
        $this->routeListenerRegistered = true;
    }