Skip to content

Commit 3c7e18f

Browse files
committed
[FrameworkBundle][WebProfilerBundle] Move ProfilerPass to the WebProfiler bundle
1 parent 60d7d43 commit 3c7e18f

File tree

10 files changed

+181
-39
lines changed

10 files changed

+181
-39
lines changed

UPGRADE-3.3.md

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ FrameworkBundle
3434
---------------
3535

3636
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass` has been deprecated. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead.
37+
38+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass` class has been deprecated and will be removed in 4.0, use the
39+
`Symfony\Bundle\WebProfilerBundle\DependencyInjection\Compiler\ProfilerPass` class instead.
3740

3841
HttpKernel
3942
-----------

UPGRADE-4.0.md

+3
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ FrameworkBundle
156156

157157
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass` has been removed. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead.
158158

159+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass` class has been removed, use the
160+
`Symfony\Bundle\WebProfilerBundle\DependencyInjection\Compiler\ProfilerPass` class instead.
161+
159162
SecurityBundle
160163
--------------
161164

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
is disabled.
1313
* Added `GlobalVariables::getToken()`
1414
* Deprecated `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass`. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead.
15+
* Deprecated `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass`. Use `Symfony\Bundle\WebProfilerBundle\DependencyInjection\Compiler\ProfilerPass` instead.
1516

1617
3.2.0
1718
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ProfilerPass.php

+6-37
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,17 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
1313

14-
use Symfony\Component\DependencyInjection\Reference;
15-
use Symfony\Component\DependencyInjection\ContainerBuilder;
16-
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17-
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
14+
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use Symfony\Bundle\WebProfilerBundle\DependencyInjection\Compiler\ProfilerPass instead.', ProfilerPass::class), E_USER_DEPRECATED);
15+
16+
use Symfony\Bundle\WebProfilerBundle\DependencyInjection\Compiler\ProfilerPass as BaseProfilerPass;
1817

1918
/**
2019
* Adds tagged data_collector services to profiler service.
2120
*
21+
* @deprecated since version 3.3, to be removed in 4.0. Use {@link BaseProfilerPass} instead.
22+
*
2223
* @author Fabien Potencier <fabien@symfony.com>
2324
*/
24-
class ProfilerPass implements CompilerPassInterface
25+
class ProfilerPass extends BaseProfilerPass
2526
{
26-
public function process(ContainerBuilder $container)
27-
{
28-
if (false === $container->hasDefinition('profiler')) {
29-
return;
30-
}
31-
32-
$definition = $container->getDefinition('profiler');
33-
34-
$collectors = new \SplPriorityQueue();
35-
$order = PHP_INT_MAX;
36-
foreach ($container->findTaggedServiceIds('data_collector') as $id => $attributes) {
37-
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
38-
$template = null;
39-
40-
if (isset($attributes[0]['template'])) {
41-
if (!isset($attributes[0]['id'])) {
42-
throw new InvalidArgumentException(sprintf('Data collector service "%s" must have an id attribute in order to specify a template', $id));
43-
}
44-
$template = array($attributes[0]['id'], $attributes[0]['template']);
45-
}
46-
47-
$collectors->insert(array($id, $template), array($priority, --$order));
48-
}
49-
50-
$templates = array();
51-
foreach ($collectors as $collector) {
52-
$definition->addMethodCall('add', array(new Reference($collector[0])));
53-
$templates[$collector[0]] = $collector[1];
54-
}
55-
56-
$container->setParameter('data_collector.templates', $templates);
57-
}
5827
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass;
2323
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
2424
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
25-
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
2625
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass;
2726
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
2827
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass;
@@ -36,6 +35,7 @@
3635
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
3736
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConfigCachePass;
3837
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ValidateWorkflowsPass;
38+
use Symfony\Bundle\WebProfilerBundle\DependencyInjection\Compiler\ProfilerPass;
3939
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
4040
use Symfony\Component\Debug\ErrorHandler;
4141
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -74,7 +74,7 @@ public function build(ContainerBuilder $container)
7474
parent::build($container);
7575

7676
$container->addCompilerPass(new RoutingResolverPass());
77-
$container->addCompilerPass(new ProfilerPass());
77+
$this->addCompilerPassIfExists($container, ProfilerPass::class);
7878
// must be registered before removing private services as some might be listeners/subscribers
7979
// but as late as possible to get resolved parameters
8080
$container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
@@ -109,4 +109,11 @@ public function build(ContainerBuilder $container)
109109
$container->addCompilerPass(new CacheCollectorPass());
110110
}
111111
}
112+
113+
private function addCompilerPassIfExists(ContainerBuilder $container, $class, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, $priority = 0)
114+
{
115+
if (class_exists($class)) {
116+
$container->addCompilerPass(new $class(), $type, $priority);
117+
}
118+
}
112119
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/ProfilerPassTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Symfony\Component\DependencyInjection\Definition;
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
1616

17+
/**
18+
* @group legacy
19+
*/
1720
class ProfilerPassTest extends \PHPUnit_Framework_TestCase
1821
{
1922
private $profilerDefinition;

src/Symfony/Bundle/FrameworkBundle/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"symfony/validator": "~3.2",
5151
"symfony/yaml": "~3.2",
5252
"symfony/property-info": "~2.8|~3.0",
53+
"symfony/web-profiler-bundle": "<3.3",
5354
"doctrine/annotations": "~1.0",
5455
"phpdocumentor/reflection-docblock": "^3.0",
5556
"twig/twig": "~1.26|~2.0"

src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* added `ProfilerPass`
8+
49
3.1.0
510
-----
611

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\WebProfilerBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Reference;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18+
19+
/**
20+
* Adds tagged data_collector services to profiler service.
21+
*
22+
* @author Fabien Potencier <fabien@symfony.com>
23+
*/
24+
class ProfilerPass implements CompilerPassInterface
25+
{
26+
public function process(ContainerBuilder $container)
27+
{
28+
if (false === $container->hasDefinition('profiler')) {
29+
return;
30+
}
31+
32+
$definition = $container->getDefinition('profiler');
33+
34+
$collectors = new \SplPriorityQueue();
35+
$order = PHP_INT_MAX;
36+
foreach ($container->findTaggedServiceIds('data_collector') as $id => $attributes) {
37+
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
38+
$template = null;
39+
40+
if (isset($attributes[0]['template'])) {
41+
if (!isset($attributes[0]['id'])) {
42+
throw new InvalidArgumentException(sprintf('Data collector service "%s" must have an id attribute in order to specify a template', $id));
43+
}
44+
$template = array($attributes[0]['id'], $attributes[0]['template']);
45+
}
46+
47+
$collectors->insert(array($id, $template), array($priority, --$order));
48+
}
49+
50+
$templates = array();
51+
foreach ($collectors as $collector) {
52+
$definition->addMethodCall('add', array(new Reference($collector[0])));
53+
$templates[$collector[0]] = $collector[1];
54+
}
55+
56+
$container->setParameter('data_collector.templates', $templates);
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\WebProfilerBundle\Tests\DependencyInjection\Compiler;
13+
14+
use Symfony\Bundle\WebProfilerBundle\DependencyInjection\Compiler\ProfilerPass;
15+
use Symfony\Component\DependencyInjection\Definition;
16+
17+
class ProfilerPassTest extends \PHPUnit_Framework_TestCase
18+
{
19+
private $profilerDefinition;
20+
21+
protected function setUp()
22+
{
23+
$this->profilerDefinition = new Definition('ProfilerClass');
24+
}
25+
26+
/**
27+
* Tests that collectors that specify a template but no "id" will throw
28+
* an exception (both are needed if the template is specified).
29+
*
30+
* Thus, a fully-valid tag looks something like this:
31+
*
32+
* <tag name="data_collector" template="YourBundle:Collector:templatename" id="your_collector_name" />
33+
*/
34+
public function testTemplateNoIdThrowsException()
35+
{
36+
// one service, with a template key, but no id
37+
$services = array(
38+
'my_collector_service' => array(0 => array('template' => 'foo')),
39+
);
40+
41+
$builder = $this->createContainerMock($services);
42+
43+
$this->setExpectedException('InvalidArgumentException');
44+
45+
$profilerPass = new ProfilerPass();
46+
$profilerPass->process($builder);
47+
}
48+
49+
public function testValidCollector()
50+
{
51+
// one service, with a template key, but no id
52+
$services = array(
53+
'my_collector_service' => array(0 => array('template' => 'foo', 'id' => 'my_collector')),
54+
);
55+
56+
$container = $this->createContainerMock($services);
57+
58+
// fake the getDefinition() to return a Profiler definition
59+
$container->expects($this->atLeastOnce())
60+
->method('getDefinition');
61+
62+
// assert that the data_collector.templates parameter should be set
63+
$container->expects($this->once())
64+
->method('setParameter')
65+
->with('data_collector.templates', array('my_collector_service' => array('my_collector', 'foo')));
66+
67+
$profilerPass = new ProfilerPass();
68+
$profilerPass->process($container);
69+
70+
// grab the method calls off of the "profiler" definition
71+
$methodCalls = $this->profilerDefinition->getMethodCalls();
72+
$this->assertCount(1, $methodCalls);
73+
$this->assertEquals('add', $methodCalls[0][0]); // grab the method part of the first call
74+
}
75+
76+
private function createContainerMock($services)
77+
{
78+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'setParameter'))->getMock();
79+
$container->expects($this->any())
80+
->method('hasDefinition')
81+
->with($this->equalTo('profiler'))
82+
->will($this->returnValue(true));
83+
$container->expects($this->any())
84+
->method('getDefinition')
85+
->will($this->returnValue($this->profilerDefinition));
86+
$container->expects($this->atLeastOnce())
87+
->method('findTaggedServiceIds')
88+
->will($this->returnValue($services));
89+
90+
return $container;
91+
}
92+
}

0 commit comments

Comments
 (0)