vendor/sulu/sulu/src/Sulu/Component/DocumentManager/Subscriber/Core/InstantiatorSubscriber.php line 86

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\DocumentManager\Subscriber\Core;
  11. use PHPCR\NodeInterface;
  12. use Sulu\Component\DocumentManager\Event\CreateEvent;
  13. use Sulu\Component\DocumentManager\Event\HydrateEvent;
  14. use Sulu\Component\DocumentManager\Events;
  15. use Sulu\Component\DocumentManager\Metadata;
  16. use Sulu\Component\DocumentManager\MetadataFactoryInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. /**
  19.  * Responsible for instantiating documents from PHPCR nodes and
  20.  * setting the document in the event so that other listeners can
  21.  * take further actions (such as hydrating it for example).
  22.  *
  23.  * NOTE: This should always be the first thing to be called
  24.  */
  25. class InstantiatorSubscriber implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var MetadataFactoryInterface
  29.      */
  30.     private $metadataFactory;
  31.     public function __construct(
  32.         MetadataFactoryInterface $metadataFactory
  33.     ) {
  34.         $this->metadataFactory $metadataFactory;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             Events::HYDRATE => ['handleHydrate'500],
  40.             Events::CREATE => ['handleCreate'500],
  41.         ];
  42.     }
  43.     public function handleHydrate(HydrateEvent $event)
  44.     {
  45.         // don't need to instantiate the document if it is already existing.
  46.         if ($event->hasDocument()) {
  47.             return;
  48.         }
  49.         $node $event->getNode();
  50.         $document $this->getDocumentFromNode($node);
  51.         $event->setDocument($document);
  52.     }
  53.     public function handleCreate(CreateEvent $event)
  54.     {
  55.         $metadata $this->metadataFactory->getMetadataForAlias($event->getAlias());
  56.         $document $this->instantiateFromMetadata($metadata);
  57.         $event->setDocument($document);
  58.     }
  59.     /**
  60.      * Instantiate a new document. The class is determined from
  61.      * the mixins present in the PHPCR node for legacy reasons.
  62.      *
  63.      * @return object
  64.      */
  65.     private function getDocumentFromNode(NodeInterface $node)
  66.     {
  67.         $metadata $this->metadataFactory->getMetadataForPhpcrNode($node);
  68.         return $this->instantiateFromMetadata($metadata);
  69.     }
  70.     /**
  71.      * @return object
  72.      */
  73.     private function instantiateFromMetadata(Metadata $metadata)
  74.     {
  75.         $class $metadata->getClass();
  76.         if (!\class_exists($class)) {
  77.             throw new \RuntimeException(\sprintf(
  78.                 'Document class "%s" does not exist'$class
  79.             ));
  80.         }
  81.         $document = new $class();
  82.         return $document;
  83.     }
  84. }