| 1 | <?php
|
| 2 |
|
| 3 | declare(strict_types=1);
|
| 4 |
|
| 5 | namespace SomeModule;
|
| 6 |
|
| 7 | use Mezzio\MiddlewareFactory;
|
| 8 | use Mezzio\Router\Route;
|
| 9 | use Mezzio\Router\RouteCollectorInterface;
|
| 10 | use Psr\Http\Server\MiddlewareInterface;
|
| 11 | use Psr\Http\Server\RequestHandlerInterface;
|
| 12 |
|
| 13 | class RouteCollector implements RouteCollectorInterface
|
| 14 | {
|
| 15 | public function __construct(
|
| 16 | private RouteCollectorInterface $collector,
|
| 17 | private MiddlewareFactory $factory,
|
| 18 | ) {
|
| 19 | }
|
| 20 |
|
| 21 | /**
|
| 22 | * @param non-empty-string $path
|
| 23 | * @param null|list<string> $methods HTTP method to accept; null indicates any.
|
| 24 | * @param null|non-empty-string $name The name of the route.
|
| 25 | */
|
| 26 | public function route(
|
| 27 | string $path,
|
| 28 | string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
|
| 29 | ?array $methods = null,
|
| 30 | ?string $name = null
|
| 31 | ): Route {
|
| 32 | return $this->collector->route($path, $this->factory->prepare($middleware), $methods, $name);
|
| 33 | }
|
| 34 |
|
| 35 | /**
|
| 36 | * @param non-empty-string $path
|
| 37 | * @param null|non-empty-string $name The name of the route.
|
| 38 | */
|
| 39 | public function get(
|
| 40 | string $path,
|
| 41 | string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
|
| 42 | ?string $name = null
|
| 43 | ): Route {
|
| 44 | return $this->collector->get($path, $this->factory->prepare($middleware), $name);
|
| 45 | }
|
| 46 |
|
| 47 | /**
|
| 48 | * @param non-empty-string $path
|
| 49 | * @param null|non-empty-string $name The name of the route.
|
| 50 | */
|
| 51 | public function post(
|
| 52 | string $path,
|
| 53 | string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
|
| 54 | ?string $name = null
|
| 55 | ): Route {
|
| 56 | return $this->collector->post($path, $this->factory->prepare($middleware), $name);
|
| 57 | }
|
| 58 |
|
| 59 | /**
|
| 60 | * @param non-empty-string $path
|
| 61 | * @param null|non-empty-string $name The name of the route.
|
| 62 | */
|
| 63 | public function put(
|
| 64 | string $path,
|
| 65 | string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
|
| 66 | ?string $name = null
|
| 67 | ): Route {
|
| 68 | return $this->collector->put($path, $this->factory->prepare($middleware), $name);
|
| 69 | }
|
| 70 |
|
| 71 | /**
|
| 72 | * @param non-empty-string $path
|
| 73 | * @param null|non-empty-string $name The name of the route.
|
| 74 | */
|
| 75 | public function patch(
|
| 76 | string $path,
|
| 77 | string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
|
| 78 | ?string $name = null
|
| 79 | ): Route {
|
| 80 | return $this->collector->patch($path, $this->factory->prepare($middleware), $name);
|
| 81 | }
|
| 82 |
|
| 83 | /**
|
| 84 | * @param non-empty-string $path
|
| 85 | * @param null|non-empty-string $name The name of the route.
|
| 86 | */
|
| 87 | public function delete(
|
| 88 | string $path,
|
| 89 | string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
|
| 90 | ?string $name = null
|
| 91 | ): Route {
|
| 92 | return $this->collector->delete($path, $this->factory->prepare($middleware), $name);
|
| 93 | }
|
| 94 |
|
| 95 | /**
|
| 96 | * @param non-empty-string $path
|
| 97 | * @param null|non-empty-string $name The name of the route.
|
| 98 | */
|
| 99 | public function any(
|
| 100 | string $path,
|
| 101 | string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
|
| 102 | ?string $name = null
|
| 103 | ): Route {
|
| 104 | return $this->collector->route($path, $this->factory->prepare($middleware), null, $name);
|
| 105 | }
|
| 106 |
|
| 107 | /**
|
| 108 | * Retrieve all directly registered routes with the application.
|
| 109 | *
|
| 110 | * @return list<Route>
|
| 111 | */
|
| 112 | public function getRoutes(): array
|
| 113 | {
|
| 114 | return $this->collector->getRoutes();
|
| 115 | }
|
| 116 | }
|