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\Subscriber\Core; |
13
|
|
|
|
14
|
|
|
use PHPCR\NodeInterface; |
15
|
|
|
use Sulu\Component\DocumentManager\Event\CreateEvent; |
16
|
|
|
use Sulu\Component\DocumentManager\Event\HydrateEvent; |
17
|
|
|
use Sulu\Component\DocumentManager\Events; |
18
|
|
|
use Sulu\Component\DocumentManager\Metadata; |
19
|
|
|
use Sulu\Component\DocumentManager\MetadataFactoryInterface; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Responsible for instantiating documents from PHPCR nodes and |
24
|
|
|
* setting the document in the event so that other listeners can |
25
|
|
|
* take further actions (such as hydrating it for example). |
26
|
|
|
* |
27
|
|
|
* NOTE: This should always be the first thing to be called |
28
|
|
|
*/ |
29
|
|
|
class InstantiatorSubscriber implements EventSubscriberInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var MetadataFactoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $metadataFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param MetadataFactoryInterface $metadataFactory |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
MetadataFactoryInterface $metadataFactory |
41
|
|
|
) { |
42
|
|
|
$this->metadataFactory = $metadataFactory; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public static function getSubscribedEvents() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
Events::HYDRATE => ['handleHydrate', 500], |
52
|
|
|
Events::CREATE => ['handleCreate', 500], |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param HydrateEvent $event |
58
|
|
|
*/ |
59
|
|
|
public function handleHydrate(HydrateEvent $event) |
60
|
|
|
{ |
61
|
|
|
// don't need to instantiate the document if it is already existing. |
62
|
|
|
if ($event->hasDocument()) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$node = $event->getNode(); |
67
|
|
|
|
68
|
|
|
$document = $this->getDocumentFromNode($node); |
69
|
|
|
$event->setDocument($document); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param mixed $event |
74
|
|
|
*/ |
75
|
|
|
public function handleCreate(CreateEvent $event) |
76
|
|
|
{ |
77
|
|
|
$metadata = $this->metadataFactory->getMetadataForAlias($event->getAlias()); |
78
|
|
|
$document = $this->instantiateFromMetadata($metadata); |
79
|
|
|
$event->setDocument($document); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Instantiate a new document. The class is determined from |
84
|
|
|
* the mixins present in the PHPCR node for legacy reasons. |
85
|
|
|
* |
86
|
|
|
* @param NodeInterface $node |
87
|
|
|
* |
88
|
|
|
* @return object |
89
|
|
|
*/ |
90
|
|
|
private function getDocumentFromNode(NodeInterface $node) |
91
|
|
|
{ |
92
|
|
|
$metadata = $this->metadataFactory->getMetadataForPhpcrNode($node); |
93
|
|
|
|
94
|
|
|
return $this->instantiateFromMetadata($metadata); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param Metadata $metadata |
99
|
|
|
* |
100
|
|
|
* @return object |
101
|
|
|
*/ |
102
|
|
|
private function instantiateFromMetadata(Metadata $metadata) |
103
|
|
|
{ |
104
|
|
|
$class = $metadata->getClass(); |
105
|
|
|
|
106
|
|
|
if (!class_exists($class)) { |
107
|
|
|
throw new \RuntimeException(sprintf( |
108
|
|
|
'Document class "%s" does not exist', $class |
109
|
|
|
)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$document = new $class(); |
113
|
|
|
|
114
|
|
|
return $document; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|