Skip to content

Commit 99831fc

Browse files
[DependencyInjection] Avoid unnecessary calls to strtolower()
1 parent da58ad7 commit 99831fc

File tree

1 file changed

+58
-51
lines changed

1 file changed

+58
-51
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class Container implements IntrospectableContainerInterface
7676
protected $scopeStacks;
7777
protected $loading = array();
7878

79+
private static $underscoreMap = array('_' => '', '.' => '_', '\\' => '_');
80+
7981
/**
8082
* Constructor.
8183
*
@@ -218,7 +220,7 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)
218220

219221
$this->services[$id] = $service;
220222

221-
if (method_exists($this, $method = 'synchronize'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')) {
223+
if (method_exists($this, $method = 'synchronize'.strtr($id, self::$underscoreMap).'Service')) {
222224
$this->$method();
223225
}
224226

@@ -242,17 +244,21 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)
242244
*/
243245
public function has($id)
244246
{
245-
$id = strtolower($id);
246-
247-
if ('service_container' === $id) {
248-
return true;
247+
for ($i = 2;;) {
248+
if ('service_container' === $id
249+
|| isset($this->services[$id])
250+
|| isset($this->aliases[$id])
251+
|| array_key_exists($id, $this->services)
252+
|| method_exists($this, 'get'.strtr($id, self::$underscoreMap).'Service')
253+
) {
254+
return true;
255+
}
256+
if (--$i && ($id !== $lcId = strtolower($id))) {
257+
$id = $lcId;
258+
} else {
259+
return false;
260+
}
249261
}
250-
251-
return isset($this->services[$id])
252-
|| array_key_exists($id, $this->services)
253-
|| isset($this->aliases[$id])
254-
|| method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')
255-
;
256262
}
257263

258264
/**
@@ -280,10 +286,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
280286
// available services. Service IDs are case insensitive, however since
281287
// this method can be called thousands of times during a request, avoid
282288
// calling strtolower() unless necessary.
283-
foreach (array(false, true) as $strtolower) {
284-
if ($strtolower) {
285-
$id = strtolower($id);
286-
}
289+
for ($i = 2;;) {
287290
if ('service_container' === $id) {
288291
return $this;
289292
}
@@ -294,57 +297,61 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
294297
if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
295298
return $this->services[$id];
296299
}
297-
}
298300

299-
if (isset($this->loading[$id])) {
300-
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
301-
}
301+
if (isset($this->loading[$id])) {
302+
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
303+
}
302304

303-
if (isset($this->methodMap[$id])) {
304-
$method = $this->methodMap[$id];
305-
} elseif (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')) {
306-
// $method is set to the right value, proceed
307-
} else {
308-
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
309-
if (!$id) {
310-
throw new ServiceNotFoundException($id);
311-
}
305+
if (isset($this->methodMap[$id])) {
306+
$method = $this->methodMap[$id];
307+
} elseif (--$i && ($id !== $lcId = strtolower($id))) {
308+
$id = $lcId;
309+
continue;
310+
}
311+
if (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')) {
312+
// $method is set to the right value, proceed
313+
} else {
314+
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
315+
if (!$id) {
316+
throw new ServiceNotFoundException($id);
317+
}
312318

313-
$alternatives = array();
314-
foreach ($this->services as $key => $associatedService) {
315-
$lev = levenshtein($id, $key);
316-
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
317-
$alternatives[] = $key;
319+
$alternatives = array();
320+
foreach ($this->services as $key => $associatedService) {
321+
$lev = levenshtein($id, $key);
322+
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
323+
$alternatives[] = $key;
324+
}
318325
}
326+
327+
throw new ServiceNotFoundException($id, null, null, $alternatives);
319328
}
320329

321-
throw new ServiceNotFoundException($id, null, null, $alternatives);
330+
return;
322331
}
323332

324-
return;
325-
}
333+
$this->loading[$id] = true;
326334

327-
$this->loading[$id] = true;
335+
try {
336+
$service = $this->$method();
337+
} catch (\Exception $e) {
338+
unset($this->loading[$id]);
328339

329-
try {
330-
$service = $this->$method();
331-
} catch (\Exception $e) {
332-
unset($this->loading[$id]);
340+
if (array_key_exists($id, $this->services)) {
341+
unset($this->services[$id]);
342+
}
333343

334-
if (array_key_exists($id, $this->services)) {
335-
unset($this->services[$id]);
336-
}
344+
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
345+
return;
346+
}
337347

338-
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
339-
return;
348+
throw $e;
340349
}
341350

342-
throw $e;
343-
}
344-
345-
unset($this->loading[$id]);
351+
unset($this->loading[$id]);
346352

347-
return $service;
353+
return $service;
354+
}
348355
}
349356

350357
/**

0 commit comments

Comments
 (0)