|
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->getMetadata(); |
|
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. |
|
75
|
|
|
*/ |
|
76
|
|
|
if ($identifier !== $metadata->getIdentifier()) { |
|
77
|
|
|
$metadata->commit(); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Loops on the registered persistence services, and tries to fetch the |
|
83
|
|
|
* form. If a form is found, it is returned and the loop breaks. If not form |
|
84
|
|
|
* is found, `null` is returned. |
|
85
|
|
|
* |
|
86
|
|
|
* @param FormMetadata $metadata |
|
87
|
|
|
* @return FormInterface|null |
|
88
|
|
|
* @throws InvalidEntryException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function fetchFirst(FormMetadata $metadata) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->initializePersistence(); |
|
93
|
|
|
|
|
94
|
|
|
foreach ($this->getSortedPersistenceServices() as $persistence) { |
|
95
|
|
|
if ($persistence->has($metadata)) { |
|
96
|
|
|
$form = $persistence->fetch($metadata); |
|
97
|
|
|
|
|
98
|
|
|
if (false === $form instanceof FormInterface) { |
|
99
|
|
|
throw InvalidEntryException::persistenceInvalidEntryFetched($persistence, $form); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->formIsPersistent = true; |
|
103
|
|
|
|
|
104
|
|
|
return $form; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Loops on persistence services and initializes them. |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function initializePersistence() |
|
115
|
|
|
{ |
|
116
|
|
|
if (false === $this->initializationDone) { |
|
117
|
|
|
$this->initializationDone = true; |
|
118
|
|
|
|
|
119
|
|
|
foreach ($this->formObject->getConfiguration()->getPersistence() as $persistence) { |
|
120
|
|
|
$persistence->initialize(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Sorts the persistence services, based on their priority property: the |
|
127
|
|
|
* ones with the highest priority will come first. |
|
128
|
|
|
* |
|
129
|
|
|
* @return PersistenceInterface[] |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getSortedPersistenceServices() |
|
132
|
|
|
{ |
|
133
|
|
|
$items = $this->formObject->getConfiguration()->getPersistence(); |
|
134
|
|
|
|
|
135
|
|
|
usort($items, function (PersistenceInterface $a, PersistenceInterface $b) { |
|
136
|
|
|
$priorityA = (int)$a->getPriority(); |
|
137
|
|
|
$priorityB = (int)$b->getPriority(); |
|
138
|
|
|
|
|
139
|
|
|
if ($priorityA === $priorityB) { |
|
140
|
|
|
return 0; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $priorityA < $priorityB ? 1 : -1; |
|
144
|
|
|
}); |
|
145
|
|
|
|
|
146
|
|
|
return $items; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return bool |
|
151
|
|
|
*/ |
|
152
|
|
|
public function formIsPersistent() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->formIsPersistent; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|