21
21
*/
22
22
final class ChainLoader implements LoaderInterface
23
23
{
24
+ /**
25
+ * @var \Traversable<LoaderInterface>|LoaderInterface[]
26
+ */
27
+ private $ loaders ;
28
+
29
+ /**
30
+ * @var array<string, bool>
31
+ */
24
32
private $ hasSourceCache = [];
25
- private $ loaders = [];
26
33
27
34
/**
28
- * @param LoaderInterface[] $loaders
35
+ * @param iterable< LoaderInterface> $loaders
29
36
*/
30
- public function __construct (array $ loaders = [])
37
+ public function __construct (iterable $ loaders = [])
31
38
{
32
- foreach ($ loaders as $ loader ) {
33
- $ this ->addLoader ($ loader );
34
- }
39
+ $ this ->loaders = $ loaders ;
35
40
}
36
41
37
42
public function addLoader (LoaderInterface $ loader ): void
38
43
{
39
- $ this ->loaders [] = $ loader ;
44
+ $ current = $ this ->loaders ;
45
+
46
+ $ this ->loaders = (static function () use ($ current , $ loader ): \Generator {
47
+ yield from $ current ;
48
+ yield $ loader ;
49
+ })();
50
+
40
51
$ this ->hasSourceCache = [];
41
52
}
42
53
@@ -45,13 +56,18 @@ public function addLoader(LoaderInterface $loader): void
45
56
*/
46
57
public function getLoaders (): array
47
58
{
59
+ if (!\is_array ($ this ->loaders )) {
60
+ $ this ->loaders = \iterator_to_array ($ this ->loaders , false );
61
+ }
62
+
48
63
return $ this ->loaders ;
49
64
}
50
65
51
66
public function getSourceContext (string $ name ): Source
52
67
{
53
68
$ exceptions = [];
54
- foreach ($ this ->loaders as $ loader ) {
69
+
70
+ foreach ($ this ->getLoaders () as $ loader ) {
55
71
if (!$ loader ->exists ($ name )) {
56
72
continue ;
57
73
}
@@ -72,7 +88,7 @@ public function exists(string $name): bool
72
88
return $ this ->hasSourceCache [$ name ];
73
89
}
74
90
75
- foreach ($ this ->loaders as $ loader ) {
91
+ foreach ($ this ->getLoaders () as $ loader ) {
76
92
if ($ loader ->exists ($ name )) {
77
93
return $ this ->hasSourceCache [$ name ] = true ;
78
94
}
@@ -84,7 +100,8 @@ public function exists(string $name): bool
84
100
public function getCacheKey (string $ name ): string
85
101
{
86
102
$ exceptions = [];
87
- foreach ($ this ->loaders as $ loader ) {
103
+
104
+ foreach ($ this ->getLoaders () as $ loader ) {
88
105
if (!$ loader ->exists ($ name )) {
89
106
continue ;
90
107
}
@@ -102,7 +119,8 @@ public function getCacheKey(string $name): string
102
119
public function isFresh (string $ name , int $ time ): bool
103
120
{
104
121
$ exceptions = [];
105
- foreach ($ this ->loaders as $ loader ) {
122
+
123
+ foreach ($ this ->getLoaders () as $ loader ) {
106
124
if (!$ loader ->exists ($ name )) {
107
125
continue ;
108
126
}
0 commit comments