-
-
Notifications
You must be signed in to change notification settings - Fork 381
[TwigComponent] Cache component properties metadata #2211
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent\DependencyInjection\Loader\Configurator; | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $container): void { | ||
$container->services() | ||
->set('cache.ux.twig_component') | ||
->parent('cache.system') | ||
->private() | ||
->tag('cache.pool') | ||
; | ||
}; |
55 changes: 55 additions & 0 deletions
55
src/TwigComponent/src/CacheWarmer/TwigComponentCacheWarmer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent\CacheWarmer; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
use Symfony\Contracts\Service\ServiceSubscriberInterface; | ||
use Symfony\UX\TwigComponent\ComponentProperties; | ||
|
||
/** | ||
* Warm the TwigComponent metadata caches. | ||
* | ||
* @author Simon André <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class TwigComponentCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface | ||
{ | ||
/** | ||
* As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected. | ||
*/ | ||
public function __construct( | ||
private readonly ContainerInterface $container, | ||
) { | ||
} | ||
|
||
public static function getSubscribedServices(): array | ||
{ | ||
return [ | ||
'ux.twig_component.component_properties' => ComponentProperties::class, | ||
]; | ||
} | ||
|
||
public function warmUp(string $cacheDir, ?string $buildDir = null): array | ||
{ | ||
$properties = $this->container->get('ux.twig_component.component_properties'); | ||
$properties->warmup(); | ||
|
||
return []; | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent; | ||
|
||
use Symfony\Component\Cache\Adapter\AdapterInterface; | ||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; | ||
|
||
/** | ||
* @author Simon André <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class ComponentProperties | ||
{ | ||
private const CACHE_KEY = 'ux.twig_component.component_properties'; | ||
|
||
/** | ||
* @var array<class-string, array{ | ||
* properties: array<class-string, array{string, array{string, string, bool}, bool}>, | ||
* methods: array<class-string, array{string, array{string, bool}}>, | ||
* }|null> | ||
*/ | ||
private array $classMetadata; | ||
|
||
public function __construct( | ||
private readonly PropertyAccessorInterface $propertyAccessor, | ||
?array $classMetadata = [], | ||
smnandre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private readonly ?AdapterInterface $cache = null, | ||
) { | ||
$cacheItem = $this->cache?->getItem(self::CACHE_KEY); | ||
|
||
$this->classMetadata = $cacheItem?->isHit() ? [...$cacheItem->get(), ...$classMetadata] : $classMetadata; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function getProperties(object $component, bool $publicProps = false): array | ||
{ | ||
return iterator_to_array($this->extractProperties($component, $publicProps)); | ||
} | ||
|
||
public function warmup(): void | ||
{ | ||
if (!$this->cache) { | ||
return; | ||
} | ||
|
||
foreach ($this->classMetadata as $class => $metadata) { | ||
if (null === $metadata) { | ||
$this->classMetadata[$class] = $this->loadClassMetadata($class); | ||
} | ||
} | ||
|
||
$this->cache->save($this->cache->getItem(self::CACHE_KEY)->set($this->classMetadata)); | ||
} | ||
|
||
/** | ||
* @return \Generator<string, mixed> | ||
*/ | ||
private function extractProperties(object $component, bool $publicProps): \Generator | ||
{ | ||
yield from $publicProps ? get_object_vars($component) : []; | ||
|
||
$metadata = $this->classMetadata[$component::class] ??= $this->loadClassMetadata($component::class); | ||
|
||
foreach ($metadata['properties'] as $propertyName => $property) { | ||
$value = $property['getter'] ? $component->{$property['getter']}() : $this->propertyAccessor->getValue($component, $propertyName); | ||
if ($property['destruct'] ?? false) { | ||
yield from $value; | ||
} else { | ||
yield $property['name'] => $value; | ||
} | ||
} | ||
|
||
foreach ($metadata['methods'] as $methodName => $method) { | ||
if ($method['destruct'] ?? false) { | ||
yield from $component->{$methodName}(); | ||
} else { | ||
yield $method['name'] => $component->{$methodName}(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param class-string $class | ||
* | ||
* @return array{ | ||
* properties: array<string, array{ | ||
* name?: string, | ||
* getter?: string, | ||
* destruct?: bool | ||
* }>, | ||
* methods: array<string, array{ | ||
* name?: string, | ||
* destruct?: bool | ||
* }>, | ||
* } | ||
*/ | ||
private function loadClassMetadata(string $class): array | ||
{ | ||
$refClass = new \ReflectionClass($class); | ||
|
||
$properties = []; | ||
foreach ($refClass->getProperties() as $property) { | ||
if (!$attributes = $property->getAttributes(ExposeInTemplate::class)) { | ||
continue; | ||
} | ||
$attribute = $attributes[0]->newInstance(); | ||
$properties[$property->name] = [ | ||
'name' => $attribute->name ?? $property->name, | ||
'getter' => $attribute->getter ? rtrim($attribute->getter, '()') : null, | ||
]; | ||
if ($attribute->destruct) { | ||
unset($properties[$property->name]['name']); | ||
$properties[$property->name]['destruct'] = true; | ||
} | ||
} | ||
|
||
$methods = []; | ||
foreach ($refClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { | ||
if (!$attributes = $method->getAttributes(ExposeInTemplate::class)) { | ||
continue; | ||
} | ||
if ($method->getNumberOfRequiredParameters()) { | ||
throw new \LogicException(\sprintf('Cannot use "%s" on methods with required parameters (%s::%s).', ExposeInTemplate::class, $class, $method->name)); | ||
} | ||
$attribute = $attributes[0]->newInstance(); | ||
$name = $attribute->name ?? (str_starts_with($method->name, 'get') ? lcfirst(substr($method->name, 3)) : $method->name); | ||
$methods[$method->name] = $attribute->destruct ? ['destruct' => true] : ['name' => $name]; | ||
} | ||
|
||
return [ | ||
'properties' => $properties, | ||
'methods' => $methods, | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.