Skip to content

Commit b1af221

Browse files
committed
[TwigComponent] Merge profiler and profiler_collect_components config options
1 parent cb8f1a1 commit b1af221

File tree

6 files changed

+68
-30
lines changed

6 files changed

+68
-30
lines changed

src/TwigComponent/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## 2.32
4+
5+
- Add option `profiler.collect_components` to control component data collection
6+
in the profiler (enabled in debug mode by default)
7+
38
## 2.30
49

510
- Ensure compatibility with PHP 8.5

src/TwigComponent/config/debug.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
->args([
2929
service('ux.twig_component.component_logger_listener'),
3030
service('twig'),
31-
abstract_arg('profiler dump components'),
31+
abstract_arg('profiler collect components'),
3232
])
3333
->tag('data_collector', [
3434
'template' => '@TwigComponent/Collector/twig_component.html.twig',

src/TwigComponent/src/DataCollector/TwigComponentDataCollector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class TwigComponentDataCollector extends AbstractDataCollector implements
3535
public function __construct(
3636
private readonly TwigComponentLoggerListener $logger,
3737
private readonly Environment $twig,
38-
private readonly bool $dumpComponents = true,
38+
private readonly bool $collectComponents = true,
3939
) {
4040
$this->hasStub = class_exists(ClassStub::class);
4141
}
@@ -136,7 +136,7 @@ private function collectDataFromLogger(): void
136136
'render_start' => $profile[0],
137137
];
138138

139-
if ($this->dumpComponents) {
139+
if ($this->collectComponents) {
140140
$renders[$renderId]['component'] = $mountedComponent->getComponent();
141141
}
142142

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ static function (ChildDefinition $definition, AsTwigComponent $attribute) {
150150
$container->setAlias('console.command.stimulus_component_debug', 'ux.twig_component.command.debug')
151151
->setDeprecated('symfony/ux-twig-component', '2.13', '%alias_id%');
152152

153-
if ($container->getParameter('kernel.debug') && $config['profiler']) {
153+
if ($config['profiler']['enabled']) {
154154
$loader->load('debug.php');
155+
155156
$container->getDefinition('ux.twig_component.data_collector')
156-
->setArgument(2, $config['profiler_dump_components'] ?? true);
157+
->setArgument(2, $config['profiler']['collect_components']);
157158
}
158159

159160
$loader->load('cache.php');
@@ -217,13 +218,13 @@ public function getConfigTreeBuilder(): TreeBuilder
217218
->scalarNode('anonymous_template_directory')
218219
->info('Defaults to `components`')
219220
->end()
220-
->booleanNode('profiler')
221-
->info('Enables the profiler for Twig Component (in debug mode)')
222-
->defaultValue('%kernel.debug%')
223-
->end()
224-
->booleanNode('profiler_dump_components')
225-
->info('Dump components in the Twig Component profiler panel')
226-
->defaultTrue()
221+
->arrayNode('profiler')
222+
->info('Enables the profiler for Twig Component')
223+
->canBeEnabled()
224+
->children()
225+
->booleanNode('enabled')->defaultValue('%kernel.debug%')->end()
226+
->booleanNode('collect_components')->info('Collect components instances')->defaultTrue()->end()
227+
->end()
227228
->end()
228229
->scalarNode('controllers_json')
229230
->setDeprecated('symfony/ux-twig-component', '2.18', 'The "twig_component.controllers_json" config option is deprecated, and will be removed in 3.0.')

src/TwigComponent/tests/Unit/DataCollector/TwigComponentDataCollectorTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\UX\TwigComponent\ComponentAttributes;
18+
use Symfony\UX\TwigComponent\ComponentMetadata;
1719
use Symfony\UX\TwigComponent\DataCollector\TwigComponentDataCollector;
20+
use Symfony\UX\TwigComponent\Event\PostRenderEvent;
21+
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
1822
use Symfony\UX\TwigComponent\EventListener\TwigComponentLoggerListener;
23+
use Symfony\UX\TwigComponent\MountedComponent;
1924
use Twig\Environment;
25+
use Twig\Loader\ArrayLoader;
26+
use Twig\Runtime\EscaperRuntime;
2027

2128
/**
2229
* @author Simon André <smn.andre@gmail.com>
@@ -35,7 +42,7 @@ public function testCollectDoesNothing()
3542
$this->assertSame([], $dataCollector->getData());
3643
}
3744

38-
public function testLateCollect()
45+
public function testLateCollectWithNoCollectedData()
3946
{
4047
$logger = new TwigComponentLoggerListener();
4148
$twig = $this->createMock(Environment::class);
@@ -54,6 +61,44 @@ public function testLateCollect()
5461
$this->assertEquals(0.0, $dataCollector->getRenderTime());
5562
}
5663

64+
/**
65+
* @testWith [true]
66+
* [false]
67+
*/
68+
public function testLateCollectWithCollectedData(bool $collectComponents)
69+
{
70+
$logger = new TwigComponentLoggerListener();
71+
$twig = new Environment(new ArrayLoader());
72+
$dataCollector = new TwigComponentDataCollector($logger, $twig, $collectComponents);
73+
74+
// Trigger some events to be logged
75+
$mounted = new MountedComponent('foo', new \stdClass(), new ComponentAttributes([], new EscaperRuntime()));
76+
$eventA = new PreRenderEvent($mounted, new ComponentMetadata(['key' => 'foo', 'template' => 'bar']), []);
77+
$logger->onPreRender($eventA);
78+
$eventB = new PostRenderEvent($mounted);
79+
$logger->onPostRender($eventB);
80+
81+
$dataCollector->lateCollect();
82+
83+
$this->assertSame(1, $dataCollector->getComponentCount());
84+
$this->assertIsIterable($dataCollector->getComponents());
85+
$this->assertNotEmpty($dataCollector->getComponents());
86+
87+
$this->assertSame(1, $dataCollector->getRenderCount());
88+
$this->assertIsIterable($dataCollector->getRenders());
89+
$this->assertNotEmpty($dataCollector->getRenders());
90+
91+
foreach ($dataCollector->getRenders() as $render) {
92+
if ($collectComponents) {
93+
$this->assertNotNull($render['component']);
94+
} else {
95+
$this->assertNull($render['component']);
96+
}
97+
}
98+
99+
$this->assertGreaterThan(0.0, $dataCollector->getRenderTime());
100+
}
101+
57102
public function testReset()
58103
{
59104
$logger = new TwigComponentLoggerListener();

src/TwigComponent/tests/Unit/DependencyInjection/TwigComponentExtensionTest.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ public function testDataCollectorWithDebugMode()
4040
$this->assertTrue($container->getDefinition('ux.twig_component.data_collector')->getArgument(2));
4141
}
4242

43-
public function testDataCollectorWithDumpComponentsDisabled()
43+
public function testDataCollectorWithCollectComponentsDisabled()
4444
{
4545
$container = $this->createContainer();
4646
$container->setParameter('kernel.debug', true);
4747
$container->registerExtension(new TwigComponentExtension());
4848
$container->loadFromExtension('twig_component', [
4949
'defaults' => [],
5050
'anonymous_template_directory' => 'components/',
51-
'profiler_dump_components' => false,
51+
'profiler' => [
52+
'collect_components' => false,
53+
],
5254
]);
5355
$this->compileContainer($container);
5456

@@ -71,21 +73,6 @@ public function testDataCollectorWithDebugModeCanBeDisabled()
7173
$this->assertFalse($container->hasDefinition('ux.twig_component.data_collector'));
7274
}
7375

74-
public function testDataCollectorWithoutDebugMode()
75-
{
76-
$container = $this->createContainer();
77-
$container->setParameter('kernel.debug', false);
78-
$container->registerExtension(new TwigComponentExtension());
79-
$container->loadFromExtension('twig_component', [
80-
'defaults' => [],
81-
'anonymous_template_directory' => 'components/',
82-
'profiler' => true,
83-
]);
84-
$this->compileContainer($container);
85-
86-
$this->assertFalse($container->hasDefinition('ux.twig_component.data_collector'));
87-
}
88-
8976
/**
9077
* @group legacy
9178
*/

0 commit comments

Comments
 (0)