Last active 1765310259

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

matthew's Avatar matthew revised this gist 1765310259. Go to revision

1 file changed, 1 insertion, 1 deletion

ConfigProvider.php

@@ -7,7 +7,7 @@ use Mezzio\Router\RouteCollectorInterface;
7 7
8 8 class ConfigProvider
9 9 {
10 - public function getConfig(): array
10 + public function __invoke(): array
11 11 {
12 12 return [
13 13 'dependencies' => $this->getDependencies(),

matthew's Avatar matthew revised this gist 1765308119. Go to revision

3 files changed, 149 insertions

ConfigProvider.php

@@ -2,17 +2,27 @@
2 2
3 3 namespace SomeModule;
4 4
5 + use Laminas\Stdlib\ArrayUtils\MergeRemoveKey;
5 6 use Mezzio\Router\RouteCollectorInterface;
6 7
7 8 class ConfigProvider
8 9 {
9 10 public function getConfig(): array
10 11 {
12 + return [
13 + 'dependencies' => $this->getDependencies(),
14 + ];
11 15 }
12 16
13 17 public function getDependencies(): array
14 18 {
15 19 return [
20 + 'aliases' => [
21 + RouteCollectorInterface::class => new MergeRemoveKey(),
22 + ],
23 + 'factories' => [
24 + RouteCollectorInterface::class => RouteCollectorFactory::class,
25 + ],
16 26 'delegators' => [
17 27 RouteCollectorInterface::class => [
18 28 RegisterRoutesFactory::class,

RouteCollector.php(file created)

@@ -0,0 +1,116 @@
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 + }

RouteCollectorFactory.php(file created)

@@ -0,0 +1,23 @@
1 + <?php
2 +
3 + namespace SomeModule;
4 +
5 + use Mezzio\MiddlewareFactory;
6 + use Mezzio\Router\RouteCollector as MezzioRouteCollector;
7 + use Psr\Container\ContainerInterface;
8 +
9 + use function assert;
10 +
11 + class 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 + }

matthew's Avatar matthew revised this gist 1765307544. Go to revision

2 files changed, 44 insertions

ConfigProvider.php(file created)

@@ -0,0 +1,23 @@
1 + <?php
2 +
3 + namespace SomeModule;
4 +
5 + use Mezzio\Router\RouteCollectorInterface;
6 +
7 + class ConfigProvider
8 + {
9 + public function getConfig(): array
10 + {
11 + }
12 +
13 + public function getDependencies(): array
14 + {
15 + return [
16 + 'delegators' => [
17 + RouteCollectorInterface::class => [
18 + RegisterRoutesFactory::class,
19 + ],
20 + ],
21 + ];
22 + }
23 + }

RegisterRoutesFactory.php(file created)

@@ -0,0 +1,21 @@
1 + <?php
2 +
3 + namespace SomeModule;
4 +
5 + use Psr\Container\ContainerInterface;
6 +
7 + class 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 + }
Newer Older