ConfigProvider.php
· 772 B · PHP
Raw
<?php
namespace SomeModule;
use Laminas\Stdlib\ArrayUtils\MergeRemoveKey;
use Mezzio\Router\RouteCollectorInterface;
class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
];
}
public function getDependencies(): array
{
return [
'aliases' => [
RouteCollectorInterface::class => new MergeRemoveKey(),
],
'factories' => [
RouteCollectorInterface::class => RouteCollectorFactory::class,
],
'delegators' => [
RouteCollectorInterface::class => [
RegisterRoutesFactory::class,
],
],
];
}
}
| 1 | <?php |
| 2 | |
| 3 | namespace SomeModule; |
| 4 | |
| 5 | use Laminas\Stdlib\ArrayUtils\MergeRemoveKey; |
| 6 | use Mezzio\Router\RouteCollectorInterface; |
| 7 | |
| 8 | class 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
· 398 B · PHP
Raw
<?php
namespace SomeModule;
use Psr\Container\ContainerInterface;
class RegisterRoutesFactory
{
public function __invoke(
ContainerInterface $container,
string $serviceName,
callable $factory,
): RouteCollectorInterface {
/** @var RouteCollectorInterface $routes */
$routes = $factory();
$routes->get(...)
return $routes;
}
}
| 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 | } |
RouteCollector.php
· 3.5 KiB · PHP
Raw
<?php
declare(strict_types=1);
namespace SomeModule;
use Mezzio\MiddlewareFactory;
use Mezzio\Router\Route;
use Mezzio\Router\RouteCollectorInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class RouteCollector implements RouteCollectorInterface
{
public function __construct(
private RouteCollectorInterface $collector,
private MiddlewareFactory $factory,
) {
}
/**
* @param non-empty-string $path
* @param null|list<string> $methods HTTP method to accept; null indicates any.
* @param null|non-empty-string $name The name of the route.
*/
public function route(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?array $methods = null,
?string $name = null
): Route {
return $this->collector->route($path, $this->factory->prepare($middleware), $methods, $name);
}
/**
* @param non-empty-string $path
* @param null|non-empty-string $name The name of the route.
*/
public function get(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?string $name = null
): Route {
return $this->collector->get($path, $this->factory->prepare($middleware), $name);
}
/**
* @param non-empty-string $path
* @param null|non-empty-string $name The name of the route.
*/
public function post(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?string $name = null
): Route {
return $this->collector->post($path, $this->factory->prepare($middleware), $name);
}
/**
* @param non-empty-string $path
* @param null|non-empty-string $name The name of the route.
*/
public function put(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?string $name = null
): Route {
return $this->collector->put($path, $this->factory->prepare($middleware), $name);
}
/**
* @param non-empty-string $path
* @param null|non-empty-string $name The name of the route.
*/
public function patch(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?string $name = null
): Route {
return $this->collector->patch($path, $this->factory->prepare($middleware), $name);
}
/**
* @param non-empty-string $path
* @param null|non-empty-string $name The name of the route.
*/
public function delete(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?string $name = null
): Route {
return $this->collector->delete($path, $this->factory->prepare($middleware), $name);
}
/**
* @param non-empty-string $path
* @param null|non-empty-string $name The name of the route.
*/
public function any(
string $path,
string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware,
?string $name = null
): Route {
return $this->collector->route($path, $this->factory->prepare($middleware), null, $name);
}
/**
* Retrieve all directly registered routes with the application.
*
* @return list<Route>
*/
public function getRoutes(): array
{
return $this->collector->getRoutes();
}
}
| 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
· 601 B · PHP
Raw
<?php
namespace SomeModule;
use Mezzio\MiddlewareFactory;
use Mezzio\Router\RouteCollector as MezzioRouteCollector;
use Psr\Container\ContainerInterface;
use function assert;
class RouteCollectorFactory
{
public function __invoke(ContainerInterface $container): RouteCollector
{
$collector = $container->get(MezzioRouteCollector::class);
assert($collector instanceof MezzioRouteCollector);
$factory = $container->get(MiddlewareFactory::class);
assert($factory instanceof MiddlewareFactory);
return new RouteCollector($collector, $factory);
}
}
| 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 | } |