4 * @see https://github.com/laminas/laminas-httphandlerrunner for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-httphandlerrunner/blob/master/COPYRIGHT.md
6 * @license https://github.com/laminas/laminas-httphandlerrunner/blob/master/LICENSE.md New BSD License
9 declare(strict_types=1);
11 namespace Laminas\HttpHandlerRunner\Emitter;
13 use Laminas\HttpHandlerRunner\Exception;
14 use Psr\Http\Message\ResponseInterface;
15 use ReturnTypeWillChange;
19 * Provides an EmitterInterface implementation that acts as a stack of Emitters.
21 * The implementations emit() method iterates itself.
23 * When iterating the stack, the first emitter to return a boolean
24 * true value will short-circuit iteration.
26 class EmitterStack extends SplStack implements EmitterInterface
31 * Loops through the stack, calling emit() on each; any that return a
32 * boolean true value will short-circuit, skipping any remaining emitters
35 * As such, return a boolean false value from an emitter to indicate it
36 * cannot emit the response, allowing the next emitter to try.
38 public function emit(ResponseInterface $response) : bool
40 foreach ($this as $emitter) {
41 if (false !== $emitter->emit($response)) {
50 * Set an emitter on the stack by index.
53 * @param EmitterInterface $emitter
55 * @throws InvalidArgumentException if not an EmitterInterface instance
57 #[ReturnTypeWillChange]
58 public function offsetSet($index, $emitter)
60 $this->validateEmitter($emitter);
61 parent::offsetSet($index, $emitter);
65 * Push an emitter to the stack.
67 * @param EmitterInterface $emitter
69 * @throws InvalidArgumentException if not an EmitterInterface instance
71 #[ReturnTypeWillChange]
72 public function push($emitter)
74 $this->validateEmitter($emitter);
75 parent::push($emitter);
79 * Unshift an emitter to the stack.
81 * @param EmitterInterface $emitter
83 * @throws InvalidArgumentException if not an EmitterInterface instance
85 #[ReturnTypeWillChange]
86 public function unshift($emitter)
88 $this->validateEmitter($emitter);
89 parent::unshift($emitter);
93 * Validate that an emitter implements EmitterInterface.
95 * @param mixed $emitter
96 * @throws Exception\InvalidEmitterException for non-emitter instances
98 private function validateEmitter($emitter) : void
100 if (! $emitter instanceof EmitterInterface) {
101 throw Exception\InvalidEmitterException::forEmitter($emitter);