Skip to content

Commit 3d71aef

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

File tree

1 file changed

+57
-51
lines changed

1 file changed

+57
-51
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 57 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,60 @@ 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+
} elseif (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')) {
311+
// $method is set to the right value, proceed
312+
} else {
313+
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
314+
if (!$id) {
315+
throw new ServiceNotFoundException($id);
316+
}
312317

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;
318+
$alternatives = array();
319+
foreach ($this->services as $key => $associatedService) {
320+
$lev = levenshtein($id, $key);
321+
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
322+
$alternatives[] = $key;
323+
}
318324
}
325+
326+
throw new ServiceNotFoundException($id, null, null, $alternatives);
319327
}
320328

321-
throw new ServiceNotFoundException($id, null, null, $alternatives);
329+
return;
322330
}
323331

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

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

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

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

338-
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
339-
return;
347+
throw $e;
340348
}
341349

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

347-
return $service;
352+
return $service;
353+
}
348354
}
349355

350356
/**

0 commit comments

Comments
 (0)