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;
18 * Provides an EmitterInterface implementation that acts as a stack of Emitters.
20 * The implementations emit() method iterates itself.
22 * When iterating the stack, the first emitter to return a boolean
23 * true value will short-circuit iteration.
25 class EmitterStack extends SplStack implements EmitterInterface
30 * Loops through the stack, calling emit() on each; any that return a
31 * boolean true value will short-circuit, skipping any remaining emitters
34 * As such, return a boolean false value from an emitter to indicate it
35 * cannot emit the response, allowing the next emitter to try.
37 public function emit(ResponseInterface $response) : bool
39 foreach ($this as $emitter) {
40 if (false !== $emitter->emit($response)) {
49 * Set an emitter on the stack by index.
52 * @param EmitterInterface $emitter
54 * @throws InvalidArgumentException if not an EmitterInterface instance
56 public function offsetSet($index, $emitter)
58 $this->validateEmitter($emitter);
59 parent::offsetSet($index, $emitter);
63 * Push an emitter to the stack.
65 * @param EmitterInterface $emitter
67 * @throws InvalidArgumentException if not an EmitterInterface instance
69 public function push($emitter)
71 $this->validateEmitter($emitter);
72 parent::push($emitter);
76 * Unshift an emitter to the stack.
78 * @param EmitterInterface $emitter
80 * @throws InvalidArgumentException if not an EmitterInterface instance
82 public function unshift($emitter)
84 $this->validateEmitter($emitter);
85 parent::unshift($emitter);
89 * Validate that an emitter implements EmitterInterface.
91 * @param mixed $emitter
92 * @throws Exception\InvalidEmitterException for non-emitter instances
94 private function validateEmitter($emitter) : void
96 if (! $emitter instanceof EmitterInterface) {
97 throw Exception\InvalidEmitterException::forEmitter($emitter);