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

Skip to content

Commit 524a810

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

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

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
@@ -601,6 +601,10 @@ public function setAliases(array $aliases)
601601
*/
602602
public function setAlias($alias, $id)
603603
{
604+
if (preg_match('/[^a-z0-9\._]/i', $alias)) {
605+
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);
606+
}
607+
604608
$alias = strtolower($alias);
605609

606610
if (is_string($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/ContainerBuilderTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,103 @@ public function testAutowiring()
729729

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

0 commit comments

Comments
 (0)