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/CompiledContainer.php b/src/CompiledContainer.php index 3120600a..74e23a23 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(string $placeholder, string $entryName) : mixed + { + 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..ff25b3aa 100644 --- a/src/Compiler/Compiler.php +++ b/src/Compiler/Compiler.php @@ -214,9 +214,10 @@ 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 = fn (array $matches) : string => '\'.$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());