Example #1
0
 /**
  * Execute router middleware functionality
  */
 public function do(InternalRequest $ireq)
 {
     $toMatch = $ireq->uriPath;
     if (isset($this->cache[$toMatch])) {
         list($args, $routeArgs) = $cache = $this->cache[$toMatch];
         list($action, $middlewares) = $args;
         $ireq->locals["aerys.routeArgs"] = $routeArgs;
         // Keep the most recently used entry at the back of the LRU cache
         unset($this->cache[$toMatch]);
         $this->cache[$toMatch] = $cache;
         $ireq->locals["aerys.routed"] = [$isMethodAllowed = true, $action];
         if (!empty($middlewares)) {
             yield from responseFilter($middlewares, $ireq);
         }
     }
     $match = $this->routeDispatcher->dispatch($ireq->method, $toMatch);
     switch ($match[0]) {
         case Dispatcher::FOUND:
             list(, $args, $routeArgs) = $match;
             list($action, $middlewares) = $args;
             $ireq->locals["aerys.routeArgs"] = $routeArgs;
             if ($this->maxCacheEntries > 0) {
                 $this->cacheDispatchResult($toMatch, $routeArgs, $args);
             }
             $ireq->locals["aerys.routed"] = [$isMethodAllowed = true, $action];
             if (!empty($middlewares)) {
                 yield from responseFilter($middlewares, $ireq);
             }
             break;
         case Dispatcher::NOT_FOUND:
             // Do nothing; allow actions further down the chain a chance to respond.
             // If no other registered host actions respond the server will send a
             // 404 automatically anyway.
             return;
             break;
         case Dispatcher::METHOD_NOT_ALLOWED:
             $allowedMethods = $match[1];
             $ireq->locals["aerys.routed"] = [$isMethodAllowed = false, $allowedMethods];
             break;
         default:
             throw new \UnexpectedValueException("Encountered unexpected Dispatcher code");
     }
 }
Example #2
0
 private function initializeResponse(InternalRequest $ireq) : Response
 {
     $ireq->responseWriter = $ireq->client->httpDriver->writer($ireq);
     $filters = $ireq->client->httpDriver->filters($ireq);
     if ($ireq->badFilterKeys) {
         $filters = array_diff_key($filters, array_flip($ireq->badFilterKeys));
     }
     $filter = responseFilter($filters, $ireq);
     $codec = $this->responseCodec($filter, $ireq);
     $codec->current();
     // initialize codec
     return $ireq->response = new StandardResponse($codec);
 }