vendor/sulu/sulu/src/Sulu/Component/Content/Document/Subscriber/ExtensionSubscriber.php line 143

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\Content\Document\Subscriber;
  11. use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector;
  12. use Sulu\Bundle\DocumentManagerBundle\Bridge\PropertyEncoder;
  13. use Sulu\Component\Content\Document\Behavior\ExtensionBehavior;
  14. use Sulu\Component\Content\Document\Extension\ManagedExtensionContainer;
  15. use Sulu\Component\Content\Extension\ExtensionManagerInterface;
  16. use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
  17. use Sulu\Component\DocumentManager\Events;
  18. use Sulu\Component\DocumentManager\NamespaceRegistry;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class ExtensionSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var ExtensionManagerInterface
  24.      */
  25.     private $extensionManager;
  26.     /**
  27.      * @var DocumentInspector
  28.      */
  29.     private $inspector;
  30.     /**
  31.      * @var NamespaceRegistry
  32.      */
  33.     private $namespaceRegistry;
  34.     /**
  35.      * @var PropertyEncoder
  36.      */
  37.     private $encoder;
  38.     /**
  39.      * TODO: Remove this: Use a dedicated namespace instead.
  40.      *
  41.      * @var string
  42.      */
  43.     private $internalPrefix '';
  44.     public function __construct(
  45.         PropertyEncoder $encoder,
  46.         ExtensionManagerInterface $extensionManager,
  47.         DocumentInspector $inspector,
  48.         // these two dependencies should absolutely not be necessary
  49.         NamespaceRegistry $namespaceRegistry
  50.     ) {
  51.         $this->encoder $encoder;
  52.         $this->extensionManager $extensionManager;
  53.         $this->inspector $inspector;
  54.         $this->namespaceRegistry $namespaceRegistry;
  55.     }
  56.     public static function getSubscribedEvents()
  57.     {
  58.         return [
  59.             // persist should happen before content is mapped
  60.             Events::PERSIST => ['saveExtensionData'10],
  61.             Events::PUBLISH => ['saveExtensionData'10],
  62.             // hydrate should happen afterwards
  63.             Events::HYDRATE => ['handleHydrate', -10],
  64.         ];
  65.     }
  66.     public function handleHydrate(AbstractMappingEvent $event)
  67.     {
  68.         if (!$event->getDocument() instanceof ExtensionBehavior) {
  69.             return;
  70.         }
  71.         $this->hydrate($event);
  72.     }
  73.     public function saveExtensionData(AbstractMappingEvent $event)
  74.     {
  75.         $locale $event->getLocale();
  76.         if (!$locale) {
  77.             return;
  78.         }
  79.         $document $event->getDocument();
  80.         if (!$document instanceof ExtensionBehavior) {
  81.             return;
  82.         }
  83.         $structureType $document->getStructureType();
  84.         $node $event->getNode();
  85.         $extensionsData $document->getExtensionsData();
  86.         $webspaceName $this->inspector->getWebspace($document);
  87.         $prefix $this->namespaceRegistry->getPrefix('extension_localized');
  88.         $extensions $this->extensionManager->getExtensions($structureType);
  89.         foreach ($extensions as $extension) {
  90.             $extensionData null;
  91.             if (!isset($extensionsData[$extension->getName()])) {
  92.                 continue;
  93.             }
  94.             $extensionData $extensionsData[$extension->getName()];
  95.             $extension->setLanguageCode($locale$prefix$this->internalPrefix);
  96.             $extension->save(
  97.                 $node,
  98.                 $extensionData,
  99.                 $webspaceName,
  100.                 $locale
  101.             );
  102.         }
  103.         $this->hydrate($event);
  104.     }
  105.     private function hydrate(AbstractMappingEvent $event)
  106.     {
  107.         $document $event->getDocument();
  108.         $node $event->getNode();
  109.         $locale $this->inspector->getLocale($document);
  110.         $webspaceName $this->inspector->getWebspace($document);
  111.         $structureType $document->getStructureType();
  112.         if (null === $structureType) {
  113.             return;
  114.         }
  115.         $prefix $this->namespaceRegistry->getPrefix('extension_localized');
  116.         $extensionContainer = new ManagedExtensionContainer(
  117.             $structureType,
  118.             $this->extensionManager,
  119.             $node,
  120.             $locale,
  121.             $prefix,
  122.             $this->internalPrefix,
  123.             $webspaceName
  124.         );
  125.         $document->setExtensionsData($extensionContainer);
  126.     }
  127. }