Last active 1765310259

An example of attaching a delegator factory to the Mezzio RouteCollectorInterface to automate route registration.

ConfigProvider.php Raw
1<?php
2
3namespace SomeModule;
4
5use Laminas\Stdlib\ArrayUtils\MergeRemoveKey;
6use Mezzio\Router\RouteCollectorInterface;
7
8class ConfigProvider
9{
10 public function __invoke(): array
11 {
12 return [
13 'dependencies' => $this->getDependencies(),
14 ];
15 }
16
17 public function getDependencies(): array
18 {
19 return [
20 'aliases' => [
21 RouteCollectorInterface::class => new MergeRemoveKey(),
22 ],
23 'factories' => [
24 RouteCollectorInterface::class => RouteCollectorFactory::class,
25 ],
26 'delegators' => [
27 RouteCollectorInterface::class => [
28 RegisterRoutesFactory::class,
29 ],
30 ],
31 ];
32 }
33}
RegisterRoutesFactory.php Raw
1<?php
2
3namespace SomeModule;
4
5use Psr\Container\ContainerInterface;
6
7class RegisterRoutesFactory
8{
9 public function __invoke(
10 ContainerInterface $container,
11 string $serviceName,
12 callable $factory,
13 ): RouteCollectorInterface {
14 /** @var RouteCollectorInterface $routes */
15 $routes = $factory();
16
17 $routes->get(...)
18
19 return $routes;
20 }
21}
RouteCollector.php Raw
1<?php
2
3declare(strict_types=1);
4
5namespace SomeModule;
6
7use Mezzio\MiddlewareFactory;
8use Mezzio\Router\Route;
9use Mezzio\Router\RouteCollectorInterface;
10use Psr\Http\Server\MiddlewareInterface;
11use Psr\Http\Server\RequestHandlerInterface;
12
13class 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}
RouteCollectorFactory.php Raw
1<?php
2
3namespace SomeModule;
4
5use Mezzio\MiddlewareFactory;
6use Mezzio\Router\RouteCollector as MezzioRouteCollector;
7use Psr\Container\ContainerInterface;
8
9use function assert;
10
11class RouteCollectorFactory
12{
13 public function __invoke(ContainerInterface $container): RouteCollector
14 {
15 $collector = $container->get(MezzioRouteCollector::class);
16 assert($collector instanceof MezzioRouteCollector);
17
18 $factory = $container->get(MiddlewareFactory::class);
19 assert($factory instanceof MiddlewareFactory);
20
21 return new RouteCollector($collector, $factory);
22 }
23}