minor #14643 [DependencyInjection] Avoid unnecessary calls to strtolower() (nicolas-grekas)

This PR was merged into the 2.3 branch.

Discussion
----------

[DependencyInjection] Avoid unnecessary calls to strtolower()

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

Commits
-------

c27f564 [DependencyInjection] Avoid unnecessary calls to strtolower()
This commit is contained in:
Fabien Potencier 2015-05-28 16:28:38 +02:00
commit 41adf224b5
2 changed files with 63 additions and 58 deletions

View File

@ -76,6 +76,8 @@ class Container implements IntrospectableContainerInterface
protected $scopeStacks;
protected $loading = array();
private $underscoreMap = array('_' => '', '.' => '_', '\\' => '_');
/**
* Constructor.
*
@ -218,7 +220,7 @@ class Container implements IntrospectableContainerInterface
$this->services[$id] = $service;
if (method_exists($this, $method = 'synchronize'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')) {
if (method_exists($this, $method = 'synchronize'.strtr($id, $this->underscoreMap).'Service')) {
$this->$method();
}
@ -242,17 +244,20 @@ class Container implements IntrospectableContainerInterface
*/
public function has($id)
{
$id = strtolower($id);
if ('service_container' === $id) {
for ($i = 2;;) {
if ('service_container' === $id
|| isset($this->aliases[$id])
|| isset($this->services[$id])
|| array_key_exists($id, $this->services)
) {
return true;
}
return isset($this->services[$id])
|| array_key_exists($id, $this->services)
|| isset($this->aliases[$id])
|| method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')
;
if (--$i && $id !== $lcId = strtolower($id)) {
$id = $lcId;
} else {
return method_exists($this, 'get'.strtr($id, $this->underscoreMap).'Service');
}
}
}
/**
@ -280,10 +285,7 @@ class Container implements IntrospectableContainerInterface
// available services. Service IDs are case insensitive, however since
// this method can be called thousands of times during a request, avoid
// calling strtolower() unless necessary.
foreach (array(false, true) as $strtolower) {
if ($strtolower) {
$id = strtolower($id);
}
for ($i = 2;;) {
if ('service_container' === $id) {
return $this;
}
@ -294,7 +296,6 @@ class Container implements IntrospectableContainerInterface
if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
return $this->services[$id];
}
}
if (isset($this->loading[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
@ -302,7 +303,10 @@ class Container implements IntrospectableContainerInterface
if (isset($this->methodMap[$id])) {
$method = $this->methodMap[$id];
} elseif (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')) {
} elseif (--$i && $id !== $lcId = strtolower($id)) {
$id = $lcId;
continue;
} elseif (method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) {
// $method is set to the right value, proceed
} else {
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
@ -346,6 +350,7 @@ class Container implements IntrospectableContainerInterface
return $service;
}
}
/**
* Returns true if the given service has actually been initialized.

View File

@ -289,7 +289,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$container->enterScope('foo');
$scoped2 = $container->get('scoped');
$scoped3 = $container->get('scoped');
$scoped3 = $container->get('SCOPED');
$scopedFoo2 = $container->get('scoped_foo');
$container->leaveScope('foo');