|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Bermuda\Http\Middleware; |
4 | 6 |
|
5 | 7 | use Psr\Http\Server\MiddlewareInterface; |
6 | 8 | use Psr\Http\Server\RequestHandlerInterface; |
7 | 9 |
|
| 10 | +/** |
| 11 | + * Interface PipelineFactoryInterface |
| 12 | + * |
| 13 | + * Factory interface for creating middleware pipeline instances. |
| 14 | + * |
| 15 | + * This interface defines a standard way to instantiate middleware pipelines, |
| 16 | + * allowing for dependency injection and easier testing. Implementations can |
| 17 | + * provide custom pipeline configurations, middleware resolution from containers, |
| 18 | + * or apply default middleware to all created pipelines. |
| 19 | + * |
| 20 | + * @example Basic factory usage |
| 21 | + * ```php |
| 22 | + * class PipelineFactory implements PipelineFactoryInterface |
| 23 | + * { |
| 24 | + * public function createMiddlewarePipeline( |
| 25 | + * iterable $middlewares = [], |
| 26 | + * ?RequestHandlerInterface $fallbackHandler = null |
| 27 | + * ): PipelineInterface { |
| 28 | + * return new Pipeline($middlewares, $fallbackHandler ?? new DefaultHandler()); |
| 29 | + * } |
| 30 | + * } |
| 31 | + * |
| 32 | + * $factory = new PipelineFactory(); |
| 33 | + * $pipeline = $factory->createMiddlewarePipeline([ |
| 34 | + * new AuthMiddleware(), |
| 35 | + * new LoggingMiddleware(), |
| 36 | + * ]); |
| 37 | + * ``` |
| 38 | + * |
| 39 | + * @example Factory with container integration |
| 40 | + * ```php |
| 41 | + * class ContainerAwarePipelineFactory implements PipelineFactoryInterface |
| 42 | + * { |
| 43 | + * public function __construct(private ContainerInterface $container) {} |
| 44 | + * |
| 45 | + * public function createMiddlewarePipeline( |
| 46 | + * iterable $middlewares = [], |
| 47 | + * ?RequestHandlerInterface $fallbackHandler = null |
| 48 | + * ): PipelineInterface { |
| 49 | + * $resolved = []; |
| 50 | + * foreach ($middlewares as $middleware) { |
| 51 | + * $resolved[] = is_string($middleware) |
| 52 | + * ? $this->container->get($middleware) |
| 53 | + * : $middleware; |
| 54 | + * } |
| 55 | + * |
| 56 | + * return new Pipeline($resolved, $fallbackHandler); |
| 57 | + * } |
| 58 | + * } |
| 59 | + * ``` |
| 60 | + * |
| 61 | + * @example Factory with default middleware |
| 62 | + * ```php |
| 63 | + * class ApiPipelineFactory implements PipelineFactoryInterface |
| 64 | + * { |
| 65 | + * public function createMiddlewarePipeline( |
| 66 | + * iterable $middlewares = [], |
| 67 | + * ?RequestHandlerInterface $fallbackHandler = null |
| 68 | + * ): PipelineInterface { |
| 69 | + * // Always prepend CORS and error handling for API routes |
| 70 | + * $defaultMiddlewares = [ |
| 71 | + * new CorsMiddleware(), |
| 72 | + * new ErrorHandlerMiddleware(), |
| 73 | + * ]; |
| 74 | + * |
| 75 | + * return new Pipeline( |
| 76 | + * array_merge($defaultMiddlewares, iterator_to_array($middlewares)), |
| 77 | + * $fallbackHandler |
| 78 | + * ); |
| 79 | + * } |
| 80 | + * } |
| 81 | + * ``` |
| 82 | + */ |
8 | 83 | interface PipelineFactoryInterface |
9 | 84 | { |
10 | | - public function createMiddlewarePipeline(iterable $middlewares = [], ?RequestHandlerInterface $fallbackHandler = null): PipelineInterface ; |
| 85 | + /** |
| 86 | + * Creates a new middleware pipeline instance. |
| 87 | + * |
| 88 | + * This method constructs a fresh PipelineInterface instance with the specified |
| 89 | + * middleware collection and fallback handler. The factory may apply additional |
| 90 | + * configuration, resolve middleware from a container, or add default middleware |
| 91 | + * depending on the implementation. |
| 92 | + * |
| 93 | + * @param iterable<MiddlewareInterface> $middlewares Optional collection of middleware |
| 94 | + * to include in the pipeline. Can be |
| 95 | + * an array, iterator, or generator. |
| 96 | + * @param RequestHandlerInterface|null $fallbackHandler Optional handler to use when the |
| 97 | + * middleware chain completes. If null, |
| 98 | + * the implementation should provide |
| 99 | + * a sensible default. |
| 100 | + * |
| 101 | + * @return PipelineInterface A new pipeline instance ready to process requests. |
| 102 | + * |
| 103 | + * @throws \InvalidArgumentException If any middleware in the collection is invalid. |
| 104 | + * |
| 105 | + * @example Creating an empty pipeline |
| 106 | + * ```php |
| 107 | + * $pipeline = $factory->createMiddlewarePipeline(); |
| 108 | + * ``` |
| 109 | + * |
| 110 | + * @example Creating a pipeline with middleware |
| 111 | + * ```php |
| 112 | + * $pipeline = $factory->createMiddlewarePipeline([ |
| 113 | + * new SecurityHeadersMiddleware(), |
| 114 | + * new CompressionMiddleware(), |
| 115 | + * ]); |
| 116 | + * ``` |
| 117 | + * |
| 118 | + * @example Creating a pipeline with custom handler |
| 119 | + * ```php |
| 120 | + * $pipeline = $factory->createMiddlewarePipeline( |
| 121 | + * [$middleware1, $middleware2], |
| 122 | + * new CustomApplicationHandler() |
| 123 | + * ); |
| 124 | + * ``` |
| 125 | + */ |
| 126 | + public function createMiddlewarePipeline( |
| 127 | + iterable $middlewares = [], |
| 128 | + ?RequestHandlerInterface $fallbackHandler = null |
| 129 | + ): PipelineInterface; |
11 | 130 | } |
0 commit comments