3c0d85fbee542d3d22b4c4fa236df0cc13d170ec
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 /**
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
7 */
8
9 declare(strict_types=1);
10
11 namespace Laminas\HttpHandlerRunner\Emitter;
12
13 use Laminas\HttpHandlerRunner\Exception;
14 use Psr\Http\Message\ResponseInterface;
15 use SplStack;
16
17 /**
18 * Provides an EmitterInterface implementation that acts as a stack of Emitters.
19 *
20 * The implementations emit() method iterates itself.
21 *
22 * When iterating the stack, the first emitter to return a boolean
23 * true value will short-circuit iteration.
24 */
25 class EmitterStack extends SplStack implements EmitterInterface
26 {
27 /**
28 * Emit a response
29 *
30 * Loops through the stack, calling emit() on each; any that return a
31 * boolean true value will short-circuit, skipping any remaining emitters
32 * in the stack.
33 *
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.
36 */
37 public function emit(ResponseInterface $response) : bool
38 {
39 foreach ($this as $emitter) {
40 if (false !== $emitter->emit($response)) {
41 return true;
42 }
43 }
44
45 return false;
46 }
47
48 /**
49 * Set an emitter on the stack by index.
50 *
51 * @param mixed $index
52 * @param EmitterInterface $emitter
53 * @return void
54 * @throws InvalidArgumentException if not an EmitterInterface instance
55 */
56 public function offsetSet($index, $emitter)
57 {
58 $this->validateEmitter($emitter);
59 parent::offsetSet($index, $emitter);
60 }
61
62 /**
63 * Push an emitter to the stack.
64 *
65 * @param EmitterInterface $emitter
66 * @return void
67 * @throws InvalidArgumentException if not an EmitterInterface instance
68 */
69 public function push($emitter)
70 {
71 $this->validateEmitter($emitter);
72 parent::push($emitter);
73 }
74
75 /**
76 * Unshift an emitter to the stack.
77 *
78 * @param EmitterInterface $emitter
79 * @return void
80 * @throws InvalidArgumentException if not an EmitterInterface instance
81 */
82 public function unshift($emitter)
83 {
84 $this->validateEmitter($emitter);
85 parent::unshift($emitter);
86 }
87
88 /**
89 * Validate that an emitter implements EmitterInterface.
90 *
91 * @param mixed $emitter
92 * @throws Exception\InvalidEmitterException for non-emitter instances
93 */
94 private function validateEmitter($emitter) : void
95 {
96 if (! $emitter instanceof EmitterInterface) {
97 throw Exception\InvalidEmitterException::forEmitter($emitter);
98 }
99 }
100 }