diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 29ef69ef..33f4467d 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -15,9 +15,14 @@ '@Symfony' => true, '@Symfony:risky' => true, 'array_syntax' => ['syntax' => 'short'], - 'braces' => [ - 'allow_single_line_closure' => true, - ], + 'single_space_around_construct' => true, + 'control_structure_braces' => true, + 'control_structure_continuation_position' => true, + 'declare_parentheses' => true, + 'no_multiple_statements_per_line' => true, + 'braces_position' => true, + 'statement_indentation' => true, + 'no_extra_blank_lines' => true, 'concat_space' => [ 'spacing' => 'one', ], diff --git a/src/ContainerBuilder.php b/src/ContainerBuilder.php index 7a7285c7..2aa61ec3 100644 --- a/src/ContainerBuilder.php +++ b/src/ContainerBuilder.php @@ -8,6 +8,7 @@ use DI\Definition\Source\AttributeBasedAutowiring; use DI\Definition\Source\DefinitionArray; use DI\Definition\Source\DefinitionFile; +use DI\Definition\Source\DefinitionGlob; use DI\Definition\Source\DefinitionSource; use DI\Definition\Source\NoAutowiring; use DI\Definition\Source\ReflectionBasedAutowiring; @@ -116,6 +117,8 @@ public function build() } if (is_array($definitions)) { return new DefinitionArray($definitions, $autowiring); + } elseif ($definitions instanceof DefinitionGlob) { + $definitions->setAutowiring($autowiring); } return $definitions; diff --git a/src/Definition/Source/DefinitionGlob.php b/src/Definition/Source/DefinitionGlob.php new file mode 100644 index 00000000..18bfd87a --- /dev/null +++ b/src/Definition/Source/DefinitionGlob.php @@ -0,0 +1,67 @@ +autowiring = $autowiring; + } + + public function getDefinition(string $name) : Definition|null + { + $this->initialize(); + + return $this->sourceChain->getDefinition($name); + } + + public function getDefinitions() : array + { + $this->initialize(); + + return $this->sourceChain->getDefinitions(); + } + + /** + * Lazy-loading of the definitions. + */ + private function initialize() : void + { + if ($this->initialized === true) { + return; + } + + // prevent errors due to GLOB_BRACE that does not exist e.g. Alpine Linux + $flags = defined('GLOB_BRACE') ? \GLOB_BRACE : 0; + $paths = glob($this->pattern, $flags); + $sources = array_map(fn (string $path) => new DefinitionFile($path, $this->autowiring), $paths); + + $this->sourceChain = new SourceChain($sources); + $this->initialized = true; + } +} diff --git a/tests/UnitTest/Definition/Source/DefinitionGlobTest.php b/tests/UnitTest/Definition/Source/DefinitionGlobTest.php new file mode 100644 index 00000000..1d1385e9 --- /dev/null +++ b/tests/UnitTest/Definition/Source/DefinitionGlobTest.php @@ -0,0 +1,53 @@ +getProperty('sourceChain'); + $property->setAccessible(true); + $sourceChain = $property->getValue($source); + // sources are not initialized (and files are not read) before getting definitions + $this->assertNull($sourceChain); + + $definitions = $source->getDefinitions(); + $this->assertCount(2, $definitions); + + /** @var ValueDefinition $definition */ + $definition = $definitions['foo']; + $this->assertInstanceOf(ValueDefinition::class, $definition); + $this->assertEquals('bar', $definition->getValue()); + $this->assertIsString($definition->getValue()); + } + + /** + * @test + */ + public function empty_definitions_for_pattern_not_matching_any_files() + { + $pattern = __DIR__ . '/*/no-definitions-here.php'; + $source = new DefinitionGlob($pattern); + + $definitions = $source->getDefinitions(); + $this->assertCount(0, $definitions); + } +}