Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[php-di v7] Optimize compiled string definitions #16

Open
wants to merge 4 commits into
base: mod7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down
18 changes: 18 additions & 0 deletions src/CompiledContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
7 changes: 4 additions & 3 deletions src/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading