4
4
5
5
namespace DI \Definition \Source ;
6
6
7
+ use DI \Definition \Definition ;
8
+
7
9
/**
8
10
* Reads DI definitions from files matching glob pattern.
9
11
*/
10
12
class DefinitionGlob implements DefinitionSource
11
13
{
12
- /**
13
- * @var bool
14
- */
15
- private $ initialized = false ;
14
+ private bool $ initialized = false ;
16
15
17
- /**
18
- * Glob pattern to files containing definitions.
19
- * @var string
20
- */
21
- private $ pattern ;
16
+ private ?Autowiring $ autowiring = null ;
22
17
23
- /**
24
- * @var Autowiring
25
- */
26
- private $ autowiring ;
27
-
28
- /**
29
- * @var SourceChain
30
- */
31
- private $ sourceChain ;
18
+ private ?SourceChain $ sourceChain = null ;
32
19
33
20
/**
34
21
* @param string $pattern Glob pattern to files containing definitions
35
22
*/
36
- public function __construct (string $ pattern )
37
- {
38
- $ this ->pattern = $ pattern ;
23
+ public function __construct (
24
+ /**
25
+ * Glob pattern to files containing definitions.
26
+ */
27
+ private string $ pattern
28
+ ) {
39
29
}
40
30
41
- public function setAutowiring (Autowiring $ autowiring )
31
+ public function setAutowiring (Autowiring $ autowiring ): void
42
32
{
43
33
$ this ->autowiring = $ autowiring ;
44
34
}
45
35
46
- public function getDefinition (string $ name, int $ startIndex = 0 )
36
+ public function getDefinition (string $ name): Definition | null
47
37
{
48
38
$ this ->initialize ();
49
39
50
- return $ this ->sourceChain ->getDefinition ($ name, $ startIndex );
40
+ return $ this ->sourceChain ->getDefinition ($ name );
51
41
}
52
42
53
43
public function getDefinitions () : array
@@ -60,18 +50,18 @@ public function getDefinitions() : array
60
50
/**
61
51
* Lazy-loading of the definitions.
62
52
*/
63
- private function initialize ()
53
+ private function initialize (): void
64
54
{
65
55
if ($ this ->initialized === true ) {
66
56
return ;
67
57
}
68
58
69
- $ paths = glob ($ this ->pattern , \GLOB_BRACE );
70
- $ sources = array_map (function ($ path ) {
71
- return new DefinitionFile ($ path , $ this ->autowiring );
72
- }, $ paths );
73
- $ this ->sourceChain = new SourceChain ($ sources );
59
+ // prevent errors due to GLOB_BRACE that does not exist e.g. Alpine Linux
60
+ $ flags = defined ('GLOB_BRACE ' ) ? GLOB_BRACE : 0 ;
61
+ $ paths = glob ($ this ->pattern , $ flags );
62
+ $ sources = array_map (fn (string $ path ) => new DefinitionFile ($ path , $ this ->autowiring ), $ paths );
74
63
64
+ $ this ->sourceChain = new SourceChain ($ sources );
75
65
$ this ->initialized = true ;
76
66
}
77
67
}
0 commit comments