/** * * {@inheritDoc} * * @see \Nia\Routing\Router\RouterInterface::handle($request, $context) */ public function handle(RequestInterface $request, MapInterface $context) : ResponseInterface { foreach ($this->getRoutes() as $route) { if (!$route->getCondition()->checkCondition($request, $context)) { continue; } // create a writeable clone of the passed context to allow communication between filter and handler $context = new Map(iterator_to_array($context->getIterator())); $filter = $route->getFilter(); try { return $filter->filterRequest($request, $context); } catch (IgnoreFilterException $exception) { } $response = $route->getHandler()->handle($request, $context); return $filter->filterResponse($response, $context); } throw new NoRouteMatchedException(); }
/** * Determines the header by using passed server configuration. * * @param string[] $server * The server configuration. * @return MapInterface The determined header as a map. */ private function determineHeader(array $server) : MapInterface { $header = new Map(); foreach ($server as $name => $value) { if (strpos($name, 'HTTP_') === 0) { $name = substr($name, 5); $name = explode('_', $name); $name = array_map('strtolower', $name); $name = array_map('ucfirst', $name); $name = implode('-', $name); $header->set($name, $value); } } return new ReadOnlyMap($header); }
/** * Reads the arguments as a map from a list of raw arguments. * * @param string[] $arguments * List with raw arguments. * @return MapInterface Readed arguments as a map. */ private function readArguments(array $arguments) : MapInterface { $result = new Map(); $regex = '/^--(?P<name>[a-z_][a-z0-9_-]*)(\\=(?P<value>(.*)))?$/i'; $matches = []; foreach ($arguments as $argument) { if (preg_match($regex, $argument, $matches) !== 0) { $result->set($matches['name'], trim($matches['value'] ?? '', '"')); } } return new ReadOnlyMap($result); }