Skip to content

Commit

Permalink
Merge pull request #43 from miladrahimi/all-routes
Browse files Browse the repository at this point in the history
add all method
  • Loading branch information
miladrahimi authored Dec 20, 2021
2 parents aa010e2 + a2ae77e commit 676cda6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
20 changes: 15 additions & 5 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
*
Expand Down
25 changes: 21 additions & 4 deletions src/Routing/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
8 changes: 8 additions & 0 deletions src/Routing/Storekeeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ public function setState(State $state): void
{
$this->state = $state;
}

/**
* @return Repository
*/
public function getRepository(): Repository
{
return $this->repository;
}
}
17 changes: 17 additions & 0 deletions tests/Features/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 676cda6

Please sign in to comment.