diff --git a/src/Router.php b/src/Router.php index 862e6a6..f085d74 100644 --- a/src/Router.php +++ b/src/Router.php @@ -69,11 +69,11 @@ class Router * @param Publisher $publisher */ public function __construct( - Container $container, + Container $container, Storekeeper $storekeeper, - Matcher $matcher, - Caller $caller, - Publisher $publisher + Matcher $matcher, + Caller $caller, + Publisher $publisher ) { $this->container = $container; @@ -101,9 +101,9 @@ public static function create(): self /** * Setup (enable) View + * @param string $directory * @link View * - * @param string $directory */ public function setupView(string $directory): void { @@ -162,6 +162,16 @@ public function pattern(string $name, string $pattern) $this->patterns[$name] = $pattern; } + /** + * Index all the defined routes + * + * @return Route[] + */ + public function all(): array + { + return $this->storekeeper->getRepository()->all(); + } + /** * Define a new route * diff --git a/src/Routing/Repository.php b/src/Routing/Repository.php index 6f645b3..fd0cda5 100644 --- a/src/Routing/Repository.php +++ b/src/Routing/Repository.php @@ -30,11 +30,11 @@ class Repository * @param string|null $domain */ public function save( - string $method, - string $path, - $controller, + string $method, + string $path, + $controller, ?string $name, - array $middleware, + array $middleware, ?string $domain ): void { @@ -75,4 +75,21 @@ public function findByName(string $name): ?Route { return $this->routes['name'][$name] ?? null; } + + /** + * Index all the defined routes + * + * @return Route[] + */ + public function all(): array + { + $all = []; + foreach ($this->routes['method'] as $group) { + foreach ($group as $route) { + $all[] = $route; + } + } + + return $all; + } } diff --git a/src/Routing/Storekeeper.php b/src/Routing/Storekeeper.php index 0780772..e2271d5 100644 --- a/src/Routing/Storekeeper.php +++ b/src/Routing/Storekeeper.php @@ -69,4 +69,12 @@ public function setState(State $state): void { $this->state = $state; } + + /** + * @return Repository + */ + public function getRepository(): Repository + { + return $this->repository; + } } diff --git a/tests/Features/ContainerTest.php b/tests/Features/ContainerTest.php index 7f9b4fe..dd1af99 100644 --- a/tests/Features/ContainerTest.php +++ b/tests/Features/ContainerTest.php @@ -3,6 +3,7 @@ namespace MiladRahimi\PhpRouter\Tests\Features; use MiladRahimi\PhpContainer\Container; +use MiladRahimi\PhpRouter\Router; use MiladRahimi\PhpRouter\Tests\Common\SampleClass; use MiladRahimi\PhpRouter\Tests\Common\SampleConstructorController; use MiladRahimi\PhpRouter\Tests\Common\SampleInterface; @@ -68,4 +69,20 @@ public function test_binding_and_resolving_with_controller_constructor() $this->assertEquals(SampleClass::class, $this->output($router)); } + + /** + * @throws Throwable + */ + public function test_binding_router_object() + { + $router = $this->router(); + + $router->get('/', function (Router $router) { + return count($router->all()); + }); + + $router->dispatch(); + + $this->assertEquals('1', $this->output($router)); + } }