|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | namespace Bermuda\Http\Middleware; |
4 | 6 |
|
5 | | -use Psr\Container\ContainerInterface; |
6 | 7 | use Psr\Http\Server\MiddlewareInterface; |
7 | 8 | use Psr\Http\Server\RequestHandlerInterface; |
8 | 9 |
|
9 | | - |
| 10 | +/** |
| 11 | + * Class PipelineFactory |
| 12 | + * |
| 13 | + * Default implementation of PipelineFactoryInterface for creating middleware pipelines. |
| 14 | + * |
| 15 | + * This factory provides a straightforward way to instantiate Pipeline objects, |
| 16 | + * making it easy to inject pipeline creation as a dependency. It can be extended |
| 17 | + * or composed with other services (like DI containers) for more advanced scenarios. |
| 18 | + * |
| 19 | + * Key features: |
| 20 | + * - Simple, stateless factory implementation |
| 21 | + * - No dependencies required |
| 22 | + * - Uses EmptyPipelineHandler as default fallback |
| 23 | + * - Can be easily extended for custom behavior |
| 24 | + * |
| 25 | + * @example Basic usage |
| 26 | + * ```php |
| 27 | + * $factory = new PipelineFactory(); |
| 28 | + * |
| 29 | + * $pipeline = $factory->createMiddlewarePipeline([ |
| 30 | + * new AuthMiddleware(), |
| 31 | + * new LoggingMiddleware(), |
| 32 | + * new RouteMiddleware(), |
| 33 | + * ], new ApplicationHandler()); |
| 34 | + * |
| 35 | + * $response = $pipeline->handle($request); |
| 36 | + * ``` |
| 37 | + * |
| 38 | + * @example Dependency injection |
| 39 | + * ```php |
| 40 | + * class Application |
| 41 | + * { |
| 42 | + * public function __construct( |
| 43 | + * private PipelineFactoryInterface $pipelineFactory |
| 44 | + * ) {} |
| 45 | + * |
| 46 | + * public function createApiPipeline(): PipelineInterface |
| 47 | + * { |
| 48 | + * return $this->pipelineFactory->createMiddlewarePipeline([ |
| 49 | + * new CorsMiddleware(), |
| 50 | + * new JsonMiddleware(), |
| 51 | + * new ApiAuthMiddleware(), |
| 52 | + * ]); |
| 53 | + * } |
| 54 | + * } |
| 55 | + * ``` |
| 56 | + * |
| 57 | + * @example Testing with factory |
| 58 | + * ```php |
| 59 | + * class PipelineTest extends TestCase |
| 60 | + * { |
| 61 | + * private PipelineFactoryInterface $factory; |
| 62 | + * |
| 63 | + * protected function setUp(): void |
| 64 | + * { |
| 65 | + * $this->factory = new PipelineFactory(); |
| 66 | + * } |
| 67 | + * |
| 68 | + * public function testPipelineExecution(): void |
| 69 | + * { |
| 70 | + * $pipeline = $this->factory->createMiddlewarePipeline([ |
| 71 | + * new TestMiddleware(), |
| 72 | + * ], new TestHandler()); |
| 73 | + * |
| 74 | + * // ... test assertions |
| 75 | + * } |
| 76 | + * } |
| 77 | + * ``` |
| 78 | + */ |
10 | 79 | final class PipelineFactory implements PipelineFactoryInterface |
11 | 80 | { |
12 | | - public function createMiddlewarePipeline(iterable $middlewares = [], ?RequestHandlerInterface $fallbackHandler = null): PipelineInterface |
13 | | - { |
14 | | - return new Pipeline($middlewares, $fallbackHandler ?? new EmptyPipelineHandler); |
| 81 | + /** |
| 82 | + * Creates a new middleware pipeline instance. |
| 83 | + * |
| 84 | + * This method constructs a fresh Pipeline with the provided middleware |
| 85 | + * collection and fallback handler. If no fallback handler is specified, |
| 86 | + * the pipeline will use EmptyPipelineHandler, which throws an exception |
| 87 | + * if the empty pipeline is executed. |
| 88 | + * |
| 89 | + * @param iterable<MiddlewareInterface> $middlewares Optional collection of middleware |
| 90 | + * to include in the pipeline. Can be |
| 91 | + * an array, iterator, or generator. |
| 92 | + * @param RequestHandlerInterface|null $fallbackHandler Optional handler to use when the |
| 93 | + * middleware chain completes. If null, |
| 94 | + * EmptyPipelineHandler is used. |
| 95 | + * |
| 96 | + * @return PipelineInterface A new Pipeline instance ready to process requests. |
| 97 | + * |
| 98 | + * @throws \InvalidArgumentException If any middleware in the collection is invalid |
| 99 | + * (thrown by Pipeline constructor). |
| 100 | + * |
| 101 | + * @example Creating an empty pipeline |
| 102 | + * ```php |
| 103 | + * $factory = new PipelineFactory(); |
| 104 | + * $pipeline = $factory->createMiddlewarePipeline(); |
| 105 | + * |
| 106 | + * // Will throw exception when handle() is called without middleware |
| 107 | + * ``` |
| 108 | + * |
| 109 | + * @example Creating a pipeline with middleware |
| 110 | + * ```php |
| 111 | + * $factory = new PipelineFactory(); |
| 112 | + * $pipeline = $factory->createMiddlewarePipeline([ |
| 113 | + * new SecurityHeadersMiddleware(), |
| 114 | + * new CompressionMiddleware(), |
| 115 | + * ], new ApplicationHandler()); |
| 116 | + * |
| 117 | + * $response = $pipeline->handle($request); |
| 118 | + * ``` |
| 119 | + * |
| 120 | + * @example Creating a pipeline from generator |
| 121 | + * ```php |
| 122 | + * $factory = new PipelineFactory(); |
| 123 | + * |
| 124 | + * $middlewares = function() { |
| 125 | + * yield new Middleware1(); |
| 126 | + * yield new Middleware2(); |
| 127 | + * yield new Middleware3(); |
| 128 | + * }; |
| 129 | + * |
| 130 | + * $pipeline = $factory->createMiddlewarePipeline($middlewares()); |
| 131 | + * ``` |
| 132 | + * |
| 133 | + * @example Using with default handler |
| 134 | + * ```php |
| 135 | + * $factory = new PipelineFactory(); |
| 136 | + * |
| 137 | + * // Pipeline will use EmptyPipelineHandler by default |
| 138 | + * $pipeline = $factory->createMiddlewarePipeline([ |
| 139 | + * new Middleware1(), |
| 140 | + * ]); |
| 141 | + * ``` |
| 142 | + */ |
| 143 | + public function createMiddlewarePipeline( |
| 144 | + iterable $middlewares = [], |
| 145 | + ?RequestHandlerInterface $fallbackHandler = null |
| 146 | + ): PipelineInterface { |
| 147 | + return new Pipeline($middlewares, $fallbackHandler ?? new EmptyPipelineHandler()); |
15 | 148 | } |
16 | 149 | } |
0 commit comments