Skip to content

Commit 7cf29d1

Browse files
committed
php8-mod: DefinitionGlob to specify a pattern for definition files
1 parent 3b59de2 commit 7cf29d1

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

src/Definition/Source/DefinitionGlob.php

+20-30
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,40 @@
44

55
namespace DI\Definition\Source;
66

7+
use DI\Definition\Definition;
8+
79
/**
810
* Reads DI definitions from files matching glob pattern.
911
*/
1012
class DefinitionGlob implements DefinitionSource
1113
{
12-
/**
13-
* @var bool
14-
*/
15-
private $initialized = false;
14+
private bool $initialized = false;
1615

17-
/**
18-
* Glob pattern to files containing definitions.
19-
* @var string
20-
*/
21-
private $pattern;
16+
private ?Autowiring $autowiring = null;
2217

23-
/**
24-
* @var Autowiring
25-
*/
26-
private $autowiring;
27-
28-
/**
29-
* @var SourceChain
30-
*/
31-
private $sourceChain;
18+
private ?SourceChain $sourceChain = null;
3219

3320
/**
3421
* @param string $pattern Glob pattern to files containing definitions
3522
*/
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+
) {
3929
}
4030

41-
public function setAutowiring(Autowiring $autowiring)
31+
public function setAutowiring(Autowiring $autowiring): void
4232
{
4333
$this->autowiring = $autowiring;
4434
}
4535

46-
public function getDefinition(string $name, int $startIndex = 0)
36+
public function getDefinition(string $name): Definition|null
4737
{
4838
$this->initialize();
4939

50-
return $this->sourceChain->getDefinition($name, $startIndex);
40+
return $this->sourceChain->getDefinition($name);
5141
}
5242

5343
public function getDefinitions() : array
@@ -60,18 +50,18 @@ public function getDefinitions() : array
6050
/**
6151
* Lazy-loading of the definitions.
6252
*/
63-
private function initialize()
53+
private function initialize(): void
6454
{
6555
if ($this->initialized === true) {
6656
return;
6757
}
6858

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);
7463

64+
$this->sourceChain = new SourceChain($sources);
7565
$this->initialized = true;
7666
}
7767
}

0 commit comments

Comments
 (0)