Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merged branch fabpot/resource-tracking (PR #6501)

This PR was merged into the master branch.

Commits
-------

6cd1fd4 [DependencyInjection] removed hard dependency on the Config component

Discussion
----------

[DependencyInjection] removed hard dependency on the Config component

The Config component is a hard dependency for the loaders (but loaders
themselves are optional); all other classes should not have a hard dep
on Config. The introduction of a new flag allows to remove this
dependency.

This commit also fixes skipped test dependencies.

---------------------------------------------------------------------------

by fabpot at 2012-12-28T09:47:13Z

As there is only one location where we are directly using a class from the Config component (`ContainerBuilder::addObjectResource()`), we can also just test this case and do nothing if the class does not exist instead of adding a flag, but that looks dirty.

```php
public function addObjectResource($object)
{
    if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
        return $this;
    }

    $parent = new \ReflectionObject($object);
    do {
        $this->addResource(new FileResource($parent->getFileName()));
    } while ($parent = $parent->getParentClass());

    return $this;
}
```

What do you think?
  • Loading branch information...
commit 132dccb0d16b7172a750cf80ce1316aa47477e59 2 parents d808dfb + 6ec2932
@fabpot fabpot authored
View
1  Compiler/MergeExtensionConfigurationPass.php
@@ -43,6 +43,7 @@ public function process(ContainerBuilder $container)
$config = $container->getParameterBag()->resolveValue($config);
$tmpContainer = new ContainerBuilder($container->getParameterBag());
+ $tmpContainer->setResourceTracking($container->isTrackingResources());
$tmpContainer->addObjectResource($extension);
$extension->load($config, $tmpContainer);
View
49 ContainerBuilder.php
@@ -63,6 +63,31 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*/
private $compiler;
+ private $trackResources = true;
+
+ /**
+ * Sets the track resources flag.
+ *
+ * If you are not using the loaders and therefore don't want
+ * to depend on the Config component, set this flag to false.
+ *
+ * @param Boolean $track true if you want to track resources, false otherwise
+ */
+ public function setResourceTracking($track)
+ {
+ $this->trackResources = (Boolean) $track;
+ }
+
+ /**
+ * Checks if resources are tracked.
+ *
+ * @return Boolean true if resources are tracked, false otherwise
+ */
+ public function isTrackingResources()
+ {
+ return $this->trackResources;
+ }
+
/**
* Registers an extension.
*
@@ -152,6 +177,10 @@ public function getResources()
*/
public function addResource(ResourceInterface $resource)
{
+ if (!$this->trackResources) {
+ return $this;
+ }
+
$this->resources[] = $resource;
return $this;
@@ -168,6 +197,10 @@ public function addResource(ResourceInterface $resource)
*/
public function setResources(array $resources)
{
+ if (!$this->trackResources) {
+ return $this;
+ }
+
$this->resources = $resources;
return $this;
@@ -184,6 +217,10 @@ public function setResources(array $resources)
*/
public function addObjectResource($object)
{
+ if (!$this->trackResources) {
+ return $this;
+ }
+
$parent = new \ReflectionObject($object);
do {
$this->addResource(new FileResource($parent->getFileName()));
@@ -437,8 +474,10 @@ public function merge(ContainerBuilder $container)
$this->addAliases($container->getAliases());
$this->getParameterBag()->add($container->getParameterBag()->all());
- foreach ($container->getResources() as $resource) {
- $this->addResource($resource);
+ if ($this->trackResources) {
+ foreach ($container->getResources() as $resource) {
+ $this->addResource($resource);
+ }
}
foreach ($this->extensions as $name => $extension) {
@@ -505,8 +544,10 @@ public function compile()
$this->compiler = new Compiler();
}
- foreach ($this->compiler->getPassConfig()->getPasses() as $pass) {
- $this->addObjectResource($pass);
+ if ($this->trackResources) {
+ foreach ($this->compiler->getPassConfig()->getPasses() as $pass) {
+ $this->addObjectResource($pass);
+ }
}
$this->compiler->compile($this);
View
10 Tests/Compiler/IntegrationTest.php
@@ -20,13 +20,6 @@
*/
class IntegrationTest extends \PHPUnit_Framework_TestCase
{
- protected function setUp()
- {
- if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
- }
-
/**
* This tests that the following dependencies are correctly processed:
*
@@ -37,6 +30,7 @@ protected function setUp()
public function testProcessRemovesAndInlinesRecursively()
{
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$a = $container
->register('a', '\stdClass')
@@ -66,6 +60,7 @@ public function testProcessRemovesAndInlinesRecursively()
public function testProcessInlinesReferencesToAliases()
{
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$a = $container
->register('a', '\stdClass')
@@ -91,6 +86,7 @@ public function testProcessInlinesReferencesToAliases()
public function testProcessInlinesWhenThereAreMultipleReferencesButFromTheSameDefinition()
{
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container
->register('a', '\stdClass')
View
49 Tests/ContainerBuilderTest.php
@@ -210,11 +210,8 @@ public function testAddAliases()
*/
public function testAddGetCompilerPass()
{
- if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
-
$builder = new ContainerBuilder();
+ $builder->setResourceTracking(false);
$builderCompilerPasses = $builder->getCompiler()->getPassConfig()->getPasses();
$builder->addCompilerPass($this->getMock('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'));
$this->assertEquals(sizeof($builderCompilerPasses) + 1, sizeof($builder->getCompiler()->getPassConfig()->getPasses()));
@@ -335,16 +332,14 @@ public function testResolveServices()
*/
public function testMerge()
{
- if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
-
$container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
+ $container->setResourceTracking(false);
$config = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
$container->merge($config);
$this->assertEquals(array('bar' => 'foo', 'foo' => 'bar'), $container->getParameterBag()->all(), '->merge() merges current parameters with the loaded ones');
$container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
+ $container->setResourceTracking(false);
$config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%')));
$container->merge($config);
////// FIXME
@@ -352,6 +347,7 @@ public function testMerge()
$this->assertEquals(array('bar' => 'foo', 'foo' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
$container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
+ $container->setResourceTracking(false);
$config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%', 'baz' => '%foo%')));
$container->merge($config);
////// FIXME
@@ -359,6 +355,7 @@ public function testMerge()
$this->assertEquals(array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container->register('foo', 'FooClass');
$container->register('bar', 'BarClass');
$config = new ContainerBuilder();
@@ -372,6 +369,7 @@ public function testMerge()
$this->assertEquals('foo', (string) $aliases['alias_for_foo']);
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container->register('foo', 'FooClass');
$config->setDefinition('foo', new Definition('BazClass'));
$container->merge($config);
@@ -384,11 +382,8 @@ public function testMerge()
*/
public function testMergeLogicException()
{
- if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
-
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container->compile();
$container->merge(new ContainerBuilder());
}
@@ -456,11 +451,8 @@ public function testResources()
*/
public function testExtension()
{
- if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
-
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container->registerExtension($extension = new \ProjectExtension());
$this->assertTrue($container->getExtension('project') === $extension, '->registerExtension() registers an extension');
@@ -471,30 +463,24 @@ public function testExtension()
public function testRegisteredButNotLoadedExtension()
{
- if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
-
$extension = $this->getMock('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface');
$extension->expects($this->once())->method('getAlias')->will($this->returnValue('project'));
$extension->expects($this->never())->method('load');
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container->registerExtension($extension);
$container->compile();
}
public function testRegisteredAndLoadedExtension()
{
- if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
-
$extension = $this->getMock('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface');
$extension->expects($this->exactly(2))->method('getAlias')->will($this->returnValue('project'));
$extension->expects($this->once())->method('load')->with(array(array('foo' => 'bar')));
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container->registerExtension($extension);
$container->loadFromExtension('project', array('foo' => 'bar'));
$container->compile();
@@ -502,13 +488,10 @@ public function testRegisteredAndLoadedExtension()
public function testPrivateServiceUser()
{
- if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
-
$fooDefinition = new Definition('BarClass');
$fooUserDefinition = new Definition('BarUserClass', array(new Reference('bar')));
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$fooDefinition->setPublic(false);
@@ -526,11 +509,8 @@ public function testPrivateServiceUser()
*/
public function testThrowsExceptionWhenSetServiceOnAFrozenContainer()
{
- if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
-
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container->setDefinition('a', new Definition('stdClass'));
$container->compile();
$container->set('a', new \stdClass());
@@ -570,11 +550,8 @@ public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()
*/
public function testThrowsExceptionWhenSetDefinitionOnAFrozenContainer()
{
- if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
-
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container->compile();
$container->setDefinition('a', new Definition());
}
View
20 Tests/CrossCheckTest.php
@@ -80,18 +80,24 @@ public function testCrossCheck($fixture, $type)
public function crossCheckLoadersDumpers()
{
- return array(
+ $tests = array(
array('services1.xml', 'xml'),
array('services2.xml', 'xml'),
array('services6.xml', 'xml'),
array('services8.xml', 'xml'),
array('services9.xml', 'xml'),
-
- array('services1.yml', 'yaml'),
- array('services2.yml', 'yaml'),
- array('services6.yml', 'yaml'),
- array('services8.yml', 'yaml'),
- array('services9.yml', 'yaml'),
);
+
+ if (class_exists('Symfony\Component\Yaml\Yaml')) {
+ $tests = array_merge($tests, array(
+ array('services1.yml', 'yaml'),
+ array('services2.yml', 'yaml'),
+ array('services6.yml', 'yaml'),
+ array('services8.yml', 'yaml'),
+ array('services9.yml', 'yaml'),
+ ));
+ }
+
+ return $tests;
}
}
View
7 Tests/Dumper/GraphvizDumperTest.php
@@ -18,13 +18,6 @@ class GraphvizDumperTest extends \PHPUnit_Framework_TestCase
{
protected static $fixturesPath;
- protected function setUp()
- {
- if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
- }
-
public static function setUpBeforeClass()
{
self::$fixturesPath = __DIR__.'/../Fixtures/';
View
9 Tests/Dumper/PhpDumperTest.php
@@ -21,13 +21,6 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
{
protected static $fixturesPath;
- protected function setUp()
- {
- if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
- }
-
public static function setUpBeforeClass()
{
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
@@ -47,6 +40,7 @@ public function testDump()
public function testDumpFrozenContainerWithNoParameter()
{
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container->register('foo', 'stdClass');
$container->compile();
@@ -76,6 +70,7 @@ public function testDumpOptimizationString()
));
$container = new ContainerBuilder();
+ $container->setResourceTracking(false);
$container->setDefinition('test', $definition);
$container->setParameter('empty_value', '');
$container->setParameter('some_string', '-');
View
7 Tests/Dumper/XmlDumperTest.php
@@ -18,13 +18,6 @@ class XmlDumperTest extends \PHPUnit_Framework_TestCase
{
protected static $fixturesPath;
- protected function setUp()
- {
- if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
- $this->markTestSkipped('The "Config" component is not available');
- }
- }
-
public static function setUpBeforeClass()
{
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
View
4 Tests/Dumper/YamlDumperTest.php
@@ -20,8 +20,8 @@ class YamlDumperTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
- if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
- $this->markTestSkipped('The "Config" component is not available');
+ if (!class_exists('Symfony\Component\Yaml\Yaml')) {
+ $this->markTestSkipped('The "Yaml" component is not available');
}
}
View
4 Tests/Loader/XmlFileLoaderTest.php
@@ -98,6 +98,10 @@ public function testLoadParameters()
public function testLoadImports()
{
+ if (!class_exists('Symfony\Component\Yaml\Yaml')) {
+ $this->markTestSkipped('The "Yaml" component is not available');
+ }
+
$container = new ContainerBuilder();
$resolver = new LoaderResolver(array(
new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
Please sign in to comment.
Something went wrong with that request. Please try again.