Skip to content

[DI] Deprecate dumping an uncompiled container #20634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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 @@ -72,6 +72,10 @@ class PhpDumper extends Dumper
*/
public function __construct(ContainerBuilder $container)
{
if (!$container->isFrozen()) {
@trigger_error('Dumping an uncompiled ContainerBuilder is deprecated since version 3.3 and will not be supported anymore in 4.0. Compile the container beforehand.', E_USER_DEPRECATED);
}

parent::__construct($container);

$this->inlinedDefinitions = new \SplObjectStorage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ public static function setUpBeforeClass()

public function testDump()
{
$dumper = new PhpDumper($container = new ContainerBuilder());
$container = new ContainerBuilder();
$container->compile();
$dumper = new PhpDumper($container);

$this->assertStringEqualsFile(self::$fixturesPath.'/php/services1.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services1-1.php', $dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer', 'namespace' => 'Symfony\Component\DependencyInjection\Dump')), '->dump() takes a class and a base_class options');

$container = new ContainerBuilder();
$container->compile();
new PhpDumper($container);
}

Expand Down Expand Up @@ -97,7 +100,9 @@ public function testDumpRelativeDir()
*/
public function testExportParameters($parameters)
{
$dumper = new PhpDumper(new ContainerBuilder(new ParameterBag($parameters)));
$container = new ContainerBuilder(new ParameterBag($parameters));
$container->compile();
$dumper = new PhpDumper($container);
$dumper->dump();
}

Expand All @@ -114,25 +119,33 @@ public function provideInvalidParameters()
public function testAddParameters()
{
$container = include self::$fixturesPath.'/containers/container8.php';
$container->compile();
$dumper = new PhpDumper($container);
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
}

public function testAddService()
/**
* @group legacy
* @expectedDeprecation Dumping an uncompiled ContainerBuilder is deprecated since version 3.3 and will not be supported anymore in 4.0. Compile the container beforehand.
*/
public function testAddServiceWithoutCompilation()
{
// without compilation
$container = include self::$fixturesPath.'/containers/container9.php';
$dumper = new PhpDumper($container);
$this->assertEquals(str_replace('%path%', str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9.php')), $dumper->dump(), '->dump() dumps services');
}

// with compilation
public function testAddService()
{
$container = include self::$fixturesPath.'/containers/container9.php';
$container->compile();
$dumper = new PhpDumper($container);
$this->assertEquals(str_replace('%path%', str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), file_get_contents(self::$fixturesPath.'/php/services9_compiled.php')), $dumper->dump(), '->dump() dumps services');

$dumper = new PhpDumper($container = new ContainerBuilder());
$container = new ContainerBuilder();
$container->register('foo', 'FooClass')->addArgument(new \stdClass());
$container->compile();
$dumper = new PhpDumper($container);
try {
$dumper->dump();
$this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
Expand All @@ -145,6 +158,7 @@ public function testAddService()
public function testServicesWithAnonymousFactories()
{
$container = include self::$fixturesPath.'/containers/container19.php';
$container->compile();
$dumper = new PhpDumper($container);

$this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories');
Expand All @@ -156,6 +170,7 @@ public function testAddServiceIdWithUnsupportedCharacters()
$container = new ContainerBuilder();
$container->register('bar$', 'FooClass');
$container->register('bar$!', 'FooClass');
$container->compile();
$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump(array('class' => $class)));

Expand All @@ -169,6 +184,7 @@ public function testConflictingServiceIds()
$container = new ContainerBuilder();
$container->register('foo_bar', 'FooClass');
$container->register('foobar', 'FooClass');
$container->compile();
$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump(array('class' => $class)));

Expand All @@ -182,6 +198,7 @@ public function testConflictingMethodsWithParent()
$container = new ContainerBuilder();
$container->register('bar', 'FooClass');
$container->register('foo_bar', 'FooClass');
$container->compile();
$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump(array(
'class' => $class,
Expand All @@ -203,6 +220,7 @@ public function testInvalidFactories($factory)
$def = new Definition('stdClass');
$def->setFactory($factory);
$container->setDefinition('bar', $def);
$container->compile();
$dumper = new PhpDumper($container);
$dumper->dump();
}
Expand Down Expand Up @@ -285,6 +303,7 @@ public function testCircularReference()
public function testDumpAutowireData()
{
$container = include self::$fixturesPath.'/containers/container24.php';
$container->compile();
$dumper = new PhpDumper($container);

$this->assertEquals(file_get_contents(self::$fixturesPath.'/php/services24.php'), $dumper->dump());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
* Container.
Expand All @@ -24,6 +24,24 @@ class Container extends AbstractContainer
*/
public function __construct()
{
parent::__construct();
$this->services = array();

$this->aliases = array();
}

/**
* {@inheritdoc}
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
}

/**
* {@inheritdoc}
*/
public function isFrozen()
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
* ProjectServiceContainer.
Expand All @@ -23,6 +23,24 @@ class ProjectServiceContainer extends Container
*/
public function __construct()
{
parent::__construct();
$this->services = array();

$this->aliases = array();
}

/**
* {@inheritdoc}
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
}

/**
* {@inheritdoc}
*/
public function isFrozen()
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
* ProjectServiceContainer.
Expand All @@ -23,11 +23,29 @@ class ProjectServiceContainer extends Container
*/
public function __construct()
{
parent::__construct();
$this->services = array();
$this->methodMap = array(
'service_from_anonymous_factory' => 'getServiceFromAnonymousFactoryService',
'service_with_method_call_and_factory' => 'getServiceWithMethodCallAndFactoryService',
);

$this->aliases = array();
}

/**
* {@inheritdoc}
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
}

/**
* {@inheritdoc}
*/
public function isFrozen()
{
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
* ProjectServiceContainer.
Expand All @@ -23,10 +23,28 @@ class ProjectServiceContainer extends Container
*/
public function __construct()
{
parent::__construct();
$this->services = array();
$this->methodMap = array(
'foo' => 'getFooService',
);

$this->aliases = array();
}

/**
* {@inheritdoc}
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
}

/**
* {@inheritdoc}
*/
public function isFrozen()
{
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
* ProjectServiceContainer.
Expand All @@ -23,7 +23,95 @@ class ProjectServiceContainer extends Container
*/
public function __construct()
{
parent::__construct(new ParameterBag($this->getDefaultParameters()));
$this->parameters = $this->getDefaultParameters();

$this->services = array();

$this->aliases = array();
}

/**
* {@inheritdoc}
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
}

/**
* {@inheritdoc}
*/
public function isFrozen()
{
return true;
}

/**
* {@inheritdoc}
*/
public function getParameter($name)
{
$name = strtolower($name);

if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters) || isset($this->loadedDynamicParameters[$name]))) {
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
}
if (isset($this->loadedDynamicParameters[$name])) {
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
}

return $this->parameters[$name];
}

/**
* {@inheritdoc}
*/
public function hasParameter($name)
{
$name = strtolower($name);

return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters) || isset($this->loadedDynamicParameters[$name]);
}

/**
* {@inheritdoc}
*/
public function setParameter($name, $value)
{
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
}

/**
* {@inheritdoc}
*/
public function getParameterBag()
{
if (null === $this->parameterBag) {
$parameters = $this->parameters;
foreach ($this->loadedDynamicParameters as $name => $loaded) {
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
}
$this->parameterBag = new FrozenParameterBag($parameters);
}

return $this->parameterBag;
}

private $loadedDynamicParameters = array();
private $dynamicParameters = array();

/**
* Computes a dynamic parameter.
*
* @param string The name of the dynamic parameter to load
*
* @return mixed The value of the dynamic parameter
*
* @throws InvalidArgumentException When the dynamic parameter does not exist
*/
private function getDynamicParameter($name)
{
throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
}

/**
Expand All @@ -34,9 +122,9 @@ public function __construct()
protected function getDefaultParameters()
{
return array(
'foo' => '%baz%',
'foo' => 'bar',
'baz' => 'bar',
'bar' => 'foo is %%foo bar',
'bar' => 'foo is %foo bar',
'escape' => '@escapeme',
'values' => array(
0 => true,
Expand Down