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 Sulu\Component\DocumentManager\Event\HydrateEvent; |
16
|
|
|
use Sulu\Component\DocumentManager\Events; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Lazily hydrate query results. |
21
|
|
|
* |
22
|
|
|
* TODO: Performance -- try fetch depth like in teh PHPCR-ODM ChildrenCollection |
23
|
|
|
*/ |
24
|
|
|
class ChildrenCollection extends AbstractLazyCollection |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var EventDispatcherInterface |
28
|
|
|
*/ |
29
|
|
|
private $dispatcher; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var NodeInterface |
33
|
|
|
*/ |
34
|
|
|
private $parentNode; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $locale; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $options; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
private $initialized = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param NodeInterface $parentNode |
53
|
|
|
* @param EventDispatcherInterface $dispatcher |
54
|
|
|
* @param string $locale |
55
|
|
|
* @param array $options |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
NodeInterface $parentNode, |
59
|
|
|
EventDispatcherInterface $dispatcher, |
60
|
|
|
$locale, |
61
|
|
|
$options = [] |
62
|
|
|
) { |
63
|
|
|
$this->parentNode = $parentNode; |
64
|
|
|
$this->dispatcher = $dispatcher; |
65
|
|
|
$this->locale = $locale; |
66
|
|
|
$this->options = $options; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function current() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$this->initialize(); |
75
|
|
|
$childNode = $this->documents->current(); |
76
|
|
|
|
77
|
|
|
$hydrateEvent = new HydrateEvent($childNode, $this->locale, $this->options); |
78
|
|
|
$this->dispatcher->dispatch(Events::HYDRATE, $hydrateEvent); |
79
|
|
|
|
80
|
|
|
return $hydrateEvent->getDocument(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
protected function initialize() |
87
|
|
|
{ |
88
|
|
|
if (true === $this->initialized) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->documents = $this->parentNode->getNodes(); |
93
|
|
|
$this->initialized = true; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.