1313 * (as a RequestHandlerInterface) and acting as an individual middleware component (as a MiddlewareInterface).
1414 * Additionally, it supports counting (via Countable) and iteration (via IteratorAggregate) over its middleware components.
1515 *
16- * This interface provides methods to:
17- * - Add one or more middleware using pipe().
18- * - Check if a specific middleware exists in the pipeline.
19- * - Determine if the pipeline has no middleware.
16+ * The pipeline maintains a sequence of PSR-15 middleware and processes HTTP requests through them.
17+ * This allows for flexible composition of middleware chains.
18+ *
19+ * Key features:
20+ * - Immutability: Methods like pipe() return new instances
21+ * - Composability: Pipelines can contain other pipelines
22+ * - Flexibility: Can act as both a handler and middleware
23+ * - PSR-15 compliant
24+ *
25+ * @example Creating and using a pipeline
26+ * ```php
27+ * $pipeline = new Pipeline([
28+ * new AuthenticationMiddleware(),
29+ * new LoggingMiddleware(),
30+ * new RateLimitMiddleware(),
31+ * ], $applicationHandler);
32+ *
33+ * $response = $pipeline->handle($request);
34+ * ```
35+ *
36+ * @example Pipeline composition
37+ * ```php
38+ * $apiPipeline = new Pipeline([
39+ * new JsonMiddleware(),
40+ * new ValidationMiddleware(),
41+ * ]);
42+ *
43+ * $mainPipeline = new Pipeline([
44+ * new CorsMiddleware(),
45+ * $apiPipeline, // Pipeline as middleware
46+ * ], $handler);
47+ * ```
48+ *
49+ * @example Adding middleware dynamically
50+ * ```php
51+ * $pipeline = $basePipeline->pipe([
52+ * new CacheMiddleware(),
53+ * new CompressionMiddleware(),
54+ * ]);
55+ * ```
2056 */
2157interface PipelineInterface extends MiddlewareInterface, RequestHandlerInterface, \Countable, \IteratorAggregate
2258{
@@ -30,6 +66,22 @@ interface PipelineInterface extends MiddlewareInterface, RequestHandlerInterface
3066 * @param bool $prepend If true, adds the middleware at the beginning of the pipeline; otherwise, they are appended.
3167 *
3268 * @return PipelineInterface A new pipeline instance that includes the added middleware.
69+ *
70+ * @throws \InvalidArgumentException If any middleware doesn't implement MiddlewareInterface.
71+ * @throws \RuntimeException If circular reference is detected.
72+ *
73+ * @example Appending middleware
74+ * ```php
75+ * $pipeline = $pipeline->pipe([
76+ * new CacheMiddleware(),
77+ * new MetricsMiddleware(),
78+ * ]);
79+ * ```
80+ *
81+ * @example Prepending middleware (execute first)
82+ * ```php
83+ * $pipeline = $pipeline->pipe(new SecurityHeadersMiddleware(), prepend: true);
84+ * ```
3385 */
3486 public function pipe (iterable |MiddlewareInterface $ middlewares , bool $ prepend = false ): PipelineInterface ;
3587
@@ -39,6 +91,13 @@ public function pipe(iterable|MiddlewareInterface $middlewares, bool $prepend =
3991 * This method enables external code to iterate over the middleware in the pipeline (e.g., using foreach).
4092 *
4193 * @return Traversable<MiddlewareInterface> A traversable that yields each middleware in the pipeline.
94+ *
95+ * @example
96+ * ```php
97+ * foreach ($pipeline as $middleware) {
98+ * echo get_class($middleware) . "\n";
99+ * }
100+ * ```
42101 */
43102 public function getIterator (): Traversable ;
44103
@@ -50,6 +109,21 @@ public function getIterator(): Traversable;
50109 * @template T of MiddlewareInterface
51110 * @param MiddlewareInterface|class-string<T> $middleware The middleware instance or fully-qualified class name to search for.
52111 * @return bool Returns true if the middleware exists in the pipeline; false otherwise.
112+ *
113+ * @example Check by class name
114+ * ```php
115+ * if ($pipeline->has(AuthMiddleware::class)) {
116+ * // Pipeline includes authentication middleware
117+ * }
118+ * ```
119+ *
120+ * @example Check by instance
121+ * ```php
122+ * $cache = new CacheMiddleware();
123+ * if ($pipeline->has($cache)) {
124+ * // This specific instance is in the pipeline
125+ * }
126+ * ```
53127 */
54128 public function has (MiddlewareInterface |string $ middleware ): bool ;
55129
@@ -60,6 +134,13 @@ public function has(MiddlewareInterface|string $middleware): bool;
60134 * indicating that the pipeline would immediately fall back to using the fallback handler if invoked.
61135 *
62136 * @return bool True if the pipeline is empty; false otherwise.
137+ *
138+ * @example
139+ * ```php
140+ * if ($pipeline->isEmpty()) {
141+ * // Request will go directly to the fallback handler
142+ * }
143+ * ```
63144 */
64145 public function isEmpty (): bool ;
65146}
0 commit comments