Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51014bf

Browse files
committed
merged branch catch56/container_array_key_exists (PR #8907)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #8907). Discussion ---------- Use isset() instead of array_key_exists() in DIC It doesn't look like the array_key_exists() in Container::get() and Container::has() is necessary. Changing this to isset() removed over 1,000 calls on a default Drupal 8 install - attaching before/after xhprof screenshots with this change. ![screen shot 2013-09-01 at 1 13 41 pm](https://f.cloud.github.com/assets/116285/1063965/d826057c-1300-11e3-96c3-2c94b89fe83a.png) ![screen shot 2013-09-01 at 1 13 49 pm](https://f.cloud.github.com/assets/116285/1063961/99a0f7b2-1300-11e3-9bd2-b0bf22d4245e.png) Commits ------- 3c01ae6 Use isset() instead of array_key_exists() in DIC
2 parents 71621d7 + e4b3039 commit 51014bf

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,9 @@ public function has($id)
240240
{
241241
$id = strtolower($id);
242242

243-
return array_key_exists($id, $this->services)
244-
|| array_key_exists($id, $this->aliases)
243+
return isset($this->services[$id])
244+
|| array_key_exists($id, $this->services)
245+
|| isset($this->aliases[$id])
245246
|| method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service')
246247
;
247248
}
@@ -267,16 +268,21 @@ public function has($id)
267268
*/
268269
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
269270
{
270-
$id = strtolower($id);
271-
272-
// resolve aliases
273-
if (isset($this->aliases[$id])) {
274-
$id = $this->aliases[$id];
275-
}
276-
277-
// re-use shared service instance if it exists
278-
if (array_key_exists($id, $this->services)) {
279-
return $this->services[$id];
271+
// Attempt to retrieve the service by checking first aliases then
272+
// available services. Service IDs are case insensitive, however since
273+
// this method can be called thousands of times during a request, avoid
274+
// calling strotolower() unless necessary.
275+
foreach (array(false, true) as $strtolower) {
276+
if ($strtolower) {
277+
$id = strtolower($id);
278+
}
279+
if (isset($this->aliases[$id])) {
280+
$id = $this->aliases[$id];
281+
}
282+
// Re-use shared service instance if it exists.
283+
if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
284+
return $this->services[$id];
285+
}
280286
}
281287

282288
if (isset($this->loading[$id])) {
@@ -339,7 +345,8 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
339345
*/
340346
public function initialized($id)
341347
{
342-
return array_key_exists(strtolower($id), $this->services);
348+
$id = strtolower($id);
349+
return isset($this->services[$id]) || array_key_exists($id, $this->services);
343350
}
344351

345352
/**

0 commit comments

Comments
 (0)