/** * @param ApplicationInterface $app * * @throws Exception */ public function run(ApplicationInterface $app) { $this->application = $app; $action = $app->getRequest()->getAction(); if ($action) { $actionMiddleware = new ActionMiddleware(Invokable::cast($action)); $serviceId = $action instanceof ServiceReference ? $action->getId() : $this->computeServiceName($app->getRequest()->getUri()->getPath()); $app->getStep('action')->plug($actionMiddleware); } else { $route = $app->getRequest()->getRoute(); // compute service id $serviceId = $this->computeServiceName($route); // if no service matching the route has been registered, // try to locate a class that could be used as service if (!$app->getServicesFactory()->isServiceRegistered($serviceId)) { $actionClass = $this->resolveActionClassName($route); $action = $this->resolveActionFullyQualifiedName($actionClass); if (!$action) { throw new Exception(sprintf('No callback found to map the requested route "%s"', $route), Exception::ACTION_NOT_FOUND); } $app->getServicesFactory()->registerService(['id' => $serviceId, 'class' => $action]); } // replace action by serviceId to ensure it will be fetched using the ServicesFactory $actionReference = new ServiceReference($serviceId); // wrap action to inject returned value in application $app->getStep('action')->plug($actionMiddleware = new ActionMiddleware($actionReference)); } // store action as application parameter for further reference $app->setParam('runtime.action.middleware', $actionMiddleware); $app->setParam('runtime.action.service-id', $serviceId); }
/** * @param ApplicationInterface $app * @return mixed * */ public function run(ApplicationInterface $app) { $path = rtrim($app->getRequest()->getUri()->getPath(), '/'); // default to home if (!$path) { $path = '/'; } // check if path is routed $aliases = $app->getConfig()->subset(UrlAlias::class); if ($aliases) { $path = $aliases[$path] ?? $path; } // look for matching route $routes = $app->getConfig()->subset(SimpleRoute::class)->reverse(); /** @var SimpleRoute $route */ $routed = false; foreach ($routes as $alias => $route) { if ($route->matches($app->getRequest())) { $app->getRequest()->setAction($route->getAction()); $app->getRequest()->setRoute($alias); $routed = true; break; } } // inject route if none matched if (!$routed) { $app->getRequest()->setRoute($path); } }
public function route(ApplicationInterface $app) : RoutingResult { // add routes $routes = $app->getConfig()->subset(FastRoute::class); $dispatcher = \FastRoute\simpleDispatcher(function (RouteCollector $collector) use($routes) { foreach ($routes as $id => $data) { $collector->addRoute($data['method'], $data['route'], $data['handler']); } }); // Fetch method and URI from somewhere $httpMethod = $app->getRequest()->getMethod(); $uri = $_SERVER['REQUEST_URI']; // Strip query string (?foo=bar) and decode URI if (false !== ($pos = strpos($uri, '?'))) { $uri = substr($uri, 0, $pos); } $uri = rawurldecode($uri); $routeInfo = $dispatcher->dispatch($httpMethod, $uri); switch ($routeInfo[0]) { case Dispatcher::FOUND: $handler = $routeInfo[1]; $vars = $routeInfo[2]; // FastRoute does not allow to name routes for further reference, // so we name matched route "anonymous" by default $matchedRoute = new MatchedRoute($this, 'anonymous', $handler, $vars); return new RoutingResult($matchedRoute); break; case Dispatcher::NOT_FOUND: case Dispatcher::METHOD_NOT_ALLOWED: default: return new RoutingResult(); break; } }
public function run(ApplicationInterface $app) { $matchedRoute = $app->getRequest()->getMatchedRoute(); $action = Invokable::cast($matchedRoute->getAction()); $app->getServicesFactory()->injectDependencies($action->getCallable()); $app->setParam('runtime.action.middleware', $action); $result = $action->getCallable()($app); if ($result instanceof Response) { $app->setResponse($result); } else { // set default content type $app->setResponse((new HttpResponse())->withHeader('Content-Type', 'text/html')); Collection::cast($result)->each(function ($value, $var) { Vars::set($var, $value); }); } }
/** * @param ApplicationInterface $app * @return bool * @throws Exception */ public function run(ApplicationInterface $app) : bool { // check route filter if ($this->getFilter() != '*') { $request = $app->getRequest(); if (!$request) { throw new Exception('Cannot run RouteFilter: no request has been set'); } $route = $request->getRoute(); if (!$route) { throw new Exception('Cannot run RouteFilter: no route has been set'); } if (!$app->getRouteMatcher()->match($this->getFilter(), $route)) { return false; } } return true; }
public function route(ApplicationInterface $app) : RoutingResult { $path = rtrim($app->getRequest()->getUri()->getPath(), '/'); // default to home if (!$path) { $path = '/'; } // check if path is routed $aliases = $app->getConfig()->subset(UrlAlias::class); if ($aliases) { $path = $aliases[$path] ?? $path; } $actionClass = $this->resolveActionClassName($path); $registeredActionNamespaces = $app->getConfig()->get(ActionNamespace::class); $action = $this->resolveActionFullyQualifiedName($actionClass, $registeredActionNamespaces); if (!$action) { return new RoutingResult(); } // return empty RoutingResult return new RoutingResult(new MatchedRoute($this, $path, $action)); }