/** * Merge parameters from another match. * * @param self $match * @return self */ public function merge(self $match) { $this->params = array_merge($this->params, $match->getParams()); $this->length += $match->getLength(); $this->matchedRouteName = $match->getMatchedRouteName(); return $this; }
private function isRouteExcluded(RouteMatch $routeMatch) { $controller = $routeMatch->getParam('controller'); $action = $routeMatch->getParam('action'); // todo: evaluate if controller / action are excluded return false; }
/** * {@inheritDoc} */ public function setUp() { $this->serviceManager = Bootstrap::getServiceManager(); $this->program = new Program(); $this->program->setId(1); $this->program->setProgram('This is the program'); $this->authorizeService = $this->serviceManager->get('BjyAuthorize\\Service\\Authorize'); if (!$this->authorizeService->getAcl()->hasResource($this->program)) { $this->authorizeService->getAcl()->addResource($this->program); $this->authorizeService->getAcl()->allow([], $this->program, []); } /** * Add the resource on the fly */ if (!$this->authorizeService->getAcl()->hasResource(new Program())) { $this->authorizeService->getAcl()->addResource(new Program()); } $this->authorizeService->getAcl()->allow([], new Program(), []); $this->programLink = $this->serviceManager->get('viewhelpermanager')->get('programlink'); $routeMatch = new RouteMatch(['program' => 1]); $routeMatch->setMatchedRouteName('route-program_entity_program'); $this->programLink->setRouteMatch($routeMatch); /** * Bootstrap the application to have the other information available */ $application = $this->serviceManager->get('application'); $application->bootstrap(); }
public function testMatchedRouteNameIsOverridenOnMerge() { $match = new RouteMatch(array()); $match->setMatchedRouteName('foo'); $subMatch = new RouteMatch(array()); $subMatch->setMatchedRouteName('bar'); $match->merge($subMatch); $this->assertEquals('bar', $match->getMatchedRouteName()); }
public function testAddBodyClass() { $bodyClassHelper = new BodyClass(); $listener = new BodyClassListener($bodyClassHelper); $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName('my/SOME/route'); $event = new \Zend\Mvc\MvcEvent(); $event->setRouteMatch($routeMatch); $listener->addBodyClass($event); $this->assertEquals('my-some-route', (string) $bodyClassHelper); }
protected function createMvcEvent() { $this->mvcEvent = new MvcEvent(); $request = new Request(); $response = new Response(); $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName('test/route'); $routeMatch->setParam('controller', 'Application\\Controller\\Index'); $routeMatch->setParam('action', 'index'); $this->mvcEvent->setRouteMatch($routeMatch); $this->mvcEvent->setRequest($request); $this->mvcEvent->setResponse($response); }
/** * @param $component * @return bool */ public function canApply($component, $config = null) { $config = is_null($config) ? $this->config : $config; $include = (array) Arrays::getRecursive($config, (string) $component . ".include"); $exclude = (array) Arrays::getRecursive($config, (string) $component . ".exclude"); $controller = $this->routeMatch->getParam('controller'); $action = $this->routeMatch->getParam('action'); if (in_array($controller, $include)) { return true; } if (in_array($controller, $exclude)) { return false; } return true; }
/** * Match a given request. * * @param Request $request Request to match * * @return RouteMatch */ public function match(Request $request) { // Get command line arguments and present working directory from // server superglobal: $filename = $request->getScriptName(); $pwd = CLI_DIR; // Convert base filename (minus .php extension and underscores) and // containing directory name into action and controller, respectively: $baseFilename = str_replace('_', '', basename($filename)); $baseFilename = substr($baseFilename, 0, strlen($baseFilename) - 4); $baseDirname = basename(dirname(realpath($pwd . '/' . $filename))); $routeMatch = new RouteMatch(array('controller' => $baseDirname, 'action' => $baseFilename), 1); // Override standard routing: $routeMatch->setMatchedRouteName('default'); return $routeMatch; }
protected function getParamsFromRouteMatch(RouteMatch $routeMatch) { $params = array(); $controllerClass = $routeMatch->getParam('controller'); $chunks = explode('\\', $controllerClass); $moduleName = strtolower($chunks[0]); $controllerName = strtolower($chunks[sizeof($chunks) - 1]); $actionName = strtolower($routeMatch->getParam('action')); if ($actionName == 'index') { $actionName = 'read'; } $params['module'] = $moduleName; $params['controller'] = $controllerName; $params['action'] = $actionName; return $params; }
/** * Because we are running our auth checks before dispatch most of the time, check to see if the route * is a SM alias of zf-rest or zf-rpc which are the common settings for API routes */ private function isApiRequest(ServiceLocatorInterface $sm, RouteMatch $routeMatch) { $config = $sm->get('config'); // Check to see if we are dealing with an Apigility command, if so dont rate limit // Since Apigility should never be available in anything but dev, dont worry about anything else if (!array_key_exists('APPLICATION_ENV', $_SERVER) || $_SERVER['APPLICATION_ENV'] !== 'development') { return false; } if ($routeMatch->getParam('is_apigility_admin_api') === TRUE) { return false; } $controllerName = $routeMatch->getParam('controller'); $rpcControllers = array_keys($config['zf-rpc']); $restControllers = array_keys($config['zf-rest']); $apiControllers = array_merge($rpcControllers, $restControllers); return in_array($controllerName, $apiControllers); }
/** * @dataProvider tetShowBackButtonDefaultProvider */ public function testShowBackButtonDefault($id, $backLinkValue, $expectedBackLink) { $this->docs->expects($this->once())->method('getOne')->with($id); $this->routeMatch->setParam('action', 'show'); $this->routeMatch->setParam('id', $id); if (null !== $backLinkValue) { $this->routeMatch->setParam('show_back_link', $backLinkValue); } $result = $this->object->dispatch($this->request, $this->response); $this->assertInternalType('array', $result); $this->assertArrayHasKey('data', $result); $this->assertArrayHasKey('showBackLink', $result); $this->assertEquals($expectedBackLink, $result['showBackLink']); }
protected function dispatch($controller, $action, $renderView = FALSE) { $this->event = $this->application->getMvcEvent(); $this->request = new Request(); // $this->routeMatch = $this->eventManager->trigger(MvcEvent::EVENT_ROUTE, $this->event)->first(); $this->routeMatch = new RouteMatch(array('controller' => $controller)); $this->routeMatch->setParam('action', $action); $this->event->setRouteMatch($this->routeMatch); $this->controller->setEvent($this->event); return $this->controller->dispatch($this->request, $this->response); // $this->request->setUri($url); // $this->viewModel = $this->eventManager->trigger(MvcEvent::EVENT_DISPATCH, $this->event)->first(); // $responseEvent = ($renderView ? MvcEvent::EVENT_RENDER : MvcEvent::EVENT_FINISH); // $this->response = $this->eventManager->trigger($responseEvent, $this->event)->first(); }
/** * @param array $params * @param string $lang * * @return array */ private function prepareLanguageInParams(array $params, $lang) { // when error occurred (404) we don't have routeMatch if ($this->routeMatch) { // we change lang only there when lang parameter exists if ($this->routeMatch->getParam('lang') !== null) { $params['lang'] = $lang; } } else { $params['lang'] = $lang; } // if default app language is the same as lang and shouldRedirect is disabled if ($this->config->getDefaultLanguage() === $lang && !$this->config->shouldRedirectToRecognizedLanguage()) { $params['lang'] = ''; } return $params; }
/** * If the system is offline, set up a handler to override the routing output. * * @return void */ protected function initSystemStatus() { // If the system is unavailable, forward to a different place: if (isset($this->config->System->available) && !$this->config->System->available) { $callback = function ($e) { $routeMatch = new RouteMatch(['controller' => 'Error', 'action' => 'Unavailable'], 1); $routeMatch->setMatchedRouteName('error-unavailable'); $e->setRouteMatch($routeMatch); }; $this->events->attach('route', $callback); } }
/** * Legacy handling for scripts: Match a given request. * * @param Request $request Request to match * * @return RouteMatch */ public function match(Request $request) { // Get command line arguments and present working directory from // server superglobal: $filename = $request->getScriptName(); // WARNING: cwd is $VUFIND_HOME, so that throws off realpath! // // Convert base filename (minus .php extension and underscores) and // containing directory name into action and controller, respectively: $base = basename($filename, ".php"); $actionName = str_replace('_', '', $base); // action is the easy part $dir = dirname($filename); if ($dir == false || $dir == '' || $dir == '.' || basename($dir) == '.') { // legacy style: cd to subdir, but set CLI_DIR $dir = $this->getCliDir(); $path = $dir . '/' . $filename; } else { // modern style: invoked as, e.g. $base=util/ping.php, already has path $level1 = basename(dirname($filename)); // but we need to re-orient relative to VUFIND_HOME $path = $level1 . '/' . basename($filename); } $controller = basename($dir); // the last directory part // Special case: if user is accessing index.php directly, we expect // controller and action as first two parameters. if ($controller == 'public' && $actionName == 'index') { list($controller, $actionName) = $this->extractFromCommandLine(); } $routeMatch = new RouteMatch(['controller' => $controller, 'action' => $actionName], 1); // Override standard routing: $routeMatch->setMatchedRouteName('default'); return $routeMatch; }
/** * @dataProvider dataProviderForTestRecognize_WhenShouldRedirectToRecognizedLanguageIsEnabled * * @param string $browserLangLocale * @param string $routeMatchLang * @param array $expectedResult */ public function testRecognize_WhenShouldRedirectToRecognizedLanguageIsEnabled($browserLangLocale, $routeMatchLang, $expectedResult) { $this->prepareTestedObject($this->prepareConfig(true)); $request = new Request(); $request->getHeaders()->addHeaderLine('Accept-Language', $browserLangLocale); $paramsBefore = ['__NAMESPACE__' => 'Module\\Some', 'controller' => 'TestController', 'action' => 'index', 'lang' => $routeMatchLang, '__CONTROLLER__' => 'test']; $routeMatch = new RouteMatch($paramsBefore); $matchedRouteName = 'some\\test'; $routeMatch->setMatchedRouteName($matchedRouteName); $event = new MvcEvent(); $event->setRequest($request); $event->setResponse(new Response()); $event->setRouteMatch($routeMatch); $result = $this->testedObject->recognize($event); $this->assertSame($expectedResult, $result); }
/** * match(): defined by RouteInterface interface. * * @see \Zend\Mvc\Router\RouteInterface::match() * @param Request $request * @param int|null $pathOffset * @param array $options * @return RouteMatch|null */ public function match(Request $request, $pathOffset = null, array $options = array()) { if (!method_exists($request, 'getUri')) { return null; } if ($pathOffset === null) { $mustTerminate = true; $pathOffset = 0; } else { $mustTerminate = false; } if ($this->chainRoutes !== null) { $this->addRoutes($this->chainRoutes); $this->chainRoutes = null; } $match = new RouteMatch(array()); $uri = $request->getUri(); $pathLength = strlen($uri->getPath()); foreach ($this->routes as $route) { $subMatch = $route->match($request, $pathOffset, $options); if ($subMatch === null) { return null; } $match->merge($subMatch); $pathOffset += $subMatch->getLength(); } if ($mustTerminate && $pathOffset !== $pathLength) { return null; } return $match; }
/** * @dataProvider dataProviderForTestCheckRedirect_WhenShouldRedirectToRecognizedLanguageIsEnabled * * @param string $recognizedLang * @param string $routeMatchLang * @param string $routeMatchLangAfterRedirect * @param array $expectedResult */ public function testCheckRedirect_WhenShouldRedirectToRecognizedLanguageIsEnabled($recognizedLang, $routeMatchLang, $routeMatchLangAfterRedirect, $expectedResult) { $this->prepareTestedObject($this->prepareConfig(true)); $paramsBefore = ['__NAMESPACE__' => 'Module\\Some', 'controller' => 'TestController', 'action' => 'index', 'lang' => $routeMatchLang ? $routeMatchLang : '', '__CONTROLLER__' => 'test']; $paramsAfter = ['__NAMESPACE__' => 'Module\\Some', 'controller' => 'test', 'action' => null, 'lang' => $routeMatchLangAfterRedirect, '__CONTROLLER__' => 'test']; $routeMatch = new RouteMatch($paramsBefore); $matchedRouteName = 'some\\test'; $routeMatch->setMatchedRouteName($matchedRouteName); $event = new MvcEvent(); $event->setResponse(new Response()); $event->setRouteMatch($routeMatch); $uriLangPrefix = $routeMatchLangAfterRedirect ? '/' . $routeMatchLangAfterRedirect : ''; $expectedUri = $uriLangPrefix . '/some/test'; if ($expectedResult) { $this->routeMock->expects($this->once())->method('assemble')->with($paramsAfter, ['name' => $matchedRouteName])->willReturn($expectedUri); } $event->setRouter($this->routeMock); $result = $this->testedObject->checkRedirect($event, $recognizedLang, $routeMatchLang); $this->assertSame($expectedResult, $result); }