1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager\Collection; |
13
|
|
|
|
14
|
|
|
use PHPCR\NodeInterface; |
15
|
|
|
use PHPCR\PropertyInterface; |
16
|
|
|
use Sulu\Component\DocumentManager\Event\HydrateEvent; |
17
|
|
|
use Sulu\Component\DocumentManager\Events; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Lazily load documents referring to the given node. |
22
|
|
|
*/ |
23
|
|
|
class ReferrerCollection extends AbstractLazyCollection |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var EventDispatcherInterface |
27
|
|
|
*/ |
28
|
|
|
private $dispatcher; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var NodeInterface |
32
|
|
|
*/ |
33
|
|
|
private $node; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $locale; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $initialized = false; |
44
|
|
|
|
45
|
|
|
public function __construct(NodeInterface $node, EventDispatcherInterface $dispatcher, $locale) |
46
|
|
|
{ |
47
|
|
|
$this->node = $node; |
48
|
|
|
$this->dispatcher = $dispatcher; |
49
|
|
|
$this->locale = $locale; |
50
|
|
|
$this->documents = new \ArrayIterator(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function current() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->initialize(); |
59
|
|
|
$referrerNode = $this->documents->current(); |
60
|
|
|
|
61
|
|
|
$hydrateEvent = new HydrateEvent($referrerNode, $this->locale); |
62
|
|
|
$this->dispatcher->dispatch(Events::HYDRATE, $hydrateEvent); |
63
|
|
|
|
64
|
|
|
return $hydrateEvent->getDocument(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected function initialize() |
71
|
|
|
{ |
72
|
|
|
if (true === $this->initialized) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$references = $this->node->getReferences(); |
77
|
|
|
|
78
|
|
|
// TODO: Performance: calling getParent adds overhead when the collection is |
79
|
|
|
// initialized, but if we don't do this, we won't know how many items are in the |
80
|
|
|
// collection, as one node could have many referring properties. |
81
|
|
|
foreach ($references as $reference) { |
82
|
|
|
/* @var PropertyInterface $reference */ |
83
|
|
|
$referrerNode = $reference->getParent(); |
84
|
|
|
$this->documents[$referrerNode->getIdentifier()] = $referrerNode; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->initialized = true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.