Skip to content

Commit cb8f1a1

Browse files
committed
[TwigComponent][Perf] Add option to disable the dump of components
1 parent 15e2163 commit cb8f1a1

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

src/TwigComponent/config/debug.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\UX\TwigComponent\DataCollector\TwigComponentDataCollector;
1616
use Symfony\UX\TwigComponent\EventListener\TwigComponentLoggerListener;
1717

18+
use function Symfony\Component\DependencyInjection\Loader\Configurator\abstract_arg;
1819
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
1920

2021
return static function (ContainerConfigurator $container) {
@@ -27,6 +28,7 @@
2728
->args([
2829
service('ux.twig_component.component_logger_listener'),
2930
service('twig'),
31+
abstract_arg('profiler dump components'),
3032
])
3133
->tag('data_collector', [
3234
'template' => '@TwigComponent/Collector/twig_component.html.twig',

src/TwigComponent/src/DataCollector/TwigComponentDataCollector.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +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,
3839
) {
3940
$this->hasStub = class_exists(ClassStub::class);
4041
}
@@ -130,12 +131,15 @@ private function collectDataFromLogger(): void
130131
'input_props' => $mountedComponent->getInputProps(),
131132
'attributes' => $mountedComponent->getAttributes()->all(),
132133
'template_index' => $event->getTemplateIndex(),
133-
'component' => $mountedComponent->getComponent(),
134134
'depth' => \count($ongoingRenders),
135135
'children' => [],
136136
'render_start' => $profile[0],
137137
];
138138

139+
if ($this->dumpComponents) {
140+
$renders[$renderId]['component'] = $mountedComponent->getComponent();
141+
}
142+
139143
if ($parentId = end($ongoingRenders)) {
140144
$renders[$parentId]['children'][] = $renderId;
141145
}

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ static function (ChildDefinition $definition, AsTwigComponent $attribute) {
152152

153153
if ($container->getParameter('kernel.debug') && $config['profiler']) {
154154
$loader->load('debug.php');
155+
$container->getDefinition('ux.twig_component.data_collector')
156+
->setArgument(2, $config['profiler_dump_components'] ?? true);
155157
}
156158

157159
$loader->load('cache.php');
@@ -219,6 +221,10 @@ public function getConfigTreeBuilder(): TreeBuilder
219221
->info('Enables the profiler for Twig Component (in debug mode)')
220222
->defaultValue('%kernel.debug%')
221223
->end()
224+
->booleanNode('profiler_dump_components')
225+
->info('Dump components in the Twig Component profiler panel')
226+
->defaultTrue()
227+
->end()
222228
->scalarNode('controllers_json')
223229
->setDeprecated('symfony/ux-twig-component', '2.18', 'The "twig_component.controllers_json" config option is deprecated, and will be removed in 3.0.')
224230
->defaultNull()

src/TwigComponent/templates/Collector/twig_component.html.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,12 @@
293293
<th scope="row">Attributes</th>
294294
<td colspan="4">{{ profiler_dump(render.attributes) }}</td>
295295
</tr>
296+
{% if render.component is defined %}
296297
<tr>
297298
<th scope="row">Component</th>
298299
<td colspan="4">{{ profiler_dump(render.component) }}</td>
299300
</tr>
301+
{% endif %}
300302
</tbody>
301303
</table>
302304
{% endfor %}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ public function testDataCollectorWithDebugMode()
3737
$this->compileContainer($container);
3838

3939
$this->assertTrue($container->hasDefinition('ux.twig_component.data_collector'));
40+
$this->assertTrue($container->getDefinition('ux.twig_component.data_collector')->getArgument(2));
41+
}
42+
43+
public function testDataCollectorWithDumpComponentsDisabled()
44+
{
45+
$container = $this->createContainer();
46+
$container->setParameter('kernel.debug', true);
47+
$container->registerExtension(new TwigComponentExtension());
48+
$container->loadFromExtension('twig_component', [
49+
'defaults' => [],
50+
'anonymous_template_directory' => 'components/',
51+
'profiler_dump_components' => false,
52+
]);
53+
$this->compileContainer($container);
54+
55+
$this->assertTrue($container->hasDefinition('ux.twig_component.data_collector'));
56+
$this->assertFalse($container->getDefinition('ux.twig_component.data_collector')->getArgument(2));
4057
}
4158

4259
public function testDataCollectorWithDebugModeCanBeDisabled()

0 commit comments

Comments
 (0)