Skip to content

Commit

Permalink
Use isset() instead of array_key_exists() in DIC
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Catchpole authored and fabpot committed Sep 3, 2013
1 parent 71621d7 commit e4b3039
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/Symfony/Component/DependencyInjection/Container.php
Expand Up @@ -240,8 +240,9 @@ public function has($id)
{
$id = strtolower($id);

return array_key_exists($id, $this->services)
|| array_key_exists($id, $this->aliases)
return isset($this->services[$id])
|| array_key_exists($id, $this->services)
|| isset($this->aliases[$id])
|| method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service')
;
}
Expand All @@ -267,16 +268,21 @@ public function has($id)
*/
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
{
$id = strtolower($id);

// resolve aliases
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}

// re-use shared service instance if it exists
if (array_key_exists($id, $this->services)) {
return $this->services[$id];
// Attempt to retrieve the service by checking first aliases then
// available services. Service IDs are case insensitive, however since
// this method can be called thousands of times during a request, avoid
// calling strotolower() unless necessary.
foreach (array(false, true) as $strtolower) {
if ($strtolower) {
$id = strtolower($id);
}
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
// Re-use shared service instance if it exists.
if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
return $this->services[$id];
}
}

if (isset($this->loading[$id])) {
Expand Down Expand Up @@ -339,7 +345,8 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
*/
public function initialized($id)
{
return array_key_exists(strtolower($id), $this->services);
$id = strtolower($id);
return isset($this->services[$id]) || array_key_exists($id, $this->services);
}

/**
Expand Down

0 comments on commit e4b3039

Please sign in to comment.