vendor/symfony/ux-live-component/src/EventListener/DataModelPropsSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\UX\LiveComponent\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  13. use Symfony\UX\LiveComponent\Util\ModelBindingParser;
  14. use Symfony\UX\TwigComponent\ComponentStack;
  15. use Symfony\UX\TwigComponent\Event\PreMountEvent;
  16. /**
  17.  * Parses the "data-model" key, which triggers extra props to be passed in.
  18.  *
  19.  * For example, data-model: "value:content" would cause a new "value" prop
  20.  * to be passed in set to the parent component's "content" prop.
  21.  *
  22.  * @author Ryan Weaver <ryan@symfonycasts.com>
  23.  *
  24.  * @internal
  25.  */
  26. final class DataModelPropsSubscriber implements EventSubscriberInterface
  27. {
  28.     private ModelBindingParser $modelBindingParser;
  29.     public function __construct(private ComponentStack $componentStack, private PropertyAccessorInterface $propertyAccessor)
  30.     {
  31.         $this->modelBindingParser = new ModelBindingParser();
  32.     }
  33.     public function onPreMount(PreMountEvent $event): void
  34.     {
  35.         $data $event->getData();
  36.         if (!\array_key_exists('data-model'$data) && !\array_key_exists('dataModel'$data)) {
  37.             return;
  38.         }
  39.         $dataModel $data['data-model'] ?? $data['dataModel'];
  40.         $bindings $this->modelBindingParser->parse($dataModel);
  41.         if (=== \count($bindings)) {
  42.             return;
  43.         }
  44.         // normalize dataModel to a data-model HTML attribute
  45.         unset($data['dataModel']);
  46.         $data['data-model'] = $dataModel;
  47.         // the parent is still listed as the "current" component at this point
  48.         $parentMountedComponent $this->componentStack->getCurrentComponent();
  49.         if (null === $parentMountedComponent) {
  50.             throw new \LogicException('You can only pass "data-model" when rendering a component when you\'re rendering inside of a parent component.');
  51.         }
  52.         foreach ($bindings as $binding) {
  53.             $childModel $binding['child'];
  54.             $parentModel $binding['parent'];
  55.             $data[$childModel] = $this->propertyAccessor->getValue($parentMountedComponent->getComponent(), $parentModel);
  56.         }
  57.         $event->setData($data);
  58.     }
  59.     public static function getSubscribedEvents(): array
  60.     {
  61.         return [
  62.             PreMountEvent::class => 'onPreMount',
  63.         ];
  64.     }
  65. }