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

Skip to content

Commit 8c250bd

Browse files
committed
merged branch igorw/graphviz-frozen-container-2.0 (PR #7017)
This PR was merged into the 2.0 branch. Commits ------- bd0ad92 [DependencyInjection] Allow frozen containers to be dumped to graphviz Discussion ---------- [DependencyInjection] Allow frozen containers to be dumped to graphviz | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT This PR replaces #7010.
2 parents 7a35cb7 + bd0ad92 commit 8c250bd

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\DependencyInjection\Reference;
1616
use Symfony\Component\DependencyInjection\Parameter;
1717
use Symfony\Component\DependencyInjection\ContainerInterface;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
1820

1921
/**
2022
* GraphvizDumper dumps a service container as a graphviz file.
@@ -159,7 +161,7 @@ private function findNodes()
159161
{
160162
$nodes = array();
161163

162-
$container = clone $this->container;
164+
$container = $this->cloneContainer();
163165

164166
foreach ($container->getDefinitions() as $id => $definition) {
165167
$nodes[$id] = array('class' => str_replace('\\', '\\\\', $this->container->getParameterBag()->resolveValue($definition->getClass())), 'attributes' => array_merge($this->options['node.definition'], array('style' => ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope() ? 'filled' : 'dotted')));
@@ -175,13 +177,31 @@ private function findNodes()
175177
}
176178

177179
if (!$container->hasDefinition($id)) {
178-
$nodes[$id] = array('class' => str_replace('\\', '\\\\', get_class($service)), 'attributes' => $this->options['node.instance']);
180+
$class = ('service_container' === $id) ? get_class($this->container) : get_class($service);
181+
$nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => $this->options['node.instance']);
179182
}
180183
}
181184

182185
return $nodes;
183186
}
184187

188+
private function cloneContainer()
189+
{
190+
$parameterBag = new ParameterBag($this->container->getParameterBag()->all());
191+
192+
$container = new ContainerBuilder($parameterBag);
193+
$container->setDefinitions($this->container->getDefinitions());
194+
$container->setAliases($this->container->getAliases());
195+
foreach ($this->container->getScopes() as $scope) {
196+
$container->addScope($scope);
197+
}
198+
foreach ($this->container->getExtensions() as $extension) {
199+
$container->registerExtension($extension);
200+
}
201+
202+
return $container;
203+
}
204+
185205
/**
186206
* Returns the start dot.
187207
*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerBuilder;
4+
use Symfony\Component\DependencyInjection\Definition;
5+
6+
$container = new ContainerBuilder();
7+
$container->
8+
register('foo', 'FooClass')->
9+
addArgument(new Reference('bar'))
10+
;
11+
$container->compile();
12+
13+
return $container;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Container14;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
7+
class ProjectServiceContainer extends ContainerBuilder
8+
{
9+
}
10+
11+
return new ProjectServiceContainer();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
digraph sc {
2+
ratio="compress"
3+
node [fontsize="11" fontname="Arial" shape="record"];
4+
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
5+
6+
node_foo [label="foo\nFooClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
7+
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
digraph sc {
2+
ratio="compress"
3+
node [fontsize="11" fontname="Arial" shape="record"];
4+
edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
5+
6+
node_service_container [label="service_container\nContainer14\\ProjectServiceContainer\n", shape=record, fillcolor="#9999ff", style="filled"];
7+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,18 @@ public function testDump()
4848
'node.missing' => array('fillcolor' => 'red', 'style' => 'empty'),
4949
)), str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services10-1.dot')), '->dump() dumps services');
5050
}
51+
52+
public function testDumpWithFrozenContainer()
53+
{
54+
$container = include self::$fixturesPath.'/containers/container13.php';
55+
$dumper = new GraphvizDumper($container);
56+
$this->assertEquals(str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services13.dot')), $dumper->dump(), '->dump() dumps services');
57+
}
58+
59+
public function testDumpWithFrozenCustomClassContainer()
60+
{
61+
$container = include self::$fixturesPath.'/containers/container14.php';
62+
$dumper = new GraphvizDumper($container);
63+
$this->assertEquals(str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services14.dot')), $dumper->dump(), '->dump() dumps services');
64+
}
5165
}

0 commit comments

Comments
 (0)