From 27cd8b23f6aa0d6c1aa81a2e5133b09b7c4a62d4 Mon Sep 17 00:00:00 2001 From: Michal Kruczek Date: Wed, 10 Jan 2024 11:38:07 +0100 Subject: [PATCH 1/3] 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 ceaf5b42e3be3c70fb44fb4a05fd3e303fb2e249 Mon Sep 17 00:00:00 2001 From: Maciej Holyszko <14310995+falkenhawk@users.noreply.github.com> Date: Sat, 27 Apr 2019 22:23:23 +0200 Subject: [PATCH 2/3] Optimize compiled string definitions - run preg_replace_callback already during compilation --- src/CompiledContainer.php | 18 ++++++++++++++++++ src/Compiler/Compiler.php | 9 ++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/CompiledContainer.php b/src/CompiledContainer.php index 3120600a..78de4b34 100644 --- a/src/CompiledContainer.php +++ b/src/CompiledContainer.php @@ -16,6 +16,7 @@ use Invoker\ParameterResolver\DefaultValueResolver; use Invoker\ParameterResolver\NumericArrayResolver; use Invoker\ParameterResolver\ResolverChain; +use Psr\Container\NotFoundExceptionInterface; /** * Compiled version of the dependency injection container. @@ -112,4 +113,21 @@ protected function resolveFactory($callable, $entryName, array $extraParameters throw new InvalidDefinition("Entry \"$entryName\" cannot be resolved: " . $e->getMessage()); } } + + /** + * Resolve a placeholder in string definition + * - wrap possible NotFound exception to conform to the one from StringDefinition::resolveExpression. + */ + protected function resolveStringPlaceholder($placeholder, $entryName) + { + try { + return $this->delegateContainer->get($placeholder); + } catch (NotFoundExceptionInterface $e) { + throw new DependencyException(sprintf( + "Error while parsing string expression for entry '%s': %s", + $entryName, + $e->getMessage() + ), 0, $e); + } + } } diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php index eeea2403..d906a8ff 100644 --- a/src/Compiler/Compiler.php +++ b/src/Compiler/Compiler.php @@ -214,9 +214,12 @@ private function compileDefinition(string $entryName, Definition $definition) : } break; case $definition instanceof StringDefinition: - $entryName = $this->compileValue($definition->getName()); - $expression = $this->compileValue($definition->getExpression()); - $code = 'return \DI\Definition\StringDefinition::resolveExpression(' . $entryName . ', ' . $expression . ', $this->delegateContainer);'; + $expression = $definition->getExpression(); + $callback = function (array $matches) use ($definition) { + return '\'.$this->resolveStringPlaceholder(' . $this->compileValue($matches[1]) . ', ' . $this->compileValue($definition->getName()) . ').\''; + }; + $value = preg_replace_callback('#\{([^\{\}]+)\}#', $callback, $expression); + $code = 'return \'' . $value . '\';'; break; case $definition instanceof EnvironmentVariableDefinition: $variableName = $this->compileValue($definition->getVariableName()); From 06a8229ccd3d4613e844b16ab9e0d6bd6d9d5b1f Mon Sep 17 00:00:00 2001 From: Michal Kruczek Date: Fri, 13 Jan 2023 16:12:54 +0100 Subject: [PATCH 3/3] porting Optimize compiled string definitions for php-di v7 --- src/CompiledContainer.php | 2 +- src/Compiler/Compiler.php | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/CompiledContainer.php b/src/CompiledContainer.php index 78de4b34..74e23a23 100644 --- a/src/CompiledContainer.php +++ b/src/CompiledContainer.php @@ -118,7 +118,7 @@ protected function resolveFactory($callable, $entryName, array $extraParameters * Resolve a placeholder in string definition * - wrap possible NotFound exception to conform to the one from StringDefinition::resolveExpression. */ - protected function resolveStringPlaceholder($placeholder, $entryName) + protected function resolveStringPlaceholder(string $placeholder, string $entryName) : mixed { try { return $this->delegateContainer->get($placeholder); diff --git a/src/Compiler/Compiler.php b/src/Compiler/Compiler.php index d906a8ff..ff25b3aa 100644 --- a/src/Compiler/Compiler.php +++ b/src/Compiler/Compiler.php @@ -215,9 +215,7 @@ private function compileDefinition(string $entryName, Definition $definition) : break; case $definition instanceof StringDefinition: $expression = $definition->getExpression(); - $callback = function (array $matches) use ($definition) { - return '\'.$this->resolveStringPlaceholder(' . $this->compileValue($matches[1]) . ', ' . $this->compileValue($definition->getName()) . ').\''; - }; + $callback = fn (array $matches) : string => '\'.$this->resolveStringPlaceholder(' . $this->compileValue($matches[1]) . ', ' . $this->compileValue($definition->getName()) . ').\''; $value = preg_replace_callback('#\{([^\{\}]+)\}#', $callback, $expression); $code = 'return \'' . $value . '\';'; break;