예제 #1
0
 /**
  * @param null $requestMethod
  * @param null $url
  *
  * @return IHttpResponse
  */
 public function createResponse($requestMethod = null, $url = null)
 {
     if (!$requestMethod) {
         $requestMethod = $this->getRequestMethod();
     }
     if (!$url) {
         $url = $this->getUrl();
     }
     $route = $this->router->match($requestMethod, $url);
     if ($route instanceof IRoute) {
         return $this->responseBuilder->buildResponseForRoute($route);
     }
     return $this->responseBuilder->buildDefaultErrorResponse();
 }
 /**
  * @param $name
  * @param $repositoryClass
  *
  * @return $this
  * @throws InvalidRepositoryClassException
  * @throws RepositoryClassNotFoundException
  */
 public function resolve($name, $repositoryClass)
 {
     if (!class_exists($repositoryClass)) {
         throw new RepositoryClassNotFoundException(s('Class "%s" not found.', $repositoryClass));
     }
     if (!array_contains(class_implements($repositoryClass), ObjectRepository::class)) {
         throw new InvalidRepositoryClassException(s('Class "%s" must implement interface "%s".', $repositoryClass, ObjectRepository::class));
     }
     $this->router->addResolver($name, function ($parameter, IContainer $container) use($repositoryClass) {
         $repository = $container->get($repositoryClass);
         return $repository->find($parameter);
     });
     return $this;
 }
 /**
  * @param IRouter $router
  *
  * @return array
  */
 private function gatherFacts(IRouter $router)
 {
     $facts = [];
     foreach ($router->getRoutesMatcher()->getFiltersMatcher()->getFilters() as $filter) {
         $filters[] = $filter->getName();
     }
     foreach ($router->getRoutes() as $route) {
         $filters = [];
         foreach ($router->getRoutesMatcher()->getFiltersMatcher()->getFilters() as $filter) {
             if ($filter->isEnabled()) {
                 $filters[] = $filter->getName();
             }
         }
         $facts[] = ['filters' => $filters, 'hosts' => $router->getRoutesMatcher()->getRestrictionsMatcher()->getHosts(), 'domains' => $router->getRoutesMatcher()->getRestrictionsMatcher()->getDomains(), 'subdomains' => $router->getRoutesMatcher()->getRestrictionsMatcher()->getSubdomains(), 'protocols' => $router->getRoutesMatcher()->getRestrictionsMatcher()->getProtocols(), 'tlds' => $router->getRoutesMatcher()->getRestrictionsMatcher()->getTLDs(), 'methods' => $route->getMethods(), 'path' => $route->getPath()];
     }
     foreach ($router->getNestedRouters() as $router) {
         $facts = array_merge($facts, $this->gatherFacts($router));
     }
     return $facts;
 }
 /**
  * @param IHttpRequest $request
  *
  * @return IHttpResponse
  */
 public function handle(IHttpRequest $request)
 {
     $route = $this->router->match($request->getMethod(), $request->getUrl());
     return $this->routesInvoker->invoke($route);
 }
 /**
  * @param IRouter $router
  * @param array $config
  *
  * @throws InvalidConfigurationException
  */
 protected function processGroups(IRouter $router, array $config)
 {
     $groups = array_get($config, 'groups', []);
     if (!is_array($groups)) {
         throw new InvalidConfigurationException('Routing config "groups" must be an array.');
     }
     foreach ($groups as $group) {
         if (!is_array($group)) {
             throw new InvalidConfigurationException(s('Routing "groups" must consist of arrays, received "%s".', get_type($group)));
         }
         $this->processConfig($router->group(), $group);
     }
 }