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