1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Persistence; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Domain\Model\FormMetadata; |
17
|
|
|
use Romm\Formz\Exceptions\InvalidEntryException; |
18
|
|
|
use Romm\Formz\Form\FormInterface; |
19
|
|
|
use Romm\Formz\Form\FormObject\FormObject; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Manages persistence for a given form object. |
23
|
|
|
*/ |
24
|
|
|
class PersistenceManager |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var FormObject |
28
|
|
|
*/ |
29
|
|
|
protected $formObject; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $formIsPersistent = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $initializationDone = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param FormObject $formObject |
43
|
|
|
*/ |
44
|
|
|
public function __construct(FormObject $formObject) |
45
|
|
|
{ |
46
|
|
|
$this->formObject = $formObject; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Loops on the registered persistence services and saves the given form |
51
|
|
|
* instance in each one. |
52
|
|
|
* |
53
|
|
|
* @param FormInterface $form |
54
|
|
|
*/ |
55
|
|
|
public function save(FormInterface $form) |
56
|
|
|
{ |
57
|
|
|
$this->initializePersistence(); |
58
|
|
|
|
59
|
|
|
$metadata = $this->formObject->getFormMetadata(); |
60
|
|
|
$identifier = $metadata->getIdentifier(); |
61
|
|
|
$persistenceServices = $this->getSortedPersistenceServices(); |
62
|
|
|
|
63
|
|
|
foreach ($persistenceServices as $persistence) { |
64
|
|
|
$persistence->save($metadata, $form); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (count($persistenceServices) > 0) { |
68
|
|
|
$this->formIsPersistent = true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/* |
72
|
|
|
* If the form identifier has changed during the saving process (for |
73
|
|
|
* instance the form has been saved in database and has a new uid), the |
74
|
|
|
* metadata is persisted to be sure the form identifier is saved in |
75
|
|
|
* database. |
76
|
|
|
*/ |
77
|
|
|
if ($identifier !== $metadata->getIdentifier()) { |
78
|
|
|
$metadata->commit(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Loops on the registered persistence services, and tries to fetch the |
84
|
|
|
* form. If a form is found, it is returned and the loop breaks. If not form |
85
|
|
|
* is found, `null` is returned. |
86
|
|
|
* |
87
|
|
|
* @param FormMetadata $metadata |
88
|
|
|
* @return FormInterface|null |
89
|
|
|
* @throws InvalidEntryException |
90
|
|
|
*/ |
91
|
|
|
public function fetchFirst(FormMetadata $metadata) |
92
|
|
|
{ |
93
|
|
|
$this->initializePersistence(); |
94
|
|
|
|
95
|
|
|
foreach ($this->getSortedPersistenceServices() as $persistence) { |
96
|
|
|
if ($persistence->has($metadata)) { |
97
|
|
|
$form = $persistence->fetch($metadata); |
98
|
|
|
|
99
|
|
|
if (false === $form instanceof FormInterface) { |
100
|
|
|
throw InvalidEntryException::persistenceInvalidEntryFetched($persistence, $form); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->formIsPersistent = true; |
104
|
|
|
|
105
|
|
|
return $form; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Loops on persistence services and initializes them. |
114
|
|
|
*/ |
115
|
|
|
protected function initializePersistence() |
116
|
|
|
{ |
117
|
|
|
if (false === $this->initializationDone) { |
118
|
|
|
$this->initializationDone = true; |
119
|
|
|
|
120
|
|
|
foreach ($this->formObject->getDefinition()->getPersistence() as $persistence) { |
121
|
|
|
$persistence->initialize(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Sorts the persistence services, based on their priority property: the |
128
|
|
|
* ones with the highest priority will come first. |
129
|
|
|
* |
130
|
|
|
* @return PersistenceInterface[] |
131
|
|
|
*/ |
132
|
|
|
protected function getSortedPersistenceServices() |
133
|
|
|
{ |
134
|
|
|
$items = $this->formObject->getDefinition()->getPersistence(); |
135
|
|
|
|
136
|
|
|
usort($items, function (PersistenceInterface $a, PersistenceInterface $b) { |
137
|
|
|
$priorityA = (int)$a->getPriority(); |
138
|
|
|
$priorityB = (int)$b->getPriority(); |
139
|
|
|
|
140
|
|
|
if ($priorityA === $priorityB) { |
141
|
|
|
return 0; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $priorityA < $priorityB ? 1 : -1; |
145
|
|
|
}); |
146
|
|
|
|
147
|
|
|
return $items; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function formIsPersistent() |
154
|
|
|
{ |
155
|
|
|
return $this->formIsPersistent; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|