Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 99083df

Browse filesBrowse files
[DI] Deprecate case insentivity of service identifiers
1 parent 16d33e1 commit 99083df
Copy full SHA for 99083df

16 files changed

+254
-73
lines changed

‎UPGRADE-3.3.md

Copy file name to clipboardExpand all lines: UPGRADE-3.3.md
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ ClassLoader
1010
DependencyInjection
1111
-------------------
1212

13+
* The `Reference` and `Alias` classes do not make service identifiers lowercase anymore.
14+
15+
* Case insensitivity of service identifiers is deprecated and will be removed in 4.0.
16+
1317
* Using the `PhpDumper` with an uncompiled `ContainerBuilder` is deprecated and
1418
will not be supported anymore in 4.0.
1519

‎src/Symfony/Component/DependencyInjection/Alias.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Alias.php
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ class Alias
2222
*/
2323
public function __construct($id, $public = true)
2424
{
25-
$this->id = strtolower($id);
25+
if (!is_string($id)) {
26+
$type = is_object($id) ? get_class($id) : gettype($id);
27+
$id = (string) $id;
28+
@trigger_error(sprintf('Non-string identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for Alias to "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED);
29+
}
30+
$this->id = $id;
2631
$this->public = $public;
2732
}
2833

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ CHANGELOG
44
3.3.0
55
-----
66

7+
* deprecated case insensitivity of service identifiers
78
* added "iterator" argument type for lazy iteration over a set of values and services
89
* added "closure-proxy" argument type for turning services' methods into lazy callables
910
* added file-wide configurable defaults for service attributes "public", "tags",
10-
"autowire" and a new "inherit-tags"
11+
"autowire" and "inherit-tags"
12+
* added "inherit-tags" service attribute to control tags' inheritance from parent context
1113
* made the "class" attribute optional, using the "id" as fallback
1214
* using the `PhpDumper` with an uncompiled `ContainerBuilder` is deprecated and
1315
will not be supported anymore in 4.0

‎src/Symfony/Component/DependencyInjection/Compiler/FactoryReturnTypePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/FactoryReturnTypePass.php
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ public function process(ContainerBuilder $container)
5151
private function updateDefinition(ContainerBuilder $container, $id, Definition $definition, array $resolveClassPassChanges, array $previous = array())
5252
{
5353
// circular reference
54-
if (isset($previous[$id])) {
54+
$lcId = strtolower($id);
55+
if (isset($previous[$lcId])) {
5556
return;
5657
}
5758

5859
$factory = $definition->getFactory();
59-
if (null === $factory || (!isset($resolveClassPassChanges[$id]) && null !== $definition->getClass())) {
60+
if (null === $factory || (!isset($resolveClassPassChanges[$lcId]) && null !== $definition->getClass())) {
6061
return;
6162
}
6263

@@ -69,9 +70,9 @@ private function updateDefinition(ContainerBuilder $container, $id, Definition $
6970
}
7071
} else {
7172
if ($factory[0] instanceof Reference) {
72-
$previous[$id] = true;
73+
$previous[$lcId] = true;
7374
$factoryDefinition = $container->findDefinition((string) $factory[0]);
74-
$this->updateDefinition($container, strtolower($factory[0]), $factoryDefinition, $resolveClassPassChanges, $previous);
75+
$this->updateDefinition($container, $factory[0], $factoryDefinition, $resolveClassPassChanges, $previous);
7576
$class = $factoryDefinition->getClass();
7677
} else {
7778
$class = $factory[0];
@@ -96,7 +97,7 @@ private function updateDefinition(ContainerBuilder $container, $id, Definition $
9697
}
9798
}
9899

99-
if (null !== $returnType && (!isset($resolveClassPassChanges[$id]) || $returnType !== $resolveClassPassChanges[$id])) {
100+
if (null !== $returnType && (!isset($resolveClassPassChanges[$lcId]) || $returnType !== $resolveClassPassChanges[$lcId])) {
100101
@trigger_error(sprintf('Relying on its factory\'s return-type to define the class of service "%s" is deprecated since Symfony 3.3 and won\'t work in 4.0. Set the "class" attribute to "%s" on the service definition instead.', $id, $returnType), E_USER_DEPRECATED);
101102
}
102103
$definition->setClass($returnType);

‎src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/ResolveClassPass.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function process(ContainerBuilder $container)
3131
continue;
3232
}
3333
if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $id)) {
34-
$this->changes[$id] = $container->getCaseSensitiveId($id);
35-
$definition->setClass($this->changes[$id]);
34+
$this->changes[strtolower($id)] = $id;
35+
$definition->setClass($id);
3636
}
3737
}
3838
}

‎src/Symfony/Component/DependencyInjection/Container.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Container.php
+40-7Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class Container implements ResettableContainerInterface
7070
protected $aliases = array();
7171
protected $loading = array();
7272

73+
/**
74+
* @internal
75+
*/
76+
protected $normalizedIds = array();
77+
7378
private $underscoreMap = array('_' => '', '.' => '_', '\\' => '_');
7479
private $envCache = array();
7580

@@ -164,7 +169,7 @@ public function setParameter($name, $value)
164169
*/
165170
public function set($id, $service)
166171
{
167-
$id = strtolower($id);
172+
$id = $this->normalizeId($id);
168173

169174
if ('service_container' === $id) {
170175
throw new InvalidArgumentException('You cannot set service "service_container".');
@@ -215,8 +220,8 @@ public function has($id)
215220
return true;
216221
}
217222

218-
if (--$i && $id !== $lcId = strtolower($id)) {
219-
$id = $lcId;
223+
if (--$i && $id !== $normalizedId = $this->normalizeId($id)) {
224+
$id = $normalizedId;
220225
continue;
221226
}
222227

@@ -254,7 +259,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
254259
// Attempt to retrieve the service by checking first aliases then
255260
// available services. Service IDs are case insensitive, however since
256261
// this method can be called thousands of times during a request, avoid
257-
// calling strtolower() unless necessary.
262+
// calling $this->normalizeId($id) unless necessary.
258263
for ($i = 2;;) {
259264
if ('service_container' === $id) {
260265
return $this;
@@ -273,8 +278,8 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
273278

274279
if (isset($this->methodMap[$id])) {
275280
$method = $this->methodMap[$id];
276-
} elseif (--$i && $id !== $lcId = strtolower($id)) {
277-
$id = $lcId;
281+
} elseif (--$i && $id !== $normalizedId = $this->normalizeId($id)) {
282+
$id = $normalizedId;
278283
continue;
279284
} elseif (!$this->methodMap && !$this instanceof ContainerBuilder && __CLASS__ !== static::class && method_exists($this, $method = 'get'.strtr($id, $this->underscoreMap).'Service')) {
280285
// We only check the convention-based factory in a compiled container (i.e. a child class other than a ContainerBuilder,
@@ -329,7 +334,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
329334
*/
330335
public function initialized($id)
331336
{
332-
$id = strtolower($id);
337+
$id = $this->normalizeId($id);
333338

334339
if ('service_container' === $id) {
335340
return false;
@@ -426,6 +431,34 @@ protected function getEnv($name)
426431
return $this->envCache[$name] = $this->getParameter("env($name)");
427432
}
428433

434+
/**
435+
* Returns the case sensitive id used at registration time.
436+
*
437+
* @param string $id
438+
*
439+
* @return string
440+
*
441+
* @internal
442+
*/
443+
public function normalizeId($id)
444+
{
445+
if (!is_string($id)) {
446+
$type = is_object($id) ? get_class($id) : gettype($id);
447+
$id = (string) $id;
448+
@trigger_error(sprintf('Non-string service identifiers are deprecated since Symfony 3.3 and won\'t be supported in 4.0 for service "%s" ("%s" given.) Cast it to string beforehand.', $id, $type), E_USER_DEPRECATED);
449+
}
450+
if (isset($this->normalizedIds[$normalizedId = strtolower($id)])) {
451+
$normalizedId = $this->normalizedIds[$normalizedId];
452+
if ($id !== $normalizedId) {
453+
@trigger_error(sprintf('Service identifiers will be made case sensitive in Symfony 4.0. Using "%s" instead of "%s" is deprecated since version 3.3.', $id, $normalizedId), E_USER_DEPRECATED);
454+
}
455+
} else {
456+
$normalizedId = $this->normalizedIds[$normalizedId] = $id;
457+
}
458+
459+
return $normalizedId;
460+
}
461+
429462
private function __clone()
430463
{
431464
}

‎src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+30-47Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
103103
*/
104104
private $envCounters = array();
105105

106-
/**
107-
* @var array a map of case less to case sensitive ids
108-
*/
109-
private $caseSensitiveIds = array();
110-
111106
/**
112107
* Sets the track resources flag.
113108
*
@@ -372,18 +367,14 @@ public function getCompiler()
372367
*/
373368
public function set($id, $service)
374369
{
375-
$caseSensitiveId = $id;
376-
$id = strtolower($caseSensitiveId);
370+
$id = $this->normalizeId($id);
377371

378372
if ($this->isFrozen() && (isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())) {
379373
// setting a synthetic service on a frozen container is alright
380374
throw new BadMethodCallException(sprintf('Setting service "%s" for an unknown or non-synthetic service definition on a frozen container is not allowed.', $id));
381375
}
382376

383377
unset($this->definitions[$id], $this->aliasDefinitions[$id]);
384-
if ($id !== $caseSensitiveId) {
385-
$this->caseSensitiveIds[$id] = $caseSensitiveId;
386-
}
387378

388379
parent::set($id, $service);
389380
}
@@ -395,7 +386,7 @@ public function set($id, $service)
395386
*/
396387
public function removeDefinition($id)
397388
{
398-
unset($this->definitions[strtolower($id)]);
389+
unset($this->definitions[$this->normalizeId($id)]);
399390
}
400391

401392
/**
@@ -407,7 +398,7 @@ public function removeDefinition($id)
407398
*/
408399
public function has($id)
409400
{
410-
$id = strtolower($id);
401+
$id = $this->normalizeId($id);
411402

412403
return isset($this->definitions[$id]) || isset($this->aliasDefinitions[$id]) || parent::has($id);
413404
}
@@ -429,7 +420,7 @@ public function has($id)
429420
*/
430421
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
431422
{
432-
$id = strtolower($id);
423+
$id = $this->normalizeId($id);
433424

434425
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
435426
return $service;
@@ -637,11 +628,10 @@ public function setAliases(array $aliases)
637628
*/
638629
public function setAlias($alias, $id)
639630
{
640-
$caseSensitiveAlias = $alias;
641-
$alias = strtolower($caseSensitiveAlias);
631+
$alias = $this->normalizeId($alias);
642632

643633
if (is_string($id)) {
644-
$id = new Alias($id);
634+
$id = new Alias($this->normalizeId($id));
645635
} elseif (!$id instanceof Alias) {
646636
throw new InvalidArgumentException('$id must be a string, or an Alias object.');
647637
}
@@ -651,9 +641,6 @@ public function setAlias($alias, $id)
651641
}
652642

653643
unset($this->definitions[$alias]);
654-
if ($alias !== $caseSensitiveAlias) {
655-
$this->caseSensitiveIds[$alias] = $caseSensitiveAlias;
656-
}
657644

658645
$this->aliasDefinitions[$alias] = $id;
659646
}
@@ -665,7 +652,7 @@ public function setAlias($alias, $id)
665652
*/
666653
public function removeAlias($alias)
667654
{
668-
unset($this->aliasDefinitions[strtolower($alias)]);
655+
unset($this->aliasDefinitions[$this->normalizeId($alias)]);
669656
}
670657

671658
/**
@@ -677,7 +664,7 @@ public function removeAlias($alias)
677664
*/
678665
public function hasAlias($id)
679666
{
680-
return isset($this->aliasDefinitions[strtolower($id)]);
667+
return isset($this->aliasDefinitions[$this->normalizeId($id)]);
681668
}
682669

683670
/**
@@ -701,7 +688,7 @@ public function getAliases()
701688
*/
702689
public function getAlias($id)
703690
{
704-
$id = strtolower($id);
691+
$id = $this->normalizeId($id);
705692

706693
if (!isset($this->aliasDefinitions[$id])) {
707694
throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
@@ -791,13 +778,9 @@ public function setDefinition($id, Definition $definition)
791778
throw new BadMethodCallException('Adding definition to a frozen container is not allowed');
792779
}
793780

794-
$caseSensitiveId = $id;
795-
$id = strtolower($caseSensitiveId);
781+
$id = $this->normalizeId($id);
796782

797783
unset($this->aliasDefinitions[$id]);
798-
if ($id !== $caseSensitiveId) {
799-
$this->caseSensitiveIds[$id] = $caseSensitiveId;
800-
}
801784

802785
return $this->definitions[$id] = $definition;
803786
}
@@ -811,7 +794,7 @@ public function setDefinition($id, Definition $definition)
811794
*/
812795
public function hasDefinition($id)
813796
{
814-
return isset($this->definitions[strtolower($id)]);
797+
return isset($this->definitions[$this->normalizeId($id)]);
815798
}
816799

817800
/**
@@ -825,7 +808,7 @@ public function hasDefinition($id)
825808
*/
826809
public function getDefinition($id)
827810
{
828-
$id = strtolower($id);
811+
$id = $this->normalizeId($id);
829812

830813
if (!isset($this->definitions[$id])) {
831814
throw new ServiceNotFoundException($id);
@@ -847,7 +830,7 @@ public function getDefinition($id)
847830
*/
848831
public function findDefinition($id)
849832
{
850-
$id = strtolower($id);
833+
$id = $this->normalizeId($id);
851834

852835
while (isset($this->aliasDefinitions[$id])) {
853836
$id = (string) $this->aliasDefinitions[$id];
@@ -856,22 +839,6 @@ public function findDefinition($id)
856839
return $this->getDefinition($id);
857840
}
858841

859-
/**
860-
* Returns the case sensitive id used at registration time.
861-
*
862-
* @param string $id
863-
*
864-
* @return string
865-
*
866-
* @internal
867-
*/
868-
public function getCaseSensitiveId($id)
869-
{
870-
$id = strtolower($id);
871-
872-
return isset($this->caseSensitiveIds[$id]) ? $this->caseSensitiveIds[$id] : $id;
873-
}
874-
875842
/**
876843
* Creates a service for a service definition.
877844
*
@@ -1188,6 +1155,22 @@ public function getEnvCounters()
11881155
return $this->envCounters;
11891156
}
11901157

1158+
/**
1159+
* @internal
1160+
*/
1161+
public function getNormalizedIds()
1162+
{
1163+
$normalizedIds = array();
1164+
1165+
foreach ($this->normalizedIds as $k => $v) {
1166+
if ($v !== (string) $k) {
1167+
$normalizedIds[$k] = $v;
1168+
}
1169+
}
1170+
1171+
return $normalizedIds;
1172+
}
1173+
11911174
/**
11921175
* Returns the Service Conditionals.
11931176
*
@@ -1247,7 +1230,7 @@ private function callMethod($service, $call)
12471230
private function shareService(Definition $definition, $service, $id)
12481231
{
12491232
if (null !== $id && $definition->isShared()) {
1250-
$this->services[strtolower($id)] = $service;
1233+
$this->services[$this->normalizeId($id)] = $service;
12511234
}
12521235
}
12531236

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.