vendor/sulu/sulu/src/Sulu/Component/DocumentManager/DocumentManager.php line 35

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;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\OptionsResolver\OptionsResolver;
  13. use Symfony\Contracts\Service\ResetInterface;
  14. class DocumentManager implements DocumentManagerInterfaceResetInterface
  15. {
  16.     /**
  17.      * @var EventDispatcherInterface
  18.      */
  19.     private $eventDispatcher;
  20.     /**
  21.      * @var array Cached options resolver instances
  22.      */
  23.     private $optionsResolvers = [];
  24.     public function __construct(EventDispatcherInterface $eventDispatcher)
  25.     {
  26.         $this->eventDispatcher $eventDispatcher;
  27.     }
  28.     public function find($identifier$locale null, array $options = [])
  29.     {
  30.         $options $this->getOptionsResolver(Events::FIND)->resolve($options);
  31.         $event = new Event\FindEvent($identifier$locale$options);
  32.         $this->eventDispatcher->dispatch($eventEvents::FIND);
  33.         return $event->getDocument();
  34.     }
  35.     public function create($alias)
  36.     {
  37.         $event = new Event\CreateEvent($alias);
  38.         $this->eventDispatcher->dispatch($eventEvents::CREATE);
  39.         return $event->getDocument();
  40.     }
  41.     public function persist($document$locale null, array $options = [])
  42.     {
  43.         $options $this->getOptionsResolver(Events::PERSIST)->resolve($options);
  44.         $event = new Event\PersistEvent($document$locale$options);
  45.         $this->eventDispatcher->dispatch($eventEvents::PERSIST);
  46.     }
  47.     public function remove($document/*, array $options = []*/)
  48.     {
  49.         $options \func_num_args() >= ? (array) \func_get_arg(1) : [];
  50.         $options $this->getOptionsResolver(Events::REMOVE)->resolve($options);
  51.         $event = new Event\RemoveEvent($document$options);
  52.         $this->eventDispatcher->dispatch($eventEvents::REMOVE);
  53.     }
  54.     public function removeLocale($document$locale)
  55.     {
  56.         $event = new Event\RemoveLocaleEvent($document$locale);
  57.         $this->eventDispatcher->dispatch($eventEvents::REMOVE_LOCALE);
  58.     }
  59.     public function move($document$destId)
  60.     {
  61.         $event = new Event\MoveEvent($document$destId);
  62.         $this->eventDispatcher->dispatch($eventEvents::MOVE);
  63.     }
  64.     public function copy($document$destPath)
  65.     {
  66.         $event = new Event\CopyEvent($document$destPath);
  67.         $this->eventDispatcher->dispatch($eventEvents::COPY);
  68.         return $event->getCopiedPath();
  69.     }
  70.     public function copyLocale($document$srcLocale$destLocale)
  71.     {
  72.         $event = new Event\CopyLocaleEvent($document$srcLocale$destLocale);
  73.         $this->eventDispatcher->dispatch($eventEvents::COPY_LOCALE);
  74.     }
  75.     public function reorder($document$destId)
  76.     {
  77.         $event = new Event\ReorderEvent($document$destId);
  78.         $this->eventDispatcher->dispatch($eventEvents::REORDER);
  79.     }
  80.     public function publish($document$locale null, array $options = [])
  81.     {
  82.         $options $this->getOptionsResolver(Events::PUBLISH)->resolve($options);
  83.         $event = new Event\PublishEvent($document$locale$options);
  84.         $this->eventDispatcher->dispatch($eventEvents::PUBLISH);
  85.     }
  86.     public function unpublish($document$locale)
  87.     {
  88.         $event = new Event\UnpublishEvent($document$locale);
  89.         $this->eventDispatcher->dispatch($eventEvents::UNPUBLISH);
  90.     }
  91.     public function removeDraft($document$locale)
  92.     {
  93.         $event = new Event\RemoveDraftEvent($document$locale);
  94.         $this->eventDispatcher->dispatch($eventEvents::REMOVE_DRAFT);
  95.     }
  96.     public function restore($document$locale$version, array $options = [])
  97.     {
  98.         $options $this->getOptionsResolver(Events::RESTORE)->resolve($options);
  99.         $event = new Event\RestoreEvent($document$locale$version$options);
  100.         $this->eventDispatcher->dispatch($eventEvents::RESTORE);
  101.     }
  102.     public function refresh($document)
  103.     {
  104.         $event = new Event\RefreshEvent($document);
  105.         $this->eventDispatcher->dispatch($eventEvents::REFRESH);
  106.     }
  107.     public function flush()
  108.     {
  109.         $event = new Event\FlushEvent();
  110.         $this->eventDispatcher->dispatch($eventEvents::FLUSH);
  111.     }
  112.     public function clear()
  113.     {
  114.         $event = new Event\ClearEvent();
  115.         $this->eventDispatcher->dispatch($eventEvents::CLEAR);
  116.     }
  117.     /**
  118.      * @return void
  119.      */
  120.     public function reset()
  121.     {
  122.         $this->clear();
  123.     }
  124.     public function createQuery($query$locale null, array $options = [])
  125.     {
  126.         $event = new Event\QueryCreateEvent($query$locale$options);
  127.         $this->eventDispatcher->dispatch($eventEvents::QUERY_CREATE);
  128.         return $event->getQuery();
  129.     }
  130.     private function getOptionsResolver($eventName)
  131.     {
  132.         if (isset($this->optionsResolvers[$eventName])) {
  133.             return $this->optionsResolvers[$eventName];
  134.         }
  135.         $resolver = new OptionsResolver();
  136.         $resolver->setDefault('locale'null);
  137.         $event = new Event\ConfigureOptionsEvent($resolver);
  138.         $this->eventDispatcher->dispatch($eventEvents::CONFIGURE_OPTIONS);
  139.         $this->optionsResolvers[$eventName] = $resolver;
  140.         return $resolver;
  141.     }
  142. }