public function addRoute(\Phalcon\Mvc\Router\Route $route) { if (@unserialize($route->getName())) { return; } $this->routes[] = $route; }
public function routeExist(\Phalcon\Mvc\Router\Route $route) { foreach ($this->getRoutes() as $_route) { if ($_route->getCompiledPattern() == $route->getCompiledPattern() && $_route->getPaths()['controller'] == $route->getPaths()['controller'] && $_route->getPaths()['action'] == $route->getPaths()['action']) { return true; } } return false; }
public function testAddRouteHttpMethods() { Route::reset(); $config = ['router' => ['home' => ['route' => '/', 'definitions' => ['controller' => 'index', 'action' => 'index', 'module' => 'Application', 'namespace' => 'Application\\Controller'], 'methods' => ['get', 'put']]]]; $di = new Di($config); $di->set('request', function () { return new PhalconRequest(); }); $diManager = new DiManager($di); $diManager->initRouterDi(); $this->assertInstanceOf(Router::class, $diManager->getDI()->get('router')); $router = $diManager->getDI()->get('router'); $httpMethods = ['GET' => true, 'POST' => false, 'PUT' => true, 'PATCH' => false, 'OPTIONS' => false, 'DELETE' => false]; foreach ($httpMethods as $httpMethod => $isMatched) { $_SERVER['REQUEST_METHOD'] = $httpMethod; $router->handle('/'); $this->assertEquals($isMatched, $router->wasMatched()); if ($router->wasMatched()) { $this->assertEquals('index', $router->getControllerName()); $this->assertEquals('index', $router->getActionName()); $this->assertEquals('Application\\Controller', $router->getNamespaceName()); $this->assertEquals('Application', $router->getModuleName()); } } }
/** * Overrides parent reconfigure method. Additionally it * removes extra slashes from the compiled pattern. * * @see parent::reConfigure */ public function reConfigure($pattern, $paths = null) { parent::reConfigure($pattern, $paths); if ($this->removeExtraSlashes) { $this->_compiledPattern = rtrim($this->_compiledPattern, '/') ?: '/'; } }
public function addRoute(\Phalcon\Mvc\Router\Route $route) { if (@unserialize($route->getName())) { return; } $name = $route->getName() ?: $route->getPattern(); $this->addRequest(new Request($this->id, uniqid(), $name, null, $this->basePath . $route->getPattern(), $route->getHttpMethods(), 'Authorization: Bearer {{authToken}}', null, "raw")); }
/** * @group listener */ public function testBootSuccess() { $di = $this->getDi(); Route::reset(); $router = new Router($di); $router->addRoute('controller/action', ['route' => '/test-boot/:controller/:action', 'definitions' => ['controller' => 1, 'action' => 2, 'module' => 'Application', 'namespace' => 'Application\\Controller'], 'methods' => ['GET', 'POST']]); $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = '/test-boot/static/route'; $appMock = m::mock(PhalconApp::class); $appMock->router = $router; $appMock->shouldReceive('getDI')->andReturn($di); $eventMock = m::mock(Event::class); (new ListenApp())->boot($eventMock, $appMock); $this->assertTrue($di->has('matchedRoute')); $this->assertTrue(isset($di['matchedRoute'])); $this->assertInstanceOf(Route::class, $di['matchedRoute']); $this->assertEquals('controller/action', $di['matchedRoute']->getName()); }
public function testHostnameRegexRouteGroup() { $this->specify("Router Groups with regular expressions don't work properly", function ($actualHost, $expectedHost, $controller) { \Phalcon\Mvc\Router\Route::reset(); $di = new \Phalcon\DI(); $di->set("request", function () { return new \Phalcon\Http\Request(); }); $router = new \Phalcon\Mvc\Router(false); $router->setDI($di); $router->add("/edit", ["controller" => "posts3", "action" => "edit3"]); $group = new \Phalcon\Mvc\Router\Group(); $group->setHostname("([a-z]+).phalconphp.com"); $group->add("/edit", ["controller" => "posts", "action" => "edit"]); $router->mount($group); $_SERVER["HTTP_HOST"] = $actualHost; $router->handle("/edit"); expect($router->getControllerName())->equals($controller); expect($router->getMatchedRoute()->getHostname())->equals($expectedHost); }, ["examples" => $this->hostnamedRegexRoutesProvider()]); }
/** * executed before each test */ protected function _before() { parent::_before(); Route::reset(); }
/** * @group before_match */ public function testBeforeMatchRaiseInvalidClass() { require_once 'tests/module/Application/src/Router/BeforeMatchFail.php'; $excMsg = sprintf('"%s" must be implemented "%s"', BeforeMatchFail::class, BeforeMatchInterface::class); $this->setExpectedException(RuntimeException::class, $excMsg); Route::reset(); $router = new Router($this->getDi()); $router->addRoute('edit-classname', ['route' => '/edit2/([1-9][0-9]*)', 'definitions' => ['controller' => 'posts2', 'action' => 'edit2', 'id' => 1], 'before_match' => ['class_name' => BeforeMatchFail::class]]); $router->handle('/edit2/100'); }
private function _getRoutes() { $routes = array(); foreach ($this->routing->route as $route) { $paths = array(self::ATTRIBUTE_NAMESPACE => $this->getModuleNamespace() . '\\Controllers', self::ATTRIBUTE_MODULE => $this->getModuleName(), self::ATTRIBUTE_CONTROLLER => self::VALUE_INDEX, self::ATTRIBUTE_ACTION => self::VALUE_INDEX); $pattern = '/' . trim((string) $route); //var_dump($pattern); foreach ($route->attributes() as $attribute => $value) { if ($attribute === self::ATTRIBUTE_HTTP_METHODS || $attribute === self::ATTRIBUTE_NAME) { continue; } $value = (string) $value; $paths[(string) $attribute] = is_numeric($value) ? (int) $value : $value; } if ($paths[self::ATTRIBUTE_MODULE] != $this->getModuleName()) { unset($paths[self::ATTRIBUTE_NAMESPACE]); } $Route = new Route($pattern, $paths); if (isset($route[self::ATTRIBUTE_HTTP_METHODS])) { $httpMethods = explode(',', (string) $route[self::ATTRIBUTE_HTTP_METHODS]); $Route->via($httpMethods); } if (isset($route[self::ATTRIBUTE_NAME])) { $Route->setName((string) $route[self::ATTRIBUTE_NAME]); } array_push($routes, $Route); } return $routes; }
public function transform(Route $route) { return ['name' => $route->getName(), 'pattern' => $route->getPattern()]; }
/** * Set Http methods constraints for router * * @param Route $route * @param array $httpMethods */ protected function setHttpMethods(Route $route, array $httpMethods) { foreach ($httpMethods as $idx => $method) { $httpMethods[$idx] = strtoupper($method); } $route->via($httpMethods); }
/** * @param Router\Route $route * @return array */ private function collectParams(Router\Route $route) { $matches = $this->router->getMatches(); $params = []; foreach ($route->getPaths() as $name => $position) { if (isset($matches[$position])) { $params[$name] = $matches[$position]; } } return $params; }