Use isset() instead of array_key_exists() in DIC

This commit is contained in:
Nathaniel Catchpole 2013-09-01 13:17:20 +01:00 committed by Fabien Potencier
parent 71621d7404
commit e4b303973f

View File

@ -240,8 +240,9 @@ class Container implements IntrospectableContainerInterface
{ {
$id = strtolower($id); $id = strtolower($id);
return array_key_exists($id, $this->services) return isset($this->services[$id])
|| array_key_exists($id, $this->aliases) || array_key_exists($id, $this->services)
|| isset($this->aliases[$id])
|| method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service') || method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service')
; ;
} }
@ -267,16 +268,21 @@ class Container implements IntrospectableContainerInterface
*/ */
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
{ {
$id = strtolower($id); // Attempt to retrieve the service by checking first aliases then
// available services. Service IDs are case insensitive, however since
// resolve aliases // this method can be called thousands of times during a request, avoid
if (isset($this->aliases[$id])) { // calling strotolower() unless necessary.
$id = $this->aliases[$id]; foreach (array(false, true) as $strtolower) {
} if ($strtolower) {
$id = strtolower($id);
// re-use shared service instance if it exists }
if (array_key_exists($id, $this->services)) { if (isset($this->aliases[$id])) {
return $this->services[$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])) { if (isset($this->loading[$id])) {
@ -339,7 +345,8 @@ class Container implements IntrospectableContainerInterface
*/ */
public function initialized($id) 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);
} }
/** /**