Skip to content

[DependencyInjection] Avoid unnecessary calls to strtolower() #14643

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

Merged
merged 1 commit into from
May 28, 2015
Merged
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
107 changes: 56 additions & 51 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class Container implements IntrospectableContainerInterface
protected $scopeStacks;
protected $loading = array();

private $underscoreMap = array('_' => '', '.' => '_', '\\' => '_');

/**
* Constructor.
*
Expand Down Expand Up @@ -218,7 +220,7 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)

$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();
}

Expand All @@ -242,17 +244,20 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)
*/
public function has($id)
{
$id = strtolower($id);

if ('service_container' === $id) {
return true;
for ($i = 2;;) {
if ('service_container' === $id
|| isset($this->aliases[$id])
|| isset($this->services[$id])
|| array_key_exists($id, $this->services)
) {
return true;
}
if (--$i && $id !== $lcId = strtolower($id)) {
$id = $lcId;
} else {
return method_exists($this, 'get'.strtr($id, $this->underscoreMap).'Service');
}
}

return isset($this->services[$id])
|| array_key_exists($id, $this->services)
|| isset($this->aliases[$id])
|| method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')
;
}

/**
Expand Down Expand Up @@ -280,10 +285,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
// 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;
}
Expand All @@ -294,57 +296,60 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
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));
}
if (isset($this->loading[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
}

if (isset($this->methodMap[$id])) {
$method = $this->methodMap[$id];
} elseif (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')) {
// $method is set to the right value, proceed
} else {
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
if (!$id) {
throw new ServiceNotFoundException($id);
}
if (isset($this->methodMap[$id])) {
$method = $this->methodMap[$id];
} 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) {
if (!$id) {
throw new ServiceNotFoundException($id);
}

$alternatives = array();
foreach ($this->services as $key => $associatedService) {
$lev = levenshtein($id, $key);
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
$alternatives[] = $key;
$alternatives = array();
foreach ($this->services as $key => $associatedService) {
$lev = levenshtein($id, $key);
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
$alternatives[] = $key;
}
}

throw new ServiceNotFoundException($id, null, null, $alternatives);
}

throw new ServiceNotFoundException($id, null, null, $alternatives);
return;
}

return;
}
$this->loading[$id] = true;

$this->loading[$id] = true;
try {
$service = $this->$method();
} catch (\Exception $e) {
unset($this->loading[$id]);

try {
$service = $this->$method();
} catch (\Exception $e) {
unset($this->loading[$id]);
if (array_key_exists($id, $this->services)) {
unset($this->services[$id]);
}

if (array_key_exists($id, $this->services)) {
unset($this->services[$id]);
}
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return;
}

if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return;
throw $e;
}

throw $e;
}

unset($this->loading[$id]);
unset($this->loading[$id]);

return $service;
return $service;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function testEnterLeaveCurrentScope()

$container->enterScope('foo');
$scoped2 = $container->get('scoped');
$scoped3 = $container->get('scoped');
$scoped3 = $container->get('SCOPED');
$scopedFoo2 = $container->get('scoped_foo');

$container->leaveScope('foo');
Expand Down