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

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