bug #10536 Avoid levenshtein comparison when using ContainerBuilder. (catch56)

This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #10536).

Discussion
----------

Avoid levenshtein comparison when using ContainerBuilder.

[DependencyInjection] ContainerBuilder catches exceptions when a service isn't found by Container::get().

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

In the process of building the exception message, I saw over 13k calls to levenshtein() when profiling a Drupal request where there's no compiled container yet, this adds around 36ms+ to the request for the function calls themselves, more for the overall logic.

Made the levenshtein calls optional based on a property on the class, then ContainerBuilder can set that to false and skip the suggestions.

![lev-before](https://f.cloud.github.com/assets/116285/2525110/d6dd3a90-b4eb-11e3-934a-a3726e0f8f09.png)
![lev-after](https://f.cloud.github.com/assets/116285/2525114/dd2de0fc-b4eb-11e3-8df0-598cadf2286b.png)

Commits
-------

cc9cc37 Avoid levenshtein comparison when using ContainerBuilder.
This commit is contained in:
Fabien Potencier 2014-03-27 19:13:32 +01:00
commit 128ca44437

View File

@ -461,52 +461,44 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
{
$id = strtolower($id);
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
return $service;
}
if (isset($this->loading[$id])) {
throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id), 0, $e);
}
if (!$this->hasDefinition($id) && isset($this->aliasDefinitions[$id])) {
return $this->get($this->aliasDefinitions[$id]);
}
try {
return parent::get($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
} catch (InactiveScopeException $e) {
$definition = $this->getDefinition($id);
} catch (InvalidArgumentException $e) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return null;
}
throw $e;
} catch (InvalidArgumentException $e) {
if (isset($this->loading[$id])) {
throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id), 0, $e);
}
}
if (!$this->hasDefinition($id) && isset($this->aliasDefinitions[$id])) {
return $this->get($this->aliasDefinitions[$id]);
}
try {
$definition = $this->getDefinition($id);
} catch (InvalidArgumentException $e) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return null;
}
throw $e;
}
$this->loading[$id] = true;
try {
$service = $this->createService($definition, $id);
} catch (\Exception $e) {
unset($this->loading[$id]);
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return null;
}
throw $e;
}
$this->loading[$id] = true;
try {
$service = $this->createService($definition, $id);
} catch (\Exception $e) {
unset($this->loading[$id]);
return $service;
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return null;
}
throw $e;
}
unset($this->loading[$id]);
return $service;
}
/**