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

Skip to content

Commit b33c4ed

Browse files
committed
deprecate most characters in service ids
1 parent b868feb commit b33c4ed

File tree

10 files changed

+151
-9
lines changed

10 files changed

+151
-9
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
227227
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". No services were found matching this %s.', $typeHint->name, $id, $classOrInterface));
228228
}
229229

230-
$argumentId = sprintf('autowired.%s', $typeHint->name);
230+
$argumentId = str_replace('\\', '_', sprintf('autowired.%s', $typeHint->name));
231231

232232
$argumentDefinition = $this->container->register($argumentId, $typeHint->name);
233233
$argumentDefinition->setPublic(false);

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ public function setParameter($name, $value)
163163
*/
164164
public function set($id, $service)
165165
{
166+
if (preg_match('/[^a-z0-9\._%]/i', $id)) {
167+
@trigger_error(sprintf('Using characters other than A-Z, 0-9, ., and _ in service ids ("%s" given) is deprecated since Symfony 3.1 and will throw an exception in 4.0.', $id), E_USER_DEPRECATED);
168+
}
169+
166170
$id = strtolower($id);
167171

168172
if ('service_container' === $id) {

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,10 @@ public function setAlias($alias, $id)
613613
throw new InvalidArgumentException(sprintf('An alias can not reference itself, got a circular reference on "%s".', $alias));
614614
}
615615

616+
if (preg_match('/[^a-z0-9\._%]/i', $alias)) {
617+
@trigger_error(sprintf('Using characters other than A-Z, 0-9, ., and _ in service ids ("%s" given) is deprecated since Symfony 3.1 and will throw an exception in 4.0.', $alias), E_USER_DEPRECATED);
618+
}
619+
616620
unset($this->definitions[$alias]);
617621

618622
$this->aliasDefinitions[$alias] = $id;
@@ -731,6 +735,10 @@ public function getDefinitions()
731735
*/
732736
public function setDefinition($id, Definition $definition)
733737
{
738+
if (preg_match('/[^a-z0-9\._%]/i', $id)) {
739+
@trigger_error(sprintf('Using characters other than A-Z, 0-9, ., and _ in service ids ("%s" given) is deprecated since Symfony 3.1 and will throw an exception in 4.0.', $id), E_USER_DEPRECATED);
740+
}
741+
734742
if ($this->isFrozen()) {
735743
throw new BadMethodCallException('Adding definition to a frozen container is not allowed');
736744
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,15 @@ public function testCreateDefinition()
210210
$pass->process($container);
211211

212212
$this->assertCount(1, $container->getDefinition('coop_tilleuls')->getArguments());
213-
$this->assertEquals('autowired.symfony\component\dependencyinjection\tests\compiler\dunglas', $container->getDefinition('coop_tilleuls')->getArgument(0));
213+
$this->assertEquals('autowired.symfony_component_dependencyinjection_tests_compiler_dunglas', $container->getDefinition('coop_tilleuls')->getArgument(0));
214214

215-
$dunglasDefinition = $container->getDefinition('autowired.symfony\component\dependencyinjection\tests\compiler\dunglas');
215+
$dunglasDefinition = $container->getDefinition('autowired.symfony_component_dependencyinjection_tests_compiler_dunglas');
216216
$this->assertEquals(__NAMESPACE__.'\Dunglas', $dunglasDefinition->getClass());
217217
$this->assertFalse($dunglasDefinition->isPublic());
218218
$this->assertCount(1, $dunglasDefinition->getArguments());
219-
$this->assertEquals('autowired.symfony\component\dependencyinjection\tests\compiler\lille', $dunglasDefinition->getArgument(0));
219+
$this->assertEquals('autowired.symfony_component_dependencyinjection_tests_compiler_lille', $dunglasDefinition->getArgument(0));
220220

221-
$lilleDefinition = $container->getDefinition('autowired.symfony\component\dependencyinjection\tests\compiler\lille');
221+
$lilleDefinition = $container->getDefinition('autowired.symfony_component_dependencyinjection_tests_compiler_lille');
222222
$this->assertEquals(__NAMESPACE__.'\Lille', $lilleDefinition->getClass());
223223
}
224224

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Symfony\Component\DependencyInjection\ContainerInterface;
2121
use Symfony\Component\DependencyInjection\Definition;
2222
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
23-
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
2423
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
2524
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2625
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
@@ -729,6 +728,104 @@ public function testAutowiring()
729728

730729
$this->assertEquals('a', (string) $container->getDefinition('b')->getArgument(0));
731730
}
731+
732+
/**
733+
* @dataProvider getSpecialCharServiceIds
734+
*/
735+
public function testSpecialCharInServiceIdTriggersDeprecationInSet($id)
736+
{
737+
$container = new ContainerBuilder();
738+
739+
$deprecations = array();
740+
set_error_handler(function ($type, $msg) use (&$deprecations) {
741+
if (E_USER_DEPRECATED === $type) {
742+
$deprecations[] = $msg;
743+
}
744+
});
745+
746+
$container->set($id, new \stdClass());
747+
748+
restore_error_handler();
749+
750+
$this->assertCount(1, $deprecations);
751+
$this->assertSame(sprintf('Using characters other than A-Z, 0-9, ., and _ in service ids ("%s" given) is deprecated since Symfony 3.1 and will throw an exception in 4.0.', $id), $deprecations[0]);
752+
}
753+
754+
/**
755+
* @dataProvider getSpecialCharServiceIds
756+
*/
757+
public function testSpecialCharInServiceIdTriggersDeprecationInRegister($id)
758+
{
759+
$container = new ContainerBuilder();
760+
761+
$deprecations = array();
762+
set_error_handler(function ($type, $msg) use (&$deprecations) {
763+
if (E_USER_DEPRECATED === $type) {
764+
$deprecations[] = $msg;
765+
}
766+
});
767+
768+
$container->register($id, 'Foo');
769+
770+
restore_error_handler();
771+
772+
$this->assertCount(1, $deprecations);
773+
$this->assertSame(sprintf('Using characters other than A-Z, 0-9, ., and _ in service ids ("%s" given) is deprecated since Symfony 3.1 and will throw an exception in 4.0.', $id), $deprecations[0]);
774+
}
775+
776+
/**
777+
* @dataProvider getSpecialCharServiceIds
778+
*/
779+
public function testSpecialCharInServiceIdTriggersDeprecationInSetDefinition($id)
780+
{
781+
$container = new ContainerBuilder();
782+
783+
$deprecations = array();
784+
set_error_handler(function ($type, $msg) use (&$deprecations) {
785+
if (E_USER_DEPRECATED === $type) {
786+
$deprecations[] = $msg;
787+
}
788+
});
789+
790+
$container->setDefinition($id, new Definition('Foo'));
791+
792+
restore_error_handler();
793+
794+
$this->assertCount(1, $deprecations);
795+
$this->assertSame(sprintf('Using characters other than A-Z, 0-9, ., and _ in service ids ("%s" given) is deprecated since Symfony 3.1 and will throw an exception in 4.0.', $id), $deprecations[0]);
796+
}
797+
798+
/**
799+
* @dataProvider getSpecialCharServiceIds
800+
*/
801+
public function testSpecialCharInServiceIdTriggersDeprecationInSetAlias($id)
802+
{
803+
$container = new ContainerBuilder();
804+
805+
$deprecations = array();
806+
set_error_handler(function ($type, $msg) use (&$deprecations) {
807+
if (E_USER_DEPRECATED === $type) {
808+
$deprecations[] = $msg;
809+
}
810+
});
811+
812+
$container->setAlias($id, 'foo');
813+
814+
restore_error_handler();
815+
816+
$this->assertCount(1, $deprecations);
817+
$this->assertSame(sprintf('Using characters other than A-Z, 0-9, ., and _ in service ids ("%s" given) is deprecated since Symfony 3.1 and will throw an exception in 4.0.', $id), $deprecations[0]);
818+
}
819+
820+
public function getSpecialCharServiceIds()
821+
{
822+
return array(
823+
'contains @' => array('@service_id'),
824+
'contains dash' => array('my-service'),
825+
'contains umlaut' => array('ä_service'),
826+
'contains $' => array('bar$'),
827+
);
828+
}
732829
}
733830

734831
class FooClass

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,37 @@ public function testSetReplacesAlias()
150150
$this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias');
151151
}
152152

153+
/**
154+
* @dataProvider getSpecialCharServiceIds
155+
*/
156+
public function testSpecialCharInServiceIdTriggersDeprecationInSet($id)
157+
{
158+
$container = new ProjectServiceContainer();
159+
160+
$deprecations = array();
161+
set_error_handler(function ($type, $msg) use (&$deprecations) {
162+
if (E_USER_DEPRECATED === $type) {
163+
$deprecations[] = $msg;
164+
}
165+
});
166+
167+
$container->set($id, new \stdClass());
168+
169+
restore_error_handler();
170+
171+
$this->assertCount(1, $deprecations);
172+
$this->assertSame(sprintf('Using characters other than A-Z, 0-9, ., and _ in service ids ("%s" given) is deprecated since Symfony 3.1 and will throw an exception in 4.0.', $id), $deprecations[0]);
173+
}
174+
175+
public function getSpecialCharServiceIds()
176+
{
177+
return array(
178+
'contains @' => array('@service_id'),
179+
'contains dash' => array('my-service'),
180+
'contains umlaut' => array('ä_service'),
181+
);
182+
}
183+
153184
public function testGet()
154185
{
155186
$sc = new ProjectServiceContainer();

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public function testServicesWithAnonymousFactories()
133133
}
134134

135135
/**
136+
* @group legacy
137+
*
136138
* @expectedException \InvalidArgumentException
137139
* @expectedExceptionMessage Service id "bar$" cannot be converted to a valid PHP method name.
138140
*/

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
;
8888
$container
8989
->register('decorator_service_with_name', 'stdClass')
90-
->setDecoratedService('decorated', 'decorated.pif-pouf')
90+
->setDecoratedService('decorated', 'decorated.pif_pouf')
9191
;
9292
$container
9393
->register('deprecated_service', 'stdClass')

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
</service>
8787
<service id="decorated" class="stdClass"/>
8888
<service id="decorator_service" class="stdClass" decorates="decorated"/>
89-
<service id="decorator_service_with_name" class="stdClass" decorates="decorated" decoration-inner-name="decorated.pif-pouf"/>
89+
<service id="decorator_service_with_name" class="stdClass" decorates="decorated" decoration-inner-name="decorated.pif_pouf"/>
9090
<service id="deprecated_service" class="stdClass">
9191
<deprecated>The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.</deprecated>
9292
</service>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ services:
7575
decorator_service_with_name:
7676
class: stdClass
7777
decorates: decorated
78-
decoration_inner_name: decorated.pif-pouf
78+
decoration_inner_name: decorated.pif_pouf
7979
deprecated_service:
8080
class: stdClass
8181
deprecated: The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.

0 commit comments

Comments
 (0)