| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
protected array $callbacks = []; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function first() |
| 25 |
{ |
| 26 |
return array_values($this->callbacks)[0]; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function invoke(): void |
| 35 |
{ |
| 36 |
$this->invokeWhen(fn () => true); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 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 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function offsetExists(mixed $offset): bool |
| 98 |
{ |
| 99 |
$this->forgetDuplicates(); |
| 100 |
|
| 101 |
return isset($this->callbacks[$offset]); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function offsetGet(mixed $offset): mixed |
| 111 |
{ |
| 112 |
$this->forgetDuplicates(); |
| 113 |
|
| 114 |
return $this->callbacks[$offset]; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function offsetUnset(mixed $offset): void |
| 140 |
{ |
| 141 |
$this->forgetDuplicates(); |
| 142 |
|
| 143 |
unset($this->callbacks[$offset]); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function count(): int |
| 152 |
{ |
| 153 |
$this->forgetDuplicates(); |
| 154 |
|
| 155 |
return count($this->callbacks); |
| 156 |
} |
| 157 |
} |