Author: Paul Dragoonis (paul@ppi.io)
Inheritance: extends Symfony\Cmf\Component\Routing\ChainRouter
Esempio n. 1
0
 /**
  * @todo - move this to a separate method() - consider how to inject custom-defined arbitrary chain router entries
  *
  * @param ServiceLocatorInterface $serviceLocator
  *
  * @throws \Exception
  *
  * @return ChainRouter
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $request = $serviceLocator->get('Request');
     $requestContext = $serviceLocator->get('RouterRequestContext');
     $routerOptions = array();
     $logger = $serviceLocator->has('Logger') ? $serviceLocator->get('Logger') : null;
     $chainRouter = new ChainRouter($logger);
     if ($serviceLocator->has('RoutingCache')) {
         $chainRouter->setCache($serviceLocator->get('RoutingCache'));
     }
     $chainRouter->setContext($requestContext);
     $allModuleRoutes = $serviceLocator->get('ModuleDefaultListener')->getRoutes();
     // For each module, add a matching instance type to the chain router
     foreach ($allModuleRoutes as $moduleName => $moduleRoutingResponse) {
         switch (true) {
             // @todo - move this to a separate method()
             case $moduleRoutingResponse instanceof SymfonyRouteCollection:
                 $sfRouter = new SymfonyRouter($requestContext, $moduleRoutingResponse, $routerOptions, $logger);
                 $sfRouterWrapper = new SymfonyRouterWrapper($sfRouter);
                 $chainRouter->add($sfRouterWrapper);
                 break;
                 // @todo - move this to a separate method()
             // @todo - move this to a separate method()
             case $moduleRoutingResponse instanceof AuraRouter:
                 $auraRouterWrapper = new AuraRouterWrapper($moduleRoutingResponse);
                 $chainRouter->add($auraRouterWrapper);
                 break;
                 // @todo - move this to a separate method()
             // @todo - move this to a separate method()
             case $moduleRoutingResponse instanceof LaravelRouter:
                 $laravelRequest = new LaravelRequest();
                 $laravelUrlGenerator = new LaravelUrlGenerator($moduleRoutingResponse->getRoutes(), $laravelRequest);
                 $laravelRouterWrapper = new LaravelRouterWrapper($moduleRoutingResponse, $laravelRequest, $laravelUrlGenerator);
                 // @todo - solve this problem
                 //                    $laravelRouterWrapper->setModuleName($this->getName());
                 $chainRouter->add($laravelRouterWrapper);
                 break;
             case $moduleRoutingResponse instanceof FastRouteWrapper:
                 $chainRouter->add($moduleRoutingResponse);
                 break;
             default:
                 throw new \Exception('Unexpected routes value return from module: ' . $moduleName . '. found value of type: ' . gettype($moduleRoutingResponse));
         }
     }
     return $chainRouter;
 }
Esempio n. 2
0
 /**
  * Perform the matching of a route and return a set of routing parameters if a valid one is found.
  * Otherwise exceptions get thrown.
  *
  * @param HttpRequest $request
  *
  * @throws \Exception
  *
  * @return array
  */
 protected function handleRouting(HttpRequest $request)
 {
     $this->router = $this->serviceManager->get('Router');
     $this->router->warmUp($this->getCacheDir());
     try {
         // Lets load up our router and match the appropriate route
         $parameters = $this->router->matchRequest($request);
         if (!empty($parameters)) {
             if (null !== $this->logger) {
                 $this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], $this->router->parametersToString($parameters)));
             }
         }
     } catch (ResourceNotFoundException $e) {
         $routeUri = $this->router->generate('Framework_404');
         $parameters = $this->router->matchRequest($request::create($routeUri));
     } catch (\Exception $e) {
         throw $e;
     }
     $parameters['_route_params'] = $parameters;
     return $parameters;
 }