/** * @test */ public function routeTest() { $route = new Route('get', 'foo/bar/baz', RoutableController::class, 'exampleAction'); $this->assertEquals('get', $route->getRequestMethod()); $this->assertEquals('foo/bar/baz', $route->getPathPattern()); $this->assertEquals(RoutableController::class, $route->getControllerClassName()); $this->assertEquals('exampleAction', $route->getMethodName()); $this->assertEquals('get foo/bar/baz => Bleicker\\FastRoute\\RequestHandler\\Tests\\Unit\\Fixtures\\Controller\\RoutableController::exampleAction', $route->getIdentity()); }
/** * @param string $requestMethod * @param string $pathPattern * @param string $controllerClassName * @param string $methodName * * @return void * @throws RouteAlreadyExistsException * @throws NoRoutableControllerException * @throws NoRoutableMethodException */ public static function add($requestMethod, $pathPattern, $controllerClassName, $methodName) { $route = new Route($requestMethod, $pathPattern, $controllerClassName, $methodName); if (array_key_exists($route->getIdentity(), static::$routeRegister)) { throw new RouteAlreadyExistsException('Route ' . $route->getIdentity() . ' already exists', 1455199265); } $controllerClassNameReflection = new ReflectionClass($controllerClassName); if (!$controllerClassNameReflection->implementsInterface(ControllerInterface::class)) { throw new NoRoutableControllerException($controllerClassName . ' must implement ' . ControllerInterface::class, 1455199942); } $methodReflection = $controllerClassNameReflection->getMethod($methodName); if ($methodReflection->isPrivate()) { throw new NoRoutableMethodException($controllerClassName . '::' . $methodName . ' must not be private', 1455199943); } if ($methodReflection->isStatic()) { throw new NoRoutableMethodException($controllerClassName . '::' . $methodName . ' must not be static', 1455199944); } if ($methodReflection->isProtected()) { throw new NoRoutableMethodException($controllerClassName . '::' . $methodName . ' must not be protected', 1455199945); } static::$routeRegister[$route->getIdentity()] = $route; }