Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Defer/DeferredCallbackCollection.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support\Defer;
4
5 use ArrayAccess;
6 use Closure;
7 use Countable;
8 use Illuminate\Support\Collection;
9
10 class DeferredCallbackCollection implements ArrayAccess, Countable
11 {
12 /**
13 * All of the deferred callbacks.
14 *
15 * @var array
16 */
17 protected array $callbacks = [];
18
19 /**
20 * Get the first callback in the collection.
21 *
22 * @return callable
23 */
24 public function first()
25 {
26 return array_values($this->callbacks)[0];
27 }
28
29 /**
30 * Invoke the deferred callbacks.
31 *
32 * @return void
33 */
34 public function invoke(): void
35 {
36 $this->invokeWhen(fn () => true);
37 }
38
39 /**
40 * Invoke the deferred callbacks if the given truth test evaluates to true.
41 *
42 * @param \Closure|null $when
43 * @return void
44 */
45 public function invokeWhen(?Closure $when = null): void
46 {
47 $when ??= fn () => true;
48
49 $this->forgetDuplicates();
50
51 foreach ($this->callbacks as $index => $callback) {
52 if ($when($callback)) {
53 rescue($callback);
54 }
55
56 unset($this->callbacks[$index]);
57 }
58 }
59
60 /**
61 * Remove any deferred callbacks with the given name.
62 *
63 * @param string $name
64 * @return void
65 */
66 public function forget(string $name): void
67 {
68 $this->callbacks = (new Collection($this->callbacks))
69 ->reject(fn ($callback) => $callback->name === $name)
70 ->values()
71 ->all();
72 }
73
74 /**
75 * Remove any duplicate callbacks.
76 *
77 * @return $this
78 */
79 protected function forgetDuplicates(): static
80 {
81 $this->callbacks = (new Collection($this->callbacks))
82 ->reverse()
83 ->unique(fn ($c) => $c->name)
84 ->reverse()
85 ->values()
86 ->all();
87
88 return $this;
89 }
90
91 /**
92 * Determine if the collection has a callback with the given key.
93 *
94 * @param mixed $offset
95 * @return bool
96 */
97 public function offsetExists(mixed $offset): bool
98 {
99 $this->forgetDuplicates();
100
101 return isset($this->callbacks[$offset]);
102 }
103
104 /**
105 * Get the callback with the given key.
106 *
107 * @param mixed $offset
108 * @return mixed
109 */
110 public function offsetGet(mixed $offset): mixed
111 {
112 $this->forgetDuplicates();
113
114 return $this->callbacks[$offset];
115 }
116
117 /**
118 * Set the callback with the given key.
119 *
120 * @param mixed $offset
121 * @param mixed $value
122 * @return void
123 */
124 public function offsetSet(mixed $offset, mixed $value): void
125 {
126 if (is_null($offset)) {
127 $this->callbacks[] = $value;
128 } else {
129 $this->callbacks[$offset] = $value;
130 }
131 }
132
133 /**
134 * Remove the callback with the given key from the collection.
135 *
136 * @param mixed $offset
137 * @return void
138 */
139 public function offsetUnset(mixed $offset): void
140 {
141 $this->forgetDuplicates();
142
143 unset($this->callbacks[$offset]);
144 }
145
146 /**
147 * Determine how many callbacks are in the collection.
148 *
149 * @return int
150 */
151 public function count(): int
152 {
153 $this->forgetDuplicates();
154
155 return count($this->callbacks);
156 }
157 }