vendor/sulu/sulu/src/Sulu/Component/DocumentManager/ProxyFactory.php line 115

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 PHPCR\NodeInterface;
  12. use ProxyManager\Factory\LazyLoadingGhostFactory;
  13. use ProxyManager\Proxy\LazyLoadingInterface;
  14. use Sulu\Component\DocumentManager\Collection\ChildrenCollection;
  15. use Sulu\Component\DocumentManager\Collection\ReferrerCollection;
  16. use Sulu\Component\DocumentManager\Event\HydrateEvent;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. /**
  19. * Handle creation of proxies.
  20. */
  21. class ProxyFactory
  22. {
  23. /**
  24. * @var LazyLoadingGhostFactory
  25. */
  26. private $proxyFactory;
  27. /**
  28. * @var EventDispatcherInterface
  29. */
  30. private $dispatcher;
  31. /**
  32. * @var DocumentRegistry
  33. */
  34. private $registry;
  35. /**
  36. * @var MetadataFactoryInterface
  37. */
  38. private $metadataFactory;
  39. public function __construct(
  40. LazyLoadingGhostFactory $proxyFactory,
  41. EventDispatcherInterface $dispatcher,
  42. DocumentRegistry $registry,
  43. MetadataFactoryInterface $metadataFactory
  44. ) {
  45. $this->proxyFactory = $proxyFactory;
  46. $this->dispatcher = $dispatcher;
  47. $this->registry = $registry;
  48. $this->metadataFactory = $metadataFactory;
  49. }
  50. /**
  51. * Create a new proxy object from the given document for
  52. * the given target node.
  53. *
  54. * TODO: We only pass the document here in order to correctly evaluate its locale
  55. * later. I wonder if it necessary.
  56. *
  57. * @param object $fromDocument
  58. * @param array $options
  59. *
  60. * @return \ProxyManager\Proxy\GhostObjectInterface
  61. */
  62. public function createProxyForNode($fromDocument, NodeInterface $targetNode, $options = [])
  63. {
  64. // if node is already registered then just return the registered document
  65. $locale = $this->registry->getOriginalLocaleForDocument($fromDocument);
  66. if ($this->registry->hasNode($targetNode, $locale)) {
  67. $document = $this->registry->getDocumentForNode($targetNode, $locale);
  68. // If the parent is not loaded in the correct locale, reload it in the correct locale.
  69. if ($this->registry->getOriginalLocaleForDocument($document) !== $locale) {
  70. $hydrateEvent = new HydrateEvent($targetNode, $locale);
  71. $hydrateEvent->setDocument($document);
  72. $this->dispatcher->dispatch($hydrateEvent, Events::HYDRATE);
  73. }
  74. return $document;
  75. }
  76. $initializer = function(LazyLoadingInterface $document, $method, array $parameters, &$initializer) use (
  77. $targetNode,
  78. $options,
  79. $locale
  80. ) {
  81. $hydrateEvent = new HydrateEvent($targetNode, $locale, $options);
  82. $hydrateEvent->setDocument($document);
  83. $this->dispatcher->dispatch($hydrateEvent, Events::HYDRATE);
  84. $initializer = null;
  85. };
  86. $targetMetadata = $this->metadataFactory->getMetadataForPhpcrNode($targetNode);
  87. $proxy = $this->proxyFactory->createProxy($targetMetadata->getClass(), $initializer);
  88. $locale = $this->registry->getOriginalLocaleForDocument($fromDocument);
  89. $this->registry->registerDocument($proxy, $targetNode, $locale);
  90. return $proxy;
  91. }
  92. /**
  93. * Create a new children collection for the given document.
  94. *
  95. * @param object $document
  96. *
  97. * @return ChildrenCollection
  98. */
  99. public function createChildrenCollection($document, array $options = [])
  100. {
  101. $node = $this->registry->getNodeForDocument($document);
  102. $locale = $this->registry->getOriginalLocaleForDocument($document);
  103. return new ChildrenCollection(
  104. $node,
  105. $this->dispatcher,
  106. $locale,
  107. $options
  108. );
  109. }
  110. /**
  111. * Create a new collection of referrers from a list of referencing items.
  112. *
  113. * @param object $document
  114. *
  115. * @return ReferrerCollection
  116. */
  117. public function createReferrerCollection($document)
  118. {
  119. $node = $this->registry->getNodeForDocument($document);
  120. $locale = $this->registry->getOriginalLocaleForDocument($document);
  121. return new ReferrerCollection(
  122. $node,
  123. $this->dispatcher,
  124. $locale
  125. );
  126. }
  127. }