From 27cd8b23f6aa0d6c1aa81a2e5133b09b7c4a62d4 Mon Sep 17 00:00:00 2001 From: Michal Kruczek Date: Wed, 10 Jan 2024 11:38:07 +0100 Subject: [PATCH 1/8] feat: php-cs-fixer - removing deprecations 1. Detected deprecations in use: - Rule "braces" is deprecated. Use "single_space_around_construct", "control_structure_braces", "control_structure_continuation_position", "declare_parentheses", "no_multiple_statements_per_line", "curly_braces_position", "statement_indentation" and "no_extra_blank_lines" instead. 2. Detected deprecations in use: - Rule "curly_braces_position" is deprecated. Use "braces_position" instead. --- .php-cs-fixer.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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', ], From 0e0d79094c83b0d3d7d71db029b15c17ef00c156 Mon Sep 17 00:00:00 2001 From: Maciej <14310995+falkenhawk@users.noreply.github.com> Date: Thu, 25 Apr 2019 15:11:15 +0200 Subject: [PATCH 2/8] DefinitionGlob to specify a pattern for definition files - there might be multiple definitions files scattered around in a modularized architecture - instead of listing all the files upfront, specify a glob pattern where to look for the files - directories would be scanned and files read lazily, when a definition is actually requested - to avoid performance hit on init --- src/ContainerBuilder.php | 3 + src/Definition/Source/DefinitionGlob.php | 76 +++++++++++++++++++ .../Definition/Source/DefinitionGlobTest.php | 53 +++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 src/Definition/Source/DefinitionGlob.php create mode 100644 tests/UnitTest/Definition/Source/DefinitionGlobTest.php 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..4948e675 --- /dev/null +++ b/src/Definition/Source/DefinitionGlob.php @@ -0,0 +1,76 @@ +pattern = $pattern; + + parent::__construct([]); + } + + public function setAutowiring(Autowiring $autowiring) + { + $this->autowiring = $autowiring; + } + + public function getDefinition(string $name, int $startIndex = 0) + { + $this->initialize(); + + return parent::getDefinition($name, $startIndex); + } + + public function getDefinitions() : array + { + $this->initialize(); + + return parent::getDefinitions(); + } + + /** + * Lazy-loading of the definitions. + */ + private function initialize() + { + if ($this->initialized === true) { + return; + } + + $paths = glob($this->pattern, GLOB_BRACE); + foreach ($paths as $path) + { + $this->sources[] = new DefinitionFile($path, $this->autowiring); + } + + $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..6f693ac5 --- /dev/null +++ b/tests/UnitTest/Definition/Source/DefinitionGlobTest.php @@ -0,0 +1,53 @@ +getProperty('sources'); + $property->setAccessible(true); + $definitions = $property->getValue($source); + // sources are not initialized (and files are not read) before getting definitions + $this->assertCount(0, $definitions); + + $definitions = $source->getDefinitions(); + $this->assertCount(2, $definitions); + + /** @var ValueDefinition $definition */ + $definition = $definitions['foo']; + $this->assertInstanceOf(ValueDefinition::class, $definition); + $this->assertEquals('bar', $definition->getValue()); + $this->assertInternalType('string', $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); + } +} From 9d6b1dca8f488dc2ba91a5314ccb34047b3b716e Mon Sep 17 00:00:00 2001 From: Maciej Holyszko <14310995+falkenhawk@users.noreply.github.com> Date: Fri, 26 Apr 2019 09:20:17 +0200 Subject: [PATCH 3/8] Prefer composition over inheritance --- src/Definition/Source/DefinitionGlob.php | 29 ++++++++++--------- .../Definition/Source/DefinitionGlobTest.php | 6 ++-- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Definition/Source/DefinitionGlob.php b/src/Definition/Source/DefinitionGlob.php index 4948e675..b0bdcd3a 100644 --- a/src/Definition/Source/DefinitionGlob.php +++ b/src/Definition/Source/DefinitionGlob.php @@ -6,9 +6,8 @@ /** * Reads DI definitions from files matching glob pattern. - * */ -class DefinitionGlob extends SourceChain +class DefinitionGlob implements DefinitionSource { /** * @var bool @@ -16,8 +15,8 @@ class DefinitionGlob extends SourceChain private $initialized = false; /** - * Glob pattern to files containing definitions - * @var string|null + * Glob pattern to files containing definitions. + * @var string */ private $pattern; @@ -26,15 +25,17 @@ class DefinitionGlob extends SourceChain */ private $autowiring; + /** + * @var SourceChain + */ + private $sourceChain; + /** * @param string $pattern Glob pattern to files containing definitions */ - public function __construct($pattern) + public function __construct(string $pattern) { - // Lazy-loading to improve performances $this->pattern = $pattern; - - parent::__construct([]); } public function setAutowiring(Autowiring $autowiring) @@ -46,14 +47,14 @@ public function getDefinition(string $name, int $startIndex = 0) { $this->initialize(); - return parent::getDefinition($name, $startIndex); + return $this->sourceChain->getDefinition($name, $startIndex); } public function getDefinitions() : array { $this->initialize(); - return parent::getDefinitions(); + return $this->sourceChain->getDefinitions(); } /** @@ -66,10 +67,10 @@ private function initialize() } $paths = glob($this->pattern, GLOB_BRACE); - foreach ($paths as $path) - { - $this->sources[] = new DefinitionFile($path, $this->autowiring); - } + $sources = array_map(function ($path) { + return 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 index 6f693ac5..23d95939 100644 --- a/tests/UnitTest/Definition/Source/DefinitionGlobTest.php +++ b/tests/UnitTest/Definition/Source/DefinitionGlobTest.php @@ -23,11 +23,11 @@ public function should_load_definitions_from_glob() $source = new DefinitionGlob($pattern); $class = new ReflectionClass(DefinitionGlob::class); - $property = $class->getProperty('sources'); + $property = $class->getProperty('sourceChain'); $property->setAccessible(true); - $definitions = $property->getValue($source); + $sourceChain = $property->getValue($source); // sources are not initialized (and files are not read) before getting definitions - $this->assertCount(0, $definitions); + $this->assertNull($sourceChain); $definitions = $source->getDefinitions(); $this->assertCount(2, $definitions); From 5b42514c9dafdf616dac75caf514f33cd2942eb4 Mon Sep 17 00:00:00 2001 From: Michal Kruczek Date: Wed, 20 Jan 2021 21:21:07 +0100 Subject: [PATCH 4/8] fix: bump phpunit assertions --- tests/UnitTest/Definition/Source/DefinitionGlobTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/UnitTest/Definition/Source/DefinitionGlobTest.php b/tests/UnitTest/Definition/Source/DefinitionGlobTest.php index 23d95939..1d1385e9 100644 --- a/tests/UnitTest/Definition/Source/DefinitionGlobTest.php +++ b/tests/UnitTest/Definition/Source/DefinitionGlobTest.php @@ -36,7 +36,7 @@ public function should_load_definitions_from_glob() $definition = $definitions['foo']; $this->assertInstanceOf(ValueDefinition::class, $definition); $this->assertEquals('bar', $definition->getValue()); - $this->assertInternalType('string', $definition->getValue()); + $this->assertIsString($definition->getValue()); } /** From ad3db3a5e4d66a74675cf770414533019ab4ddc7 Mon Sep 17 00:00:00 2001 From: Michal Kruczek Date: Thu, 21 Jan 2021 12:04:43 +0100 Subject: [PATCH 5/8] csfix --- src/Definition/Source/DefinitionGlob.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Definition/Source/DefinitionGlob.php b/src/Definition/Source/DefinitionGlob.php index b0bdcd3a..7c3f4669 100644 --- a/src/Definition/Source/DefinitionGlob.php +++ b/src/Definition/Source/DefinitionGlob.php @@ -66,7 +66,7 @@ private function initialize() return; } - $paths = glob($this->pattern, GLOB_BRACE); + $paths = glob($this->pattern, \GLOB_BRACE); $sources = array_map(function ($path) { return new DefinitionFile($path, $this->autowiring); }, $paths); From 735ce80f02049429a1b6d7e6157aa39e7652df99 Mon Sep 17 00:00:00 2001 From: Michal Kruczek Date: Fri, 13 Jan 2023 16:47:05 +0100 Subject: [PATCH 6/8] porting DefinitionGlob to specify a pattern for definition files for php-di v7 --- src/Definition/Source/DefinitionGlob.php | 50 ++++++++++-------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/src/Definition/Source/DefinitionGlob.php b/src/Definition/Source/DefinitionGlob.php index 7c3f4669..d3467b2f 100644 --- a/src/Definition/Source/DefinitionGlob.php +++ b/src/Definition/Source/DefinitionGlob.php @@ -4,50 +4,40 @@ namespace DI\Definition\Source; +use DI\Definition\Definition; + /** * Reads DI definitions from files matching glob pattern. */ class DefinitionGlob implements DefinitionSource { - /** - * @var bool - */ - private $initialized = false; + private bool $initialized = false; - /** - * Glob pattern to files containing definitions. - * @var string - */ - private $pattern; + private ?Autowiring $autowiring = null; - /** - * @var Autowiring - */ - private $autowiring; - - /** - * @var SourceChain - */ - private $sourceChain; + private ?SourceChain $sourceChain = null; /** * @param string $pattern Glob pattern to files containing definitions */ - public function __construct(string $pattern) - { - $this->pattern = $pattern; + public function __construct( + /** + * Glob pattern to files containing definitions. + */ + private string $pattern + ) { } - public function setAutowiring(Autowiring $autowiring) + public function setAutowiring(Autowiring $autowiring): void { $this->autowiring = $autowiring; } - public function getDefinition(string $name, int $startIndex = 0) + public function getDefinition(string $name): Definition|null { $this->initialize(); - return $this->sourceChain->getDefinition($name, $startIndex); + return $this->sourceChain->getDefinition($name); } public function getDefinitions() : array @@ -60,18 +50,18 @@ public function getDefinitions() : array /** * Lazy-loading of the definitions. */ - private function initialize() + private function initialize(): void { if ($this->initialized === true) { return; } - $paths = glob($this->pattern, \GLOB_BRACE); - $sources = array_map(function ($path) { - return new DefinitionFile($path, $this->autowiring); - }, $paths); - $this->sourceChain = new SourceChain($sources); + // 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; } } From ba36ec1e7b619b181d38d38b681feb0fa6b794d7 Mon Sep 17 00:00:00 2001 From: Michal Kruczek Date: Wed, 10 Jan 2024 09:43:32 +0100 Subject: [PATCH 7/8] csfix --- src/Definition/Source/DefinitionGlob.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Definition/Source/DefinitionGlob.php b/src/Definition/Source/DefinitionGlob.php index d3467b2f..ba39c5e6 100644 --- a/src/Definition/Source/DefinitionGlob.php +++ b/src/Definition/Source/DefinitionGlob.php @@ -28,12 +28,12 @@ public function __construct( ) { } - public function setAutowiring(Autowiring $autowiring): void + public function setAutowiring(Autowiring $autowiring) : void { $this->autowiring = $autowiring; } - public function getDefinition(string $name): Definition|null + public function getDefinition(string $name) : Definition|null { $this->initialize(); @@ -50,7 +50,7 @@ public function getDefinitions() : array /** * Lazy-loading of the definitions. */ - private function initialize(): void + private function initialize() : void { if ($this->initialized === true) { return; From e6fdfdea8f1dcbd282d1d278abc7d9faba988576 Mon Sep 17 00:00:00 2001 From: Michal Kruczek Date: Wed, 10 Jan 2024 11:30:03 +0100 Subject: [PATCH 8/8] style: native_constant_invocation --- src/Definition/Source/DefinitionGlob.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Definition/Source/DefinitionGlob.php b/src/Definition/Source/DefinitionGlob.php index ba39c5e6..18bfd87a 100644 --- a/src/Definition/Source/DefinitionGlob.php +++ b/src/Definition/Source/DefinitionGlob.php @@ -57,7 +57,7 @@ private function initialize() : void } // prevent errors due to GLOB_BRACE that does not exist e.g. Alpine Linux - $flags = defined('GLOB_BRACE') ? GLOB_BRACE : 0; + $flags = defined('GLOB_BRACE') ? \GLOB_BRACE : 0; $paths = glob($this->pattern, $flags); $sources = array_map(fn (string $path) => new DefinitionFile($path, $this->autowiring), $paths);