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

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ public function testProcess($public)
$definition = new Definition('%my-command.class%');
$definition->setPublic($public);
$definition->addTag('console.command');
$container->setDefinition('my-command', $definition);
$container->setDefinition('app.command', $definition);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get why we need to reject -? Could we allow it?


$container->compile();

$alias = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';

if ($container->hasAlias($alias)) {
$this->assertSame('my-command', (string) $container->getAlias($alias));
$this->assertSame('app.command', (string) $container->getAlias($alias));
} else {
// The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
// in case the original service is private
Expand All @@ -59,7 +60,7 @@ public function visibilityProvider()

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract.
* @expectedExceptionMessage The service "app.command" tagged "console.command" must not be abstract.
*/
public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
{
Expand All @@ -69,14 +70,14 @@ public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
$definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand');
$definition->addTag('console.command');
$definition->setAbstract(true);
$container->setDefinition('my-command', $definition);
$container->setDefinition('app.command', $definition);

$container->compile();
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
* @expectedExceptionMessage The service "app.command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
*/
public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
{
Expand All @@ -85,7 +86,7 @@ public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()

$definition = new Definition('SplObjectStorage');
$definition->addTag('console.command');
$container->setDefinition('my-command', $definition);
$container->setDefinition('app.command', $definition);

$container->compile();
}
Expand All @@ -96,7 +97,7 @@ public function testHttpKernelRegisterCommandsIngoreCommandAsAService()
$container->addCompilerPass(new AddConsoleCommandPass());
$definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand');
$definition->addTag('console.command');
$container->setDefinition('my-command', $definition);
$container->setDefinition('app.command', $definition);
$container->compile();

$application = $this->getMock('Symfony\Component\Console\Application');
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
*/
class Container implements ResettableContainerInterface
{
/**
* @internal
*/
const INVALID_SERVICE_ID_REGEX = '/[\s\x00-\x1F\x7F!?"\'`@-]/';

/**
* @var ParameterBagInterface
*/
Expand Down Expand Up @@ -163,6 +168,10 @@ public function setParameter($name, $value)
*/
public function set($id, $service)
{
if (preg_match(self::INVALID_SERVICE_ID_REGEX, $id)) {
@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);
}

$id = strtolower($id);

if ('service_container' === $id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,10 @@ public function setAlias($alias, $id)
throw new InvalidArgumentException(sprintf('An alias can not reference itself, got a circular reference on "%s".', $alias));
}

if (preg_match(self::INVALID_SERVICE_ID_REGEX, $alias)) {
@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);
}

unset($this->definitions[$alias]);

$this->aliasDefinitions[$alias] = $id;
Expand Down Expand Up @@ -731,6 +735,10 @@ public function getDefinitions()
*/
public function setDefinition($id, Definition $definition)
{
if (preg_match(self::INVALID_SERVICE_ID_REGEX, $id)) {
@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);
}

if ($this->isFrozen()) {
throw new BadMethodCallException('Adding definition to a frozen container is not allowed');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
Expand Down Expand Up @@ -732,6 +731,109 @@ public function testAutowiring()

$this->assertEquals('a', (string) $container->getDefinition('b')->getArgument(0));
}

/**
* @dataProvider getSpecialCharServiceIds
*/
public function testSpecialCharInServiceIdTriggersDeprecationInSet($id)
{
$container = new ContainerBuilder();

$deprecations = array();
set_error_handler(function ($type, $msg) use (&$deprecations) {
if (E_USER_DEPRECATED === $type) {
$deprecations[] = $msg;
}
});

$container->set($id, new \stdClass());

restore_error_handler();

$this->assertCount(1, $deprecations);
$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]);
}

/**
* @dataProvider getSpecialCharServiceIds
*/
public function testSpecialCharInServiceIdTriggersDeprecationInRegister($id)
{
$container = new ContainerBuilder();

$deprecations = array();
set_error_handler(function ($type, $msg) use (&$deprecations) {
if (E_USER_DEPRECATED === $type) {
$deprecations[] = $msg;
}
});

$container->register($id, 'Foo');

restore_error_handler();

$this->assertCount(1, $deprecations);
$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]);
}

/**
* @dataProvider getSpecialCharServiceIds
*/
public function testSpecialCharInServiceIdTriggersDeprecationInSetDefinition($id)
{
$container = new ContainerBuilder();

$deprecations = array();
set_error_handler(function ($type, $msg) use (&$deprecations) {
if (E_USER_DEPRECATED === $type) {
$deprecations[] = $msg;
}
});

$container->setDefinition($id, new Definition('Foo'));

restore_error_handler();

$this->assertCount(1, $deprecations);
$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]);
}

/**
* @dataProvider getSpecialCharServiceIds
*/
public function testSpecialCharInServiceIdTriggersDeprecationInSetAlias($id)
{
$container = new ContainerBuilder();

$deprecations = array();
set_error_handler(function ($type, $msg) use (&$deprecations) {
if (E_USER_DEPRECATED === $type) {
$deprecations[] = $msg;
}
});

$container->setAlias($id, 'foo');

restore_error_handler();

$this->assertCount(1, $deprecations);
$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]);
}

public function getSpecialCharServiceIds()
{
return array(
'contains whitespace' => array('service id'),
'contains ASCII control sequence' => array("service\x07id"),
'contains exclamation mark' => array('service!'),
'contains question mark' => array('service?'),
'contains @' => array('@service_id'),
'contains double quote' => array('"my_service"'),
'contains single quote' => array("'my_service'"),
'contains backtick' => array('`my_service`'),
'contains dash' => array('my-service'),
);
}
}

class FooClass
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,43 @@ public function testSetReplacesAlias()
$this->assertSame($foo, $c->get('alias'), '->set() replaces an existing alias');
}

/**
* @dataProvider getSpecialCharServiceIds
*/
public function testSpecialCharInServiceIdTriggersDeprecationInSet($id)
{
$container = new ProjectServiceContainer();

$deprecations = array();
set_error_handler(function ($type, $msg) use (&$deprecations) {
if (E_USER_DEPRECATED === $type) {
$deprecations[] = $msg;
}
});

$container->set($id, new \stdClass());

restore_error_handler();

$this->assertCount(1, $deprecations);
$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]);
}

public function getSpecialCharServiceIds()
{
return array(
'contains whitespace' => array('service id'),
'contains ASCII control sequence' => array("service\x07id"),
'contains exclamation mark' => array('service!'),
'contains question mark' => array('service?'),
'contains @' => array('@service_id'),
'contains double quote' => array('"my_service"'),
'contains single quote' => array("'my_service'"),
'contains backtick' => array('`my_service`'),
'contains dash' => array('my-service'),
);
}

public function testGet()
{
$sc = new ProjectServiceContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
;
$container
->register('decorator_service_with_name', 'stdClass')
->setDecoratedService('decorated', 'decorated.pif-pouf')
->setDecoratedService('decorated', 'decorated.pif_pouf')
;
$container
->register('deprecated_service', 'stdClass')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</service>
<service id="decorated" class="stdClass"/>
<service id="decorator_service" class="stdClass" decorates="decorated"/>
<service id="decorator_service_with_name" class="stdClass" decorates="decorated" decoration-inner-name="decorated.pif-pouf"/>
<service id="decorator_service_with_name" class="stdClass" decorates="decorated" decoration-inner-name="decorated.pif_pouf"/>
<service id="deprecated_service" class="stdClass">
<deprecated>The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.</deprecated>
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ services:
decorator_service_with_name:
class: stdClass
decorates: decorated
decoration_inner_name: decorated.pif-pouf
decoration_inner_name: decorated.pif_pouf
deprecated_service:
class: stdClass
deprecated: The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.
Expand Down