1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace UnicornFail\Emoji\Traits; |
6
|
|
|
|
7
|
|
|
use UnicornFail\Emoji\Extension\ExtensionInterface; |
8
|
|
|
|
9
|
|
|
trait ExtensibleEnvironmentTrait |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ExtensionInterface[] |
13
|
|
|
* |
14
|
|
|
* @psalm-readonly-allow-private-mutation |
15
|
|
|
*/ |
16
|
|
|
private $extensions = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
* |
21
|
|
|
* @psalm-readonly-allow-private-mutation |
22
|
|
|
*/ |
23
|
|
|
private $extensionsInitialized = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ExtensionInterface[] |
27
|
|
|
* |
28
|
|
|
* @psalm-readonly-allow-private-mutation |
29
|
|
|
*/ |
30
|
|
|
private $uninitializedExtensions = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return iterable<ExtensionInterface> |
34
|
|
|
*/ |
35
|
|
|
protected static function defaultExtensions(): iterable |
36
|
|
|
{ |
37
|
|
|
return []; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
public function addExtension(ExtensionInterface $extension) |
44
|
|
|
{ |
45
|
|
|
$this->assertUninitialized('Failed to add extension.'); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->extensions[] = $extension; |
48
|
|
|
$this->uninitializedExtensions[] = $extension; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
* |
56
|
|
|
* @return ExtensionInterface[] |
57
|
|
|
*/ |
58
|
|
|
public function getExtensions(): iterable |
59
|
|
|
{ |
60
|
|
|
return $this->extensions; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function initializeExtensions(): void |
64
|
|
|
{ |
65
|
|
|
if ($this->extensionsInitialized) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Ask all extensions to register their components. |
70
|
|
|
while (\count($this->uninitializedExtensions) > 0) { |
71
|
|
|
foreach ($this->uninitializedExtensions as $i => $extension) { |
72
|
|
|
$extension->register($this); |
|
|
|
|
73
|
|
|
unset($this->uninitializedExtensions[$i]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->extensionsInitialized = true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|