|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\DependencyInjection\ServiceLocator; |
| 15 | +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Kevin Bond <kevinbond@gmail.com> |
| 19 | + * |
| 20 | + * @experimental |
| 21 | + */ |
| 22 | +final class ComponentFactory |
| 23 | +{ |
| 24 | + private $components; |
| 25 | + private $propertyAccessor; |
| 26 | + private $serviceIdMap; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param ServiceLocator|ComponentInterface[] $components |
| 30 | + */ |
| 31 | + public function __construct(ServiceLocator $components, PropertyAccessorInterface $propertyAccessor, array $serviceIdMap) |
| 32 | + { |
| 33 | + $this->components = $components; |
| 34 | + $this->propertyAccessor = $propertyAccessor; |
| 35 | + $this->serviceIdMap = $serviceIdMap; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates the component and "mounts" it with the passed data. |
| 40 | + */ |
| 41 | + public function create(string $name, array $data = []): ComponentInterface |
| 42 | + { |
| 43 | + $component = $this->getComponent($name); |
| 44 | + |
| 45 | + $this->mount($component, $data); |
| 46 | + |
| 47 | + // set data that wasn't set in mount on the component directly |
| 48 | + foreach ($data as $property => $value) { |
| 49 | + if (!$this->propertyAccessor->isWritable($component, $property)) { |
| 50 | + throw new \LogicException(\sprintf('Unable to write "%s" to component "%s". Make sure this is a writable property or create a mount() with a $%s argument.', $property, \get_class($component), $property)); |
| 51 | + } |
| 52 | + |
| 53 | + $this->propertyAccessor->setValue($component, $property, $value); |
| 54 | + } |
| 55 | + |
| 56 | + return $component; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the "unmounted" component. |
| 61 | + */ |
| 62 | + public function get(string $name): ComponentInterface |
| 63 | + { |
| 64 | + return $this->getComponent($name); |
| 65 | + } |
| 66 | + |
| 67 | + public function serviceIdFor(string $name): string |
| 68 | + { |
| 69 | + if (!isset($this->serviceIdMap[$name])) { |
| 70 | + throw new \InvalidArgumentException('Component not found.'); |
| 71 | + } |
| 72 | + |
| 73 | + return $this->serviceIdMap[$name]; |
| 74 | + } |
| 75 | + |
| 76 | + private function mount(ComponentInterface $component, array &$data): void |
| 77 | + { |
| 78 | + try { |
| 79 | + $method = (new \ReflectionClass($component))->getMethod('mount'); |
| 80 | + } catch (\ReflectionException $e) { |
| 81 | + // no hydrate method |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + $parameters = []; |
| 86 | + |
| 87 | + foreach ($method->getParameters() as $refParameter) { |
| 88 | + $name = $refParameter->getName(); |
| 89 | + |
| 90 | + if (\array_key_exists($name, $data)) { |
| 91 | + $parameters[] = $data[$name]; |
| 92 | + |
| 93 | + // remove the data element so it isn't used to set the property directly. |
| 94 | + unset($data[$name]); |
| 95 | + } elseif ($refParameter->isDefaultValueAvailable()) { |
| 96 | + $parameters[] = $refParameter->getDefaultValue(); |
| 97 | + } else { |
| 98 | + throw new \LogicException(\sprintf('%s::mount() has a required $%s parameter. Make sure this is passed or make give a default value.', \get_class($component), $refParameter->getName())); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + $component->mount(...$parameters); |
| 103 | + } |
| 104 | + |
| 105 | + private function getComponent(string $name): ComponentInterface |
| 106 | + { |
| 107 | + if (!$this->components->has($name)) { |
| 108 | + throw new \InvalidArgumentException(sprintf( |
| 109 | + 'Unknown component "%s". The registered components are: %s', |
| 110 | + $name, |
| 111 | + implode(', ', array_keys($this->serviceIdMap)) |
| 112 | + )); |
| 113 | + } |
| 114 | + |
| 115 | + return $this->components->get($name); |
| 116 | + } |
| 117 | +} |
0 commit comments