Skip to content

Commit 676cda6

Browse files
authored
Merge pull request #43 from miladrahimi/all-routes
add all method
2 parents aa010e2 + a2ae77e commit 676cda6

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

src/Router.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ class Router
6969
* @param Publisher $publisher
7070
*/
7171
public function __construct(
72-
Container $container,
72+
Container $container,
7373
Storekeeper $storekeeper,
74-
Matcher $matcher,
75-
Caller $caller,
76-
Publisher $publisher
74+
Matcher $matcher,
75+
Caller $caller,
76+
Publisher $publisher
7777
)
7878
{
7979
$this->container = $container;
@@ -101,9 +101,9 @@ public static function create(): self
101101

102102
/**
103103
* Setup (enable) View
104+
* @param string $directory
104105
* @link View
105106
*
106-
* @param string $directory
107107
*/
108108
public function setupView(string $directory): void
109109
{
@@ -162,6 +162,16 @@ public function pattern(string $name, string $pattern)
162162
$this->patterns[$name] = $pattern;
163163
}
164164

165+
/**
166+
* Index all the defined routes
167+
*
168+
* @return Route[]
169+
*/
170+
public function all(): array
171+
{
172+
return $this->storekeeper->getRepository()->all();
173+
}
174+
165175
/**
166176
* Define a new route
167177
*

src/Routing/Repository.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class Repository
3030
* @param string|null $domain
3131
*/
3232
public function save(
33-
string $method,
34-
string $path,
35-
$controller,
33+
string $method,
34+
string $path,
35+
$controller,
3636
?string $name,
37-
array $middleware,
37+
array $middleware,
3838
?string $domain
3939
): void
4040
{
@@ -75,4 +75,21 @@ public function findByName(string $name): ?Route
7575
{
7676
return $this->routes['name'][$name] ?? null;
7777
}
78+
79+
/**
80+
* Index all the defined routes
81+
*
82+
* @return Route[]
83+
*/
84+
public function all(): array
85+
{
86+
$all = [];
87+
foreach ($this->routes['method'] as $group) {
88+
foreach ($group as $route) {
89+
$all[] = $route;
90+
}
91+
}
92+
93+
return $all;
94+
}
7895
}

src/Routing/Storekeeper.php

+8
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,12 @@ public function setState(State $state): void
6969
{
7070
$this->state = $state;
7171
}
72+
73+
/**
74+
* @return Repository
75+
*/
76+
public function getRepository(): Repository
77+
{
78+
return $this->repository;
79+
}
7280
}

tests/Features/ContainerTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MiladRahimi\PhpRouter\Tests\Features;
44

55
use MiladRahimi\PhpContainer\Container;
6+
use MiladRahimi\PhpRouter\Router;
67
use MiladRahimi\PhpRouter\Tests\Common\SampleClass;
78
use MiladRahimi\PhpRouter\Tests\Common\SampleConstructorController;
89
use MiladRahimi\PhpRouter\Tests\Common\SampleInterface;
@@ -68,4 +69,20 @@ public function test_binding_and_resolving_with_controller_constructor()
6869

6970
$this->assertEquals(SampleClass::class, $this->output($router));
7071
}
72+
73+
/**
74+
* @throws Throwable
75+
*/
76+
public function test_binding_router_object()
77+
{
78+
$router = $this->router();
79+
80+
$router->get('/', function (Router $router) {
81+
return count($router->all());
82+
});
83+
84+
$router->dispatch();
85+
86+
$this->assertEquals('1', $this->output($router));
87+
}
7188
}

0 commit comments

Comments
 (0)