3 declare(strict_types=1);
5 namespace Laminas\HttpHandlerRunner\Emitter;
7 use Laminas\HttpHandlerRunner\Exception;
8 use Psr\Http\Message\ResponseInterface;
9 use ReturnTypeWillChange;
13 * Provides an EmitterInterface implementation that acts as a stack of Emitters.
15 * The implementations emit() method iterates itself.
17 * When iterating the stack, the first emitter to return a boolean
18 * true value will short-circuit iteration.
20 * @template-extends SplStack<EmitterInterface>
22 class EmitterStack extends SplStack implements EmitterInterface
27 * Loops through the stack, calling emit() on each; any that return a
28 * boolean true value will short-circuit, skipping any remaining emitters
31 * As such, return a boolean false value from an emitter to indicate it
32 * cannot emit the response, allowing the next emitter to try.
34 public function emit(ResponseInterface $response): bool
36 foreach ($this as $emitter) {
37 if (false !== $emitter->emit($response)) {
46 * Set an emitter on the stack by index.
49 * @param EmitterInterface $emitter
51 * @throws Exception\InvalidEmitterException If not an EmitterInterface instance.
53 #[ReturnTypeWillChange]
54 public function offsetSet($index, $emitter)
56 /** @psalm-suppress RedundantConditionGivenDocblockType */
57 $this->validateEmitter($emitter);
58 parent::offsetSet($index, $emitter);
62 * Push an emitter to the stack.
64 * @param EmitterInterface $emitter
66 * @throws Exception\InvalidEmitterException If not an EmitterInterface instance.
68 #[ReturnTypeWillChange]
69 public function push($emitter)
71 /** @psalm-suppress RedundantConditionGivenDocblockType */
72 $this->validateEmitter($emitter);
73 parent::push($emitter);
77 * Unshift an emitter to the stack.
79 * @param EmitterInterface $emitter
81 * @throws Exception\InvalidEmitterException If not an EmitterInterface instance.
83 #[ReturnTypeWillChange]
84 public function unshift($emitter)
86 /** @psalm-suppress RedundantConditionGivenDocblockType */
87 $this->validateEmitter($emitter);
88 parent::unshift($emitter);
92 * Validate that an emitter implements EmitterInterface.
94 * @throws Exception\InvalidEmitterException For non-emitter instances.
95 * @psalm-assert EmitterInterface $emitter
97 private function validateEmitter(mixed $emitter): void
99 if (! $emitter instanceof EmitterInterface) {
100 throw Exception\InvalidEmitterException::forEmitter($emitter);