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

Skip to content

Commit 097ae80

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

File tree

7 files changed

+160
-4
lines changed

7 files changed

+160
-4
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
*/
5959
class Container implements ResettableContainerInterface
6060
{
61+
/**
62+
* @internal
63+
*/
64+
const INVALID_SERVICE_ID_REGEX = '/[\s\x00-\x1F\x7F!?"\'`@-]/';
65+
6166
/**
6267
* @var ParameterBagInterface
6368
*/
@@ -163,6 +168,10 @@ public function setParameter($name, $value)
163168
*/
164169
public function set($id, $service)
165170
{
171+
if (preg_match(self::INVALID_SERVICE_ID_REGEX, $id)) {
172+
@trigger_error(sprintf('Using whitespaces, control sequences, quotes, or the characters "!", "?", "@", "`" and "-" in service ids ("%s" given) is deprecated since Symfony 3.1 and will throw an exception in 4.0.', $id), E_USER_DEPRECATED);
173+
}
174+
166175
$id = strtolower($id);
167176

168177
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(self::INVALID_SERVICE_ID_REGEX, $alias)) {
617+
@trigger_error(sprintf('Using whitespaces, control sequences, quotes, or the characters "!", "?", "@", "`" 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(self::INVALID_SERVICE_ID_REGEX, $id)) {
739+
@trigger_error(sprintf('Using whitespaces, control sequences, quotes, or the characters "!", "?", "@", "`" 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: 103 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,109 @@ 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 whitespaces, control sequences, quotes, or the characters "!", "?", "@", "`" 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 whitespaces, control sequences, quotes, or the characters "!", "?", "@", "`" 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 whitespaces, control sequences, quotes, or the characters "!", "?", "@", "`" 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 whitespaces, control sequences, quotes, or the characters "!", "?", "@", "`" 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 whitespace' => array('service id'),
824+
'contains ASCII control sequence' => array("service\x07id"),
825+
'contains exclamation mark' => array('service!'),
826+
'contains question mark' => array('service?'),
827+
'contains @' => array('@service_id'),
828+
'contains double quote' => array('"my_service"'),
829+
'contains single quote' => array("'my_service'"),
830+
'contains backtick' => array('`my_service`'),
831+
'contains dash' => array('my-service'),
832+
);
833+
}
732834
}
733835

734836
class FooClass

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,43 @@ 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 whitespaces, control sequences, quotes, or the characters "!", "?", "@", "`" 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 whitespace' => array('service id'),
179+
'contains ASCII control sequence' => array("service\x07id"),
180+
'contains exclamation mark' => array('service!'),
181+
'contains question mark' => array('service?'),
182+
'contains @' => array('@service_id'),
183+
'contains double quote' => array('"my_service"'),
184+
'contains single quote' => array("'my_service'"),
185+
'contains backtick' => array('`my_service`'),
186+
'contains dash' => array('my-service'),
187+
);
188+
}
189+
153190
public function testGet()
154191
{
155192
$sc = new ProjectServiceContainer();

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)