Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Twig] Cache template class resolution #2364

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,7 @@ public function getTest(LiveComponentMetadataFactory $metadataFactory): Hydratio
new ComponentMetadata([
'key' => '__testing',
'mount' => $reflectionClass->hasMethod('mount') ? ['mount'] : [],
'service_id' => '__testing_id',
]),
$metadataFactory->createPropMetadatas($reflectionClass),
),
Expand Down
55 changes: 24 additions & 31 deletions src/TwigComponent/src/ComponentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@
*/
final class ComponentFactory implements ResetInterface
{
private static array $mountMethods = [];
private array $mountMethods = [];

/**
* @param array<string, array> $config
* @param array<class-string, string> $classMap
* @param array<class-string, array<string, string[]> $classMounts
*/
public function __construct(
private ComponentTemplateFinderInterface $componentTemplateFinder,
private ServiceLocator $components,
private PropertyAccessorInterface $propertyAccessor,
private EventDispatcherInterface $eventDispatcher,
private array $config,
private array $classMap,
private readonly array $classMap,
) {
}

Expand Down Expand Up @@ -88,29 +87,29 @@ public function create(string $name, array $data = []): MountedComponent
public function mountFromObject(object $component, array $data, ComponentMetadata $componentMetadata): MountedComponent
{
$originalData = $data;
$data = $this->preMount($component, $data, $componentMetadata);
$event = $this->preMount($component, $data, $componentMetadata);
$data = $event->getData();

$this->mount($component, $data, $componentMetadata);

// set data that wasn't set in mount on the component directly
foreach ($data as $property => $value) {
if ($this->propertyAccessor->isWritable($component, $property)) {
$this->propertyAccessor->setValue($component, $property, $value);

unset($data[$property]);
if (!$componentMetadata->isAnonymous()) {
// set data that wasn't set in mount on the component directly
foreach ($data as $property => $value) {
if ($this->propertyAccessor->isWritable($component, $property)) {
$this->propertyAccessor->setValue($component, $property, $value);
unset($data[$property]);
}
}
}

$postMount = $this->postMount($component, $data, $componentMetadata);
$data = $postMount['data'];
$extraMetadata = $postMount['extraMetadata'];
$data = $postMount->getData();

// create attributes from "attributes" key if exists
$attributesVar = $componentMetadata->getAttributesVar();
$attributes = $data[$attributesVar] ?? [];
unset($data[$attributesVar]);

// ensure remaining data is scalar
foreach ($data as $key => $value) {
if ($value instanceof \Stringable) {
$data[$key] = (string) $value;
Expand All @@ -120,9 +119,9 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
return new MountedComponent(
$componentMetadata->getName(),
$component,
new ComponentAttributes(array_merge($attributes, $data)),
new ComponentAttributes([...$attributes, ...$data]),
$originalData,
$extraMetadata,
$postMount->getExtraMetadata(),
);
}

Expand Down Expand Up @@ -154,7 +153,7 @@ private function mount(object $component, array &$data, ComponentMetadata $compo
return;
}

$mount = self::$mountMethods[$component::class] ??= (new \ReflectionClass($component))->getMethod('mount');
$mount = $this->mountMethods[$component::class] ??= (new \ReflectionClass($component))->getMethod('mount');

$parameters = [];
foreach ($mount->getParameters() as $refParameter) {
Expand All @@ -172,40 +171,34 @@ private function mount(object $component, array &$data, ComponentMetadata $compo
$mount->invoke($component, ...$parameters);
}

private function preMount(object $component, array $data, ComponentMetadata $componentMetadata): array
private function preMount(object $component, array $data, ComponentMetadata $componentMetadata): PreMountEvent
{
$event = new PreMountEvent($component, $data, $componentMetadata);
$this->eventDispatcher->dispatch($event);
$data = $event->getData();

$data = $event->getData();
foreach ($componentMetadata->getPreMounts() as $preMount) {
if (null !== $newData = $component->$preMount($data)) {
$data = $newData;
$event->setData($data = $newData);
}
}

return $data;
return $event;
}

/**
* @return array{data: array<string, mixed>, extraMetadata: array<string, mixed>}
*/
private function postMount(object $component, array $data, ComponentMetadata $componentMetadata): array
private function postMount(object $component, array $data, ComponentMetadata $componentMetadata): PostMountEvent
{
$event = new PostMountEvent($component, $data, $componentMetadata);
$this->eventDispatcher->dispatch($event);
$data = $event->getData();

$data = $event->getData();
foreach ($componentMetadata->getPostMounts() as $postMount) {
if (null !== $newData = $component->$postMount($data)) {
$data = $newData;
$event->setData($data = $newData);
}
}

return [
'data' => $data,
'extraMetadata' => $event->getExtraMetadata(),
];
return $event;
}

/**
Expand Down Expand Up @@ -244,6 +237,6 @@ private function throwUnknownComponentException(string $name): void

public function reset(): void
{
self::$mountMethods = [];
$this->mountMethods = [];
}
}
18 changes: 13 additions & 5 deletions src/TwigComponent/src/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\TwigComponent;

use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Service\ResetInterface;
use Symfony\UX\TwigComponent\Event\PostRenderEvent;
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
Expand All @@ -22,8 +23,10 @@
*
* @internal
*/
final class ComponentRenderer implements ComponentRendererInterface
final class ComponentRenderer implements ComponentRendererInterface, ResetInterface
{
private array $templateClasses = [];
smnandre marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(
private Environment $twig,
private EventDispatcherInterface $dispatcher,
Expand Down Expand Up @@ -62,15 +65,15 @@ public function render(MountedComponent $mounted): string
$variables = $event->getVariables();
// see ComponentNode. When rendering an individual embedded component,
// *not* through its parent, we need to set the parent template.
if ($event->getTemplateIndex()) {
if ($templateIndex = $event->getTemplateIndex()) {
$variables['__parent__'] = $event->getParentTemplateForEmbedded();
}

try {
return $this->twig->loadTemplate(
$this->twig->getTemplateClass($event->getTemplate()),
$event->getTemplate(),
$event->getTemplateIndex(),
$this->templateClasses[$template = $event->getTemplate()] ??= $this->twig->getTemplateClass($template),
$template,
$templateIndex,
)->render($variables);
} finally {
$mounted = $this->componentStack->pop();
Expand Down Expand Up @@ -137,4 +140,9 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR

return $event;
}

public function reset(): void
{
$this->templateClasses = [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static function (ChildDefinition $definition, AsTwigComponent $attribute) {
new Reference('ux.twig_component.component_properties'),
new Reference('ux.twig_component.component_stack'),
])
->addTag('kernel.reset', ['method' => 'reset'])
;

$container->register('ux.twig_component.twig.component_extension', ComponentExtension::class)
Expand Down