|
| 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; |
| 13 | + |
| 14 | +use Symfony\Component\Cache\Adapter\AdapterInterface; |
| 15 | +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
| 16 | +use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Simon André <[email protected]> |
| 20 | + * |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +final class ComponentProperties |
| 24 | +{ |
| 25 | + private const CACHE_KEY = 'ux.twig_component.component_properties'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var array<class-string, array{ |
| 29 | + * properties: array<class-string, array{string, array{string, string, bool}, bool}>, |
| 30 | + * methods: array<class-string, array{string, array{string, bool}}>, |
| 31 | + * }|null> |
| 32 | + */ |
| 33 | + private array $classMetadata; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + private readonly PropertyAccessorInterface $propertyAccessor, |
| 37 | + ?array $classMetadata = [], |
| 38 | + private readonly ?AdapterInterface $cache = null, |
| 39 | + ) { |
| 40 | + $cacheItem = $this->cache?->getItem(self::CACHE_KEY); |
| 41 | + |
| 42 | + $this->classMetadata = $cacheItem?->isHit() ? [...$cacheItem->get(), ...$classMetadata] : $classMetadata; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return array<string, mixed> |
| 47 | + */ |
| 48 | + public function getProperties(object $component, bool $publicProps = false): array |
| 49 | + { |
| 50 | + return iterator_to_array($this->extractProperties($component, $publicProps)); |
| 51 | + } |
| 52 | + |
| 53 | + public function warmup(): void |
| 54 | + { |
| 55 | + if (!$this->cache) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + foreach ($this->classMetadata as $class => $metadata) { |
| 60 | + if (null === $metadata) { |
| 61 | + $this->classMetadata[$class] = $this->loadClassMetadata($class); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + $this->cache->save($this->cache->getItem(self::CACHE_KEY)->set($this->classMetadata)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return \Generator<string, mixed> |
| 70 | + */ |
| 71 | + private function extractProperties(object $component, bool $publicProps): \Generator |
| 72 | + { |
| 73 | + yield from $publicProps ? get_object_vars($component) : []; |
| 74 | + |
| 75 | + $metadata = $this->classMetadata[$component::class] ??= $this->loadClassMetadata($component::class); |
| 76 | + |
| 77 | + foreach ($metadata['properties'] as $propertyName => $property) { |
| 78 | + $value = $property['getter'] ? $component->{$property['getter']}() : $this->propertyAccessor->getValue($component, $propertyName); |
| 79 | + if ($property['destruct'] ?? false) { |
| 80 | + yield from $value; |
| 81 | + } else { |
| 82 | + yield $property['name'] => $value; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + foreach ($metadata['methods'] as $methodName => $method) { |
| 87 | + if ($method['destruct'] ?? false) { |
| 88 | + yield from $component->{$methodName}(); |
| 89 | + } else { |
| 90 | + yield $method['name'] => $component->{$methodName}(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param class-string $class |
| 97 | + * |
| 98 | + * @return array{ |
| 99 | + * properties: array<string, array{ |
| 100 | + * name?: string, |
| 101 | + * getter?: string, |
| 102 | + * destruct?: bool |
| 103 | + * }>, |
| 104 | + * methods: array<string, array{ |
| 105 | + * name?: string, |
| 106 | + * destruct?: bool |
| 107 | + * }>, |
| 108 | + * } |
| 109 | + */ |
| 110 | + private function loadClassMetadata(string $class): array |
| 111 | + { |
| 112 | + $refClass = new \ReflectionClass($class); |
| 113 | + |
| 114 | + $properties = []; |
| 115 | + foreach ($refClass->getProperties() as $property) { |
| 116 | + if (!$attributes = $property->getAttributes(ExposeInTemplate::class)) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + $attribute = $attributes[0]->newInstance(); |
| 120 | + $properties[$property->name] = [ |
| 121 | + 'name' => $attribute->name ?? $property->name, |
| 122 | + 'getter' => $attribute->getter ? rtrim($attribute->getter, '()') : null, |
| 123 | + ]; |
| 124 | + if ($attribute->destruct) { |
| 125 | + unset($properties[$property->name]['name']); |
| 126 | + $properties[$property->name]['destruct'] = true; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + $methods = []; |
| 131 | + foreach ($refClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
| 132 | + if (!$attributes = $method->getAttributes(ExposeInTemplate::class)) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + if ($method->getNumberOfRequiredParameters()) { |
| 136 | + throw new \LogicException(\sprintf('Cannot use "%s" on methods with required parameters (%s::%s).', ExposeInTemplate::class, $class, $method->name)); |
| 137 | + } |
| 138 | + $attribute = $attributes[0]->newInstance(); |
| 139 | + $name = $attribute->name ?? (str_starts_with($method->name, 'get') ? lcfirst(substr($method->name, 3)) : $method->name); |
| 140 | + $methods[$method->name] = $attribute->destruct ? ['destruct' => true] : ['name' => $name]; |
| 141 | + } |
| 142 | + |
| 143 | + return [ |
| 144 | + 'properties' => $properties, |
| 145 | + 'methods' => $methods, |
| 146 | + ]; |
| 147 | + } |
| 148 | +} |
0 commit comments