Skip to content

Commit 801a917

Browse files
committed
Merge branch '2.3' into 2.6
* 2.3: Revert "bug #14262 [TwigBundle] Refresh twig paths when resources change. (aitboudad)" InvalidResourceException file name [Validators] Remove forgotten space in a translation key [nl] [Validators] Correct translation key and content [nl] added missing CVE number bumped Symfony version to 2.3.30 updated VERSION for 2.3.29 update CONTRIBUTORS for 2.3.29 updated CHANGELOG for 2.3.29 [CS] [Console] StreamOuput : fix loose comparison [DependencyInjection] Avoid unnecessary calls to strtolower() Conflicts: src/Symfony/Component/Console/Output/StreamOutput.php src/Symfony/Component/HttpKernel/Kernel.php
2 parents d244c4e + 85fe678 commit 801a917

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

Container.php

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

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

212214
$this->services[$id] = $service;
213215

214-
if (method_exists($this, $method = 'synchronize'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')) {
216+
if (method_exists($this, $method = 'synchronize'.strtr($id, $this->underscoreMap).'Service')) {
215217
$this->$method();
216218
}
217219

@@ -235,17 +237,20 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)
235237
*/
236238
public function has($id)
237239
{
238-
$id = strtolower($id);
239-
240-
if ('service_container' === $id) {
241-
return true;
240+
for ($i = 2;;) {
241+
if ('service_container' === $id
242+
|| isset($this->aliases[$id])
243+
|| isset($this->services[$id])
244+
|| array_key_exists($id, $this->services)
245+
) {
246+
return true;
247+
}
248+
if (--$i && $id !== $lcId = strtolower($id)) {
249+
$id = $lcId;
250+
} else {
251+
return method_exists($this, 'get'.strtr($id, $this->underscoreMap).'Service');
252+
}
242253
}
243-
244-
return isset($this->services[$id])
245-
|| array_key_exists($id, $this->services)
246-
|| isset($this->aliases[$id])
247-
|| method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')
248-
;
249254
}
250255

251256
/**
@@ -273,10 +278,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
273278
// available services. Service IDs are case insensitive, however since
274279
// this method can be called thousands of times during a request, avoid
275280
// calling strtolower() unless necessary.
276-
foreach (array(false, true) as $strtolower) {
277-
if ($strtolower) {
278-
$id = strtolower($id);
279-
}
281+
for ($i = 2;;) {
280282
if ('service_container' === $id) {
281283
return $this;
282284
}
@@ -287,57 +289,60 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
287289
if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
288290
return $this->services[$id];
289291
}
290-
}
291292

292-
if (isset($this->loading[$id])) {
293-
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
294-
}
293+
if (isset($this->loading[$id])) {
294+
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
295+
}
295296

296-
if (isset($this->methodMap[$id])) {
297-
$method = $this->methodMap[$id];
298-
} elseif (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_', '\\' => '_')).'Service')) {
299-
// $method is set to the right value, proceed
300-
} else {
301-
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
302-
if (!$id) {
303-
throw new ServiceNotFoundException($id);
304-
}
297+
if (isset($this->methodMap[$id])) {
298+
$method = $this->methodMap[$id];
299+
} elseif (--$i && $id !== $lcId = strtolower($id)) {
300+
$id = $lcId;
301+
continue;
302+
} elseif (method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) {
303+
// $method is set to the right value, proceed
304+
} else {
305+
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
306+
if (!$id) {
307+
throw new ServiceNotFoundException($id);
308+
}
305309

306-
$alternatives = array();
307-
foreach ($this->services as $key => $associatedService) {
308-
$lev = levenshtein($id, $key);
309-
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
310-
$alternatives[] = $key;
310+
$alternatives = array();
311+
foreach ($this->services as $key => $associatedService) {
312+
$lev = levenshtein($id, $key);
313+
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
314+
$alternatives[] = $key;
315+
}
311316
}
317+
318+
throw new ServiceNotFoundException($id, null, null, $alternatives);
312319
}
313320

314-
throw new ServiceNotFoundException($id, null, null, $alternatives);
321+
return;
315322
}
316323

317-
return;
318-
}
324+
$this->loading[$id] = true;
319325

320-
$this->loading[$id] = true;
326+
try {
327+
$service = $this->$method();
328+
} catch (\Exception $e) {
329+
unset($this->loading[$id]);
321330

322-
try {
323-
$service = $this->$method();
324-
} catch (\Exception $e) {
325-
unset($this->loading[$id]);
331+
if (array_key_exists($id, $this->services)) {
332+
unset($this->services[$id]);
333+
}
326334

327-
if (array_key_exists($id, $this->services)) {
328-
unset($this->services[$id]);
329-
}
335+
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
336+
return;
337+
}
330338

331-
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
332-
return;
339+
throw $e;
333340
}
334341

335-
throw $e;
336-
}
337-
338-
unset($this->loading[$id]);
342+
unset($this->loading[$id]);
339343

340-
return $service;
344+
return $service;
345+
}
341346
}
342347

343348
/**

Tests/ContainerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public function testEnterLeaveCurrentScope()
322322

323323
$container->enterScope('foo');
324324
$scoped2 = $container->get('scoped');
325-
$scoped3 = $container->get('scoped');
325+
$scoped3 = $container->get('SCOPED');
326326
$scopedFoo2 = $container->get('scoped_foo');
327327

328328
$container->leaveScope('foo');

0 commit comments

Comments
 (0)