diff --git a/src/factories/RouterFactory.php b/src/factories/RouterFactory.php index b33b3b5..4901987 100644 --- a/src/factories/RouterFactory.php +++ b/src/factories/RouterFactory.php @@ -10,30 +10,43 @@ class RouterFactory implements FactoryInterface { + /** + * @var array + */ + private $routes; + /** + * @var Router + */ + private $router; + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /** * @var array $config */ $config = $container->get('config'); - /** - * @var array $routes - */ - $routes = $container->get('routes'); - /** - * @var ServerRequestInterface $request - */ + $this->routes = $container->get('routes'); $request = $container->get(ServerRequestInterface::class); - $router = new Router( + $this->router = new Router( $config['app']['default_return_type'] ?? Router::HTML, $request->getMethod(), $request->getUri()->getPath(), '', $config['app']['cache_file'] ); - foreach ($routes as $route) { - $router->add($route[0], $route[1], $route[2], $route[3], $route[4] ?? ''); + return $this->getRouter(); + } + + private function getRouter() + { + $this->addRoutes(); + return $this->router; + } + + private function addRoutes() + { + foreach ($this->routes as $route) { + $this->router->add($route[0], $route[1], $route[2], $route[3], $route[4] ?? ''); } - return $router; } } \ No newline at end of file