Skip to content

Commit 16702fc

Browse files
committed
merged branch fabpot/resource-tracking (PR symfony#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?
2 parents fc43ae4 + 6cd1fd4 commit 16702fc

File tree

10 files changed

+83
-77
lines changed

10 files changed

+83
-77
lines changed

src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function process(ContainerBuilder $container)
4343
$config = $container->getParameterBag()->resolveValue($config);
4444

4545
$tmpContainer = new ContainerBuilder($container->getParameterBag());
46+
$tmpContainer->setResourceTracking($container->isTrackingResources());
4647
$tmpContainer->addObjectResource($extension);
4748

4849
$extension->load($config, $tmpContainer);

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
6363
*/
6464
private $compiler;
6565

66+
private $trackResources = true;
67+
68+
/**
69+
* Sets the track resources flag.
70+
*
71+
* If you are not using the loaders and therefore don't want
72+
* to depend on the Config component, set this flag to false.
73+
*
74+
* @param Boolean $track true if you want to track resources, false otherwise
75+
*/
76+
public function setResourceTracking($track)
77+
{
78+
$this->trackResources = (Boolean) $track;
79+
}
80+
81+
/**
82+
* Checks if resources are tracked.
83+
*
84+
* @return Boolean true if resources are tracked, false otherwise
85+
*/
86+
public function isTrackingResources()
87+
{
88+
return $this->trackResources;
89+
}
90+
6691
/**
6792
* Registers an extension.
6893
*
@@ -152,6 +177,10 @@ public function getResources()
152177
*/
153178
public function addResource(ResourceInterface $resource)
154179
{
180+
if (!$this->trackResources) {
181+
return $this;
182+
}
183+
155184
$this->resources[] = $resource;
156185

157186
return $this;
@@ -168,6 +197,10 @@ public function addResource(ResourceInterface $resource)
168197
*/
169198
public function setResources(array $resources)
170199
{
200+
if (!$this->trackResources) {
201+
return $this;
202+
}
203+
171204
$this->resources = $resources;
172205

173206
return $this;
@@ -184,6 +217,10 @@ public function setResources(array $resources)
184217
*/
185218
public function addObjectResource($object)
186219
{
220+
if (!$this->trackResources) {
221+
return $this;
222+
}
223+
187224
$parent = new \ReflectionObject($object);
188225
do {
189226
$this->addResource(new FileResource($parent->getFileName()));
@@ -437,8 +474,10 @@ public function merge(ContainerBuilder $container)
437474
$this->addAliases($container->getAliases());
438475
$this->getParameterBag()->add($container->getParameterBag()->all());
439476

440-
foreach ($container->getResources() as $resource) {
441-
$this->addResource($resource);
477+
if ($this->trackResources) {
478+
foreach ($container->getResources() as $resource) {
479+
$this->addResource($resource);
480+
}
442481
}
443482

444483
foreach ($this->extensions as $name => $extension) {
@@ -505,8 +544,10 @@ public function compile()
505544
$this->compiler = new Compiler();
506545
}
507546

508-
foreach ($this->compiler->getPassConfig()->getPasses() as $pass) {
509-
$this->addObjectResource($pass);
547+
if ($this->trackResources) {
548+
foreach ($this->compiler->getPassConfig()->getPasses() as $pass) {
549+
$this->addObjectResource($pass);
550+
}
510551
}
511552

512553
$this->compiler->compile($this);

src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@
2020
*/
2121
class IntegrationTest extends \PHPUnit_Framework_TestCase
2222
{
23-
protected function setUp()
24-
{
25-
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
26-
$this->markTestSkipped('The "Config" component is not available');
27-
}
28-
}
29-
3023
/**
3124
* This tests that the following dependencies are correctly processed:
3225
*
@@ -37,6 +30,7 @@ protected function setUp()
3730
public function testProcessRemovesAndInlinesRecursively()
3831
{
3932
$container = new ContainerBuilder();
33+
$container->setResourceTracking(false);
4034

4135
$a = $container
4236
->register('a', '\stdClass')
@@ -66,6 +60,7 @@ public function testProcessRemovesAndInlinesRecursively()
6660
public function testProcessInlinesReferencesToAliases()
6761
{
6862
$container = new ContainerBuilder();
63+
$container->setResourceTracking(false);
6964

7065
$a = $container
7166
->register('a', '\stdClass')
@@ -91,6 +86,7 @@ public function testProcessInlinesReferencesToAliases()
9186
public function testProcessInlinesWhenThereAreMultipleReferencesButFromTheSameDefinition()
9287
{
9388
$container = new ContainerBuilder();
89+
$container->setResourceTracking(false);
9490

9591
$container
9692
->register('a', '\stdClass')

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

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,8 @@ public function testAddAliases()
210210
*/
211211
public function testAddGetCompilerPass()
212212
{
213-
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
214-
$this->markTestSkipped('The "Config" component is not available');
215-
}
216-
217213
$builder = new ContainerBuilder();
214+
$builder->setResourceTracking(false);
218215
$builderCompilerPasses = $builder->getCompiler()->getPassConfig()->getPasses();
219216
$builder->addCompilerPass($this->getMock('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'));
220217
$this->assertEquals(sizeof($builderCompilerPasses) + 1, sizeof($builder->getCompiler()->getPassConfig()->getPasses()));
@@ -335,30 +332,30 @@ public function testResolveServices()
335332
*/
336333
public function testMerge()
337334
{
338-
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
339-
$this->markTestSkipped('The "Config" component is not available');
340-
}
341-
342335
$container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
336+
$container->setResourceTracking(false);
343337
$config = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
344338
$container->merge($config);
345339
$this->assertEquals(array('bar' => 'foo', 'foo' => 'bar'), $container->getParameterBag()->all(), '->merge() merges current parameters with the loaded ones');
346340

347341
$container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
342+
$container->setResourceTracking(false);
348343
$config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%')));
349344
$container->merge($config);
350345
////// FIXME
351346
$container->compile();
352347
$this->assertEquals(array('bar' => 'foo', 'foo' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
353348

354349
$container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
350+
$container->setResourceTracking(false);
355351
$config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%', 'baz' => '%foo%')));
356352
$container->merge($config);
357353
////// FIXME
358354
$container->compile();
359355
$this->assertEquals(array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
360356

361357
$container = new ContainerBuilder();
358+
$container->setResourceTracking(false);
362359
$container->register('foo', 'FooClass');
363360
$container->register('bar', 'BarClass');
364361
$config = new ContainerBuilder();
@@ -372,6 +369,7 @@ public function testMerge()
372369
$this->assertEquals('foo', (string) $aliases['alias_for_foo']);
373370

374371
$container = new ContainerBuilder();
372+
$container->setResourceTracking(false);
375373
$container->register('foo', 'FooClass');
376374
$config->setDefinition('foo', new Definition('BazClass'));
377375
$container->merge($config);
@@ -384,11 +382,8 @@ public function testMerge()
384382
*/
385383
public function testMergeLogicException()
386384
{
387-
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
388-
$this->markTestSkipped('The "Config" component is not available');
389-
}
390-
391385
$container = new ContainerBuilder();
386+
$container->setResourceTracking(false);
392387
$container->compile();
393388
$container->merge(new ContainerBuilder());
394389
}
@@ -456,11 +451,8 @@ public function testResources()
456451
*/
457452
public function testExtension()
458453
{
459-
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
460-
$this->markTestSkipped('The "Config" component is not available');
461-
}
462-
463454
$container = new ContainerBuilder();
455+
$container->setResourceTracking(false);
464456

465457
$container->registerExtension($extension = new \ProjectExtension());
466458
$this->assertTrue($container->getExtension('project') === $extension, '->registerExtension() registers an extension');
@@ -471,44 +463,35 @@ public function testExtension()
471463

472464
public function testRegisteredButNotLoadedExtension()
473465
{
474-
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
475-
$this->markTestSkipped('The "Config" component is not available');
476-
}
477-
478466
$extension = $this->getMock('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface');
479467
$extension->expects($this->once())->method('getAlias')->will($this->returnValue('project'));
480468
$extension->expects($this->never())->method('load');
481469

482470
$container = new ContainerBuilder();
471+
$container->setResourceTracking(false);
483472
$container->registerExtension($extension);
484473
$container->compile();
485474
}
486475

487476
public function testRegisteredAndLoadedExtension()
488477
{
489-
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
490-
$this->markTestSkipped('The "Config" component is not available');
491-
}
492-
493478
$extension = $this->getMock('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface');
494479
$extension->expects($this->exactly(2))->method('getAlias')->will($this->returnValue('project'));
495480
$extension->expects($this->once())->method('load')->with(array(array('foo' => 'bar')));
496481

497482
$container = new ContainerBuilder();
483+
$container->setResourceTracking(false);
498484
$container->registerExtension($extension);
499485
$container->loadFromExtension('project', array('foo' => 'bar'));
500486
$container->compile();
501487
}
502488

503489
public function testPrivateServiceUser()
504490
{
505-
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
506-
$this->markTestSkipped('The "Config" component is not available');
507-
}
508-
509491
$fooDefinition = new Definition('BarClass');
510492
$fooUserDefinition = new Definition('BarUserClass', array(new Reference('bar')));
511493
$container = new ContainerBuilder();
494+
$container->setResourceTracking(false);
512495

513496
$fooDefinition->setPublic(false);
514497

@@ -526,11 +509,8 @@ public function testPrivateServiceUser()
526509
*/
527510
public function testThrowsExceptionWhenSetServiceOnAFrozenContainer()
528511
{
529-
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
530-
$this->markTestSkipped('The "Config" component is not available');
531-
}
532-
533512
$container = new ContainerBuilder();
513+
$container->setResourceTracking(false);
534514
$container->setDefinition('a', new Definition('stdClass'));
535515
$container->compile();
536516
$container->set('a', new \stdClass());
@@ -570,11 +550,8 @@ public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()
570550
*/
571551
public function testThrowsExceptionWhenSetDefinitionOnAFrozenContainer()
572552
{
573-
if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
574-
$this->markTestSkipped('The "Config" component is not available');
575-
}
576-
577553
$container = new ContainerBuilder();
554+
$container->setResourceTracking(false);
578555
$container->compile();
579556
$container->setDefinition('a', new Definition());
580557
}

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,24 @@ public function testCrossCheck($fixture, $type)
8080

8181
public function crossCheckLoadersDumpers()
8282
{
83-
return array(
83+
$tests = array(
8484
array('services1.xml', 'xml'),
8585
array('services2.xml', 'xml'),
8686
array('services6.xml', 'xml'),
8787
array('services8.xml', 'xml'),
8888
array('services9.xml', 'xml'),
89-
90-
array('services1.yml', 'yaml'),
91-
array('services2.yml', 'yaml'),
92-
array('services6.yml', 'yaml'),
93-
array('services8.yml', 'yaml'),
94-
array('services9.yml', 'yaml'),
9589
);
90+
91+
if (class_exists('Symfony\Component\Yaml\Yaml')) {
92+
$tests = array_merge($tests, array(
93+
array('services1.yml', 'yaml'),
94+
array('services2.yml', 'yaml'),
95+
array('services6.yml', 'yaml'),
96+
array('services8.yml', 'yaml'),
97+
array('services9.yml', 'yaml'),
98+
));
99+
}
100+
101+
return $tests;
96102
}
97103
}

src/Symfony/Component/DependencyInjection/Tests/Dumper/GraphvizDumperTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ class GraphvizDumperTest extends \PHPUnit_Framework_TestCase
1818
{
1919
protected static $fixturesPath;
2020

21-
protected function setUp()
22-
{
23-
if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
24-
$this->markTestSkipped('The "Config" component is not available');
25-
}
26-
}
27-
2821
public static function setUpBeforeClass()
2922
{
3023
self::$fixturesPath = __DIR__.'/../Fixtures/';

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
2121
{
2222
protected static $fixturesPath;
2323

24-
protected function setUp()
25-
{
26-
if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
27-
$this->markTestSkipped('The "Config" component is not available');
28-
}
29-
}
30-
3124
public static function setUpBeforeClass()
3225
{
3326
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
@@ -47,6 +40,7 @@ public function testDump()
4740
public function testDumpFrozenContainerWithNoParameter()
4841
{
4942
$container = new ContainerBuilder();
43+
$container->setResourceTracking(false);
5044
$container->register('foo', 'stdClass');
5145

5246
$container->compile();
@@ -76,6 +70,7 @@ public function testDumpOptimizationString()
7670
));
7771

7872
$container = new ContainerBuilder();
73+
$container->setResourceTracking(false);
7974
$container->setDefinition('test', $definition);
8075
$container->setParameter('empty_value', '');
8176
$container->setParameter('some_string', '-');

src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ class XmlDumperTest extends \PHPUnit_Framework_TestCase
1818
{
1919
protected static $fixturesPath;
2020

21-
protected function setUp()
22-
{
23-
if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
24-
$this->markTestSkipped('The "Config" component is not available');
25-
}
26-
}
27-
2821
public static function setUpBeforeClass()
2922
{
3023
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');

src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class YamlDumperTest extends \PHPUnit_Framework_TestCase
2020

2121
protected function setUp()
2222
{
23-
if (!class_exists('Symfony\Component\Config\Loader\Loader')) {
24-
$this->markTestSkipped('The "Config" component is not available');
23+
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
24+
$this->markTestSkipped('The "Yaml" component is not available');
2525
}
2626
}
2727

0 commit comments

Comments
 (0)