/** * Retrieve the version from the route match. * * The route prototype sets "version", while the Content-Type listener sets * "zf_ver_version"; check both to obtain the version, giving priority to the * route prototype result. * * @param RouteMatch|V2RouteMatch $routeMatches * @return int|false */ protected function getVersionFromRouteMatch($routeMatches) { $version = $routeMatches->getParam('zf_ver_version', false); if ($version) { return $version; } return $routeMatches->getParam('version', false); }
/** * Returns whether page should be considered active or not * * This method will compare the page properties against the route matches * composed in the object. * * @param bool $recursive [optional] whether page should be considered * active if any child pages are active. Default is * false. * @return bool whether page should be considered active or not */ public function isActive($recursive = false) { if (!$this->active) { $reqParams = []; if ($this->routeMatch instanceof RouteMatch || $this->routeMatch instanceof MvcRouter\RouteMatch) { $reqParams = $this->routeMatch->getParams(); if (isset($reqParams[self::ORIGINAL_CONTROLLER])) { $reqParams['controller'] = $reqParams[self::ORIGINAL_CONTROLLER]; } $pageParams = $this->params; if (null !== $this->controller) { $pageParams['controller'] = $this->controller; } if (null !== $this->action) { $pageParams['action'] = $this->action; } if (null !== $this->getRoute()) { if ($this->routeMatch->getMatchedRouteName() === $this->getRoute() && count(array_intersect_assoc($reqParams, $pageParams)) == count($pageParams)) { $this->active = true; return $this->active; } else { return parent::isActive($recursive); } } } $pageParams = $this->params; if (null !== $this->controller) { $pageParams['controller'] = $this->controller; } else { /** * @todo In ZF1, this was configurable and pulled from the front controller */ $pageParams['controller'] = 'index'; } if (null !== $this->action) { $pageParams['action'] = $this->action; } else { /** * @todo In ZF1, this was configurable and pulled from the front controller */ $pageParams['action'] = 'index'; } if (count(array_intersect_assoc($reqParams, $pageParams)) == count($pageParams)) { $this->active = true; return true; } } return parent::isActive($recursive); }
/** * Retrieve the identifier, if any * * Attempts to see if an identifier was passed in either the URI or the * query string, returning it if found. Otherwise, returns a boolean false. * * @param \Zend\Router\RouteMatch $routeMatch * @param Request $request * @return false|mixed */ protected function getIdentifier($routeMatch, $request) { $identifier = $this->getIdentifierName(); $id = $routeMatch->getParam($identifier, false); if ($id !== false) { return $id; } $id = $request->getQuery()->get($identifier, false); if ($id !== false) { return $id; } return false; }
/** * Inject regex matches into the route matches * * @param RouteMatch|V2RouteMatch $routeMatches * @param array $matches */ protected function injectRouteMatches($routeMatches, array $matches) { foreach ($matches as $key => $value) { if (is_numeric($key) || is_int($key) || $value === '') { continue; } $routeMatches->setParam($key, $value); } }
/** * Create a part RouteMatch with given parameters and length. * * @param array $params * @param int $length */ public function __construct(array $params, $length = 0) { parent::__construct($params); $this->length = $length; }
/** * Dispatch another controller * * @param string $name Controller name; either a class name or an alias used in the controller manager * @param null|array $params Parameters with which to seed a custom RouteMatch object for the new controller * @return mixed * @throws Exception\DomainException if composed controller does not define InjectApplicationEventInterface * or Locator aware; or if the discovered controller is not dispatchable */ public function dispatch($name, array $params = null) { $event = clone $this->getEvent(); $controller = $this->controllers->get($name); if ($controller instanceof InjectApplicationEventInterface) { $controller->setEvent($event); } // Allow passing parameters to seed the RouteMatch with & copy matched route name if ($params !== null) { $routeMatch = new RouteMatch($params); $routeMatch->setMatchedRouteName($event->getRouteMatch()->getMatchedRouteName()); $event->setRouteMatch($routeMatch); } if ($this->numNestedForwards > $this->maxNestedForwards) { throw new Exception\DomainException("Circular forwarding detected: greater than {$this->maxNestedForwards} nested forwards"); } $this->numNestedForwards++; // Detach listeners that may cause problems during dispatch: $sharedEvents = $event->getApplication()->getEventManager()->getSharedManager(); $listeners = $this->detachProblemListeners($sharedEvents); $return = $controller->dispatch($event->getRequest(), $event->getResponse()); // If we detached any listeners, reattach them now: $this->reattachProblemListeners($sharedEvents, $listeners); $this->numNestedForwards--; return $return; }
/** * Does the request represent a collection? * * @param string $serviceName * @param array $data * @param RouteMatch|V2RouteMatch $matches * @param HttpRequest $request * @return bool */ protected function isCollection($serviceName, $data, $matches, HttpRequest $request) { if (!array_key_exists($serviceName, $this->restControllers)) { return false; } if ($request->isPost() && (empty($data) || ArrayUtils::isHashTable($data))) { return false; } $identifierName = $this->restControllers[$serviceName]; if ($matches->getParam($identifierName) !== null) { return false; } return null === $request->getQuery($identifierName, null); }
/** * Valid route but with no login with no login */ public function it_dispatches_unauthorized(MvcEvent $event, RouteMatch $match, EventManagerInterface $eventManager, Application $application, $accessService) { $application->getEventManager()->willReturn($eventManager); $accessService->requiresAuthentication(self::CONTROLLER_ADMIN, 'index')->willReturn(true); $accessService->hasUser()->willReturn(false); $event->getTarget()->willReturn($application); $match->getParam('controller')->willReturn(self::CONTROLLER_ADMIN); $match->getParam('action')->willReturn('index'); $match->getMatchedRouteName()->willReturn('admin'); $event->getRouteMatch()->willReturn($match); $accessService->getRoles()->willReturn([]); $eventManager->triggerEvent($event)->shouldBeCalled(); $event->setError(AccessService::ACCESS_UNAUTHORIZED)->shouldBeCalled(); $event->setParam('route', 'admin')->shouldBeCalled(); $event->setParam('controller', self::CONTROLLER_ADMIN)->shouldBeCalled(); $event->setParam('action', 'index')->shouldBeCalled(); $event->setParam('roles', 'none')->shouldBeCalled(); $event->setName(MvcEvent::EVENT_DISPATCH_ERROR)->shouldBeCalled(); $this->verifyAccess($event); }
/** * Create a successful RouteResult from the given RouteMatch. * * @param RouteMatch $match * @return RouteResult */ private function marshalSuccessResultFromRouteMatch(RouteMatch $match) { $params = $match->getParams(); if (array_key_exists(self::METHOD_NOT_ALLOWED_ROUTE, $params)) { return RouteResult::fromRouteFailure($this->allowedMethodsByPath[$params[self::METHOD_NOT_ALLOWED_ROUTE]]); } return RouteResult::fromRouteMatch($this->getMatchedRouteName($match->getMatchedRouteName()), $params['middleware'], $params); }
/** * Attempt to retrieve the identifier for a given request * * Checks first if the $identifierName is in the route matches, and then * as a query string parameter. * * @param string $identifierName * @param RouteMatch|V2RouteMatch $routeMatch Validated by calling method. * @param \Zend\Stdlib\RequestInterface $request * @return false|mixed */ protected function getIdentifier($identifierName, $routeMatch, $request) { $id = $routeMatch->getParam($identifierName, false); if ($id !== false) { return $id; } if (!$request instanceof Request) { return false; } return $request->getQuery($identifierName, false); }