forked from neos/flow-development-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Repository.php
233 lines (213 loc) · 7.17 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
namespace TYPO3\Flow\Persistence;
/*
* This file is part of the TYPO3.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Persistence\Exception\IllegalObjectTypeException;
/**
* The Flow default Repository
*
* @api
*/
abstract class Repository implements RepositoryInterface
{
/**
* @Flow\Inject
* @var PersistenceManagerInterface
*/
protected $persistenceManager;
/**
* Warning: if you think you want to set this,
* look at RepositoryInterface::ENTITY_CLASSNAME first!
*
* @var string
*/
protected $entityClassName;
/**
* @var array
*/
protected $defaultOrderings = [];
/**
* Initializes a new Repository.
*/
public function __construct()
{
if (defined('static::ENTITY_CLASSNAME') === false) {
$this->entityClassName = preg_replace(['/\\\Repository\\\/', '/Repository$/'], ['\\Model\\', ''], get_class($this));
} else {
$this->entityClassName = static::ENTITY_CLASSNAME;
}
}
/**
* Returns the classname of the entities this repository is managing.
*
* Note that anything that is an "instanceof" this class is accepted
* by the repository.
*
* @return string
* @api
*/
public function getEntityClassName()
{
return $this->entityClassName;
}
/**
* Adds an object to this repository.
*
* @param object $object The object to add
* @return void
* @throws IllegalObjectTypeException
* @api
*/
public function add($object)
{
if (!is_object($object) || !($object instanceof $this->entityClassName)) {
$type = (is_object($object) ? get_class($object) : gettype($object));
throw new IllegalObjectTypeException('The value given to add() was ' . $type . ' , however the ' . get_class($this) . ' can only store ' . $this->entityClassName . ' instances.', 1298403438);
}
$this->persistenceManager->add($object);
}
/**
* Removes an object from this repository.
*
* @param object $object The object to remove
* @return void
* @throws IllegalObjectTypeException
* @api
*/
public function remove($object)
{
if (!is_object($object) || !($object instanceof $this->entityClassName)) {
$type = (is_object($object) ? get_class($object) : gettype($object));
throw new IllegalObjectTypeException('The value given to remove() was ' . $type . ' , however the ' . get_class($this) . ' can only handle ' . $this->entityClassName . ' instances.', 1298403442);
}
$this->persistenceManager->remove($object);
}
/**
* Returns all objects of this repository
*
* @return QueryResultInterface The query result
* @api
* @see QueryInterface::execute()
*/
public function findAll()
{
return $this->createQuery()->execute();
}
/**
* Finds an object matching the given identifier.
*
* @param string $identifier The identifier of the object to find
* @return object The matching object if found, otherwise NULL
* @api
*/
public function findByIdentifier($identifier)
{
return $this->persistenceManager->getObjectByIdentifier($identifier, $this->entityClassName);
}
/**
* Returns a query for objects of this repository
*
* @return QueryInterface
* @api
*/
public function createQuery()
{
$query = $this->persistenceManager->createQueryForType($this->entityClassName);
if ($this->defaultOrderings !== []) {
$query->setOrderings($this->defaultOrderings);
}
return $query;
}
/**
* Counts all objects of this repository
*
* @return integer
* @api
*/
public function countAll()
{
return $this->createQuery()->count();
}
/**
* Removes all objects of this repository as if remove() was called for
* all of them.
*
* @return void
* @api
* @todo use DQL here, would be much more performant
*/
public function removeAll()
{
foreach ($this->findAll() as $object) {
$this->remove($object);
}
}
/**
* Sets the property names to order results by. Expected like this:
* array(
* 'foo' => \TYPO3\Flow\Persistence\QueryInterface::ORDER_ASCENDING,
* 'bar' => \TYPO3\Flow\Persistence\QueryInterface::ORDER_DESCENDING
* )
*
* @param array $defaultOrderings The property names to order by by default
* @return void
* @api
*/
public function setDefaultOrderings(array $defaultOrderings)
{
$this->defaultOrderings = $defaultOrderings;
}
/**
* Schedules a modified object for persistence.
*
* @param object $object The modified object
* @throws IllegalObjectTypeException
* @api
*/
public function update($object)
{
if (!is_object($object) || !($object instanceof $this->entityClassName)) {
$type = (is_object($object) ? get_class($object) : gettype($object));
throw new IllegalObjectTypeException('The value given to update() was ' . $type . ' , however the ' . get_class($this) . ' can only store ' . $this->entityClassName . ' instances.', 1249479625);
}
$this->persistenceManager->update($object);
}
/**
* Magic call method for repository methods.
*
* Provides three methods
* - findBy<PropertyName>($value, $caseSensitive = TRUE, $cacheResult = FALSE)
* - findOneBy<PropertyName>($value, $caseSensitive = TRUE, $cacheResult = FALSE)
* - countBy<PropertyName>($value, $caseSensitive = TRUE)
*
* @param string $method Name of the method
* @param array $arguments The arguments
* @return mixed The result of the repository method
* @api
*/
public function __call($method, $arguments)
{
$query = $this->createQuery();
$caseSensitive = isset($arguments[1]) ? (boolean)$arguments[1] : true;
$cacheResult = isset($arguments[2]) ? (boolean)$arguments[2] : false;
if (isset($method[10]) && strpos($method, 'findOneBy') === 0) {
$propertyName = lcfirst(substr($method, 9));
return $query->matching($query->equals($propertyName, $arguments[0], $caseSensitive))->execute($cacheResult)->getFirst();
} elseif (isset($method[8]) && strpos($method, 'countBy') === 0) {
$propertyName = lcfirst(substr($method, 7));
return $query->matching($query->equals($propertyName, $arguments[0], $caseSensitive))->count();
} elseif (isset($method[7]) && strpos($method, 'findBy') === 0) {
$propertyName = lcfirst(substr($method, 6));
return $query->matching($query->equals($propertyName, $arguments[0], $caseSensitive))->execute($cacheResult);
}
trigger_error('Call to undefined method ' . get_class($this) . '::' . $method, E_USER_ERROR);
}
}