Skip to content

Commit 2dfc72d

Browse files
committed
add PreRender event
1 parent 17f73c4 commit 2dfc72d

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

src/TwigComponent/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
- Add `HasAttributesTrait` to help with adding html attributes (ie class, style)
1212
to your component's root node.
1313

14+
- Add `PreRender` event.
15+
1416
## 2.0.0
1517

1618
- Support for `stimulus` version 2 was removed and support for `@hotwired/stimulus`

src/TwigComponent/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"php": ">=8.0",
3030
"twig/twig": "^2.0|^3.0",
3131
"symfony/property-access": "^4.4|^5.0|^6.0",
32-
"symfony/dependency-injection": "^4.4|^5.0|^6.0"
32+
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
33+
"symfony/event-dispatcher": "^4.4|^5.0|^6.0"
3334
},
3435
"require-dev": {
3536
"symfony/framework-bundle": "^4.4|^5.0|^6.0",

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\UX\TwigComponent;
1313

14+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
15+
use Symfony\UX\TwigComponent\EventListener\PreRenderEvent;
1416
use Twig\Environment;
1517

1618
/**
@@ -20,18 +22,21 @@
2022
*/
2123
final class ComponentRenderer
2224
{
23-
private Environment $twig;
24-
25-
public function __construct(Environment $twig)
25+
public function __construct(private Environment $twig, private EventDispatcherInterface $dispatcher)
2626
{
27-
$this->twig = $twig;
2827
}
2928

3029
public function render(object $component, array $config): string
3130
{
32-
return $this->twig->render($config['template'], array_merge(
33-
['this' => $component, '_component_config' => $config],
34-
get_object_vars($component)
35-
));
31+
$event = new PreRenderEvent(
32+
$component,
33+
$config['template'],
34+
array_merge(['this' => $component, '_component_config' => $config], get_object_vars($component)),
35+
$config
36+
);
37+
38+
$this->dispatcher->dispatch($event);
39+
40+
return $this->twig->render($event->template, $event->context);
3641
}
3742
}

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %
5757
$container->register('ux.twig_component.component_renderer', ComponentRenderer::class)
5858
->setArguments([
5959
new Reference('twig'),
60+
new Reference('event_dispatcher'),
6061
])
6162
;
6263

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\UX\TwigComponent\EventListener;
13+
14+
use Symfony\Contracts\EventDispatcher\Event;
15+
16+
/**
17+
* @author Kevin Bond <[email protected]>
18+
*/
19+
final class PreRenderEvent extends Event
20+
{
21+
public function __construct(
22+
public object $component,
23+
public string $template,
24+
public array $context,
25+
public array $config
26+
) {
27+
}
28+
}

0 commit comments

Comments
 (0)