Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bd0ad92

Browse filesBrowse files
committed
[DependencyInjection] Allow frozen containers to be dumped to graphviz
1 parent 7a35cb7 commit bd0ad92
Copy full SHA for bd0ad92

File tree

6 files changed

+75
-2
lines changed
Filter options

6 files changed

+75
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php
+22-2Lines 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
*
+13Lines 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;
+11Lines 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();
+8Lines 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+
}
+7Lines 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

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Component/DependencyInjection/Dumper/GraphvizDumperTest.php
+14Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.