aeddf9526e12ed58deb3e1a9c3669768612dc165
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Repository.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * The base repository - will usually be extended by a more concrete repository.
32 *
33 * @api
34 */
35 class Repository implements \TYPO3\CMS\Extbase\Persistence\RepositoryInterface, \TYPO3\CMS\Core\SingletonInterface {
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Persistence\Generic\IdentityMap
39 * @deprecated since 6.1 will be removed two versions later, use the persistence session instead
40 */
41 protected $identityMap;
42
43 /**
44 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
45 * @deprecated since 6.1, will be removed two versions later, use the persistence manager instead
46 */
47 protected $backend;
48
49 /**
50 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
51 * @deprecated since 6.1 will be removed two versions later, use the persistence manager instead
52 */
53 protected $session;
54
55 /**
56 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
57 */
58 protected $persistenceManager;
59
60 /**
61 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
62 */
63 protected $objectManager;
64
65 /**
66 * @var string
67 */
68 protected $objectType;
69
70 /**
71 * @var array
72 */
73 protected $defaultOrderings = array();
74
75 /**
76 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
77 */
78 protected $defaultQuerySettings = NULL;
79
80 /**
81 * Constructs a new Repository
82 *
83 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
84 * @deprecated since Extbase 6.0.0; will be removed in Extbase 6.2 - Use objectManager to instantiate repository objects instead of GeneralUtility::makeInstance
85 */
86 public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager = NULL) {
87 $nsSeparator = strpos($this->getRepositoryClassName(), '\\') !== FALSE ? '\\\\' : '_';
88 $this->objectType = preg_replace(array('/' . $nsSeparator . 'Repository' . $nsSeparator . '(?!.*' . $nsSeparator . 'Repository' . $nsSeparator . ')/', '/Repository$/'), array($nsSeparator . 'Model' . $nsSeparator, ''), $this->getRepositoryClassName());
89 if ($objectManager === NULL) {
90 // Legacy creation, in case the object manager is NOT injected
91 // If ObjectManager IS there, then all properties are automatically injected
92 // @deprecated since Extbase 6.0.0, will be removed in Extbase 6.2
93 \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
94
95 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
96 $this->injectIdentityMap($this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\IdentityMap'));
97 $this->injectPersistenceManager($this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager'));
98 $this->injectBackend($this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\BackendInterface'));
99 $this->injectSession($this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Session'));
100 } else {
101 $this->objectManager = $objectManager;
102 }
103 }
104
105 /**
106 * @param \TYPO3\CMS\Extbase\Persistence\Generic\IdentityMap $identityMap
107 * @return void
108 * @deprecated since 6.1, will be removed two versions later
109 */
110 public function injectIdentityMap(\TYPO3\CMS\Extbase\Persistence\Generic\IdentityMap $identityMap) {
111 $this->identityMap = $identityMap;
112 }
113
114 /**
115 * Injects the Persistence Backend
116 *
117 * @param \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend The persistence backend
118 * @return void
119 * @deprecated since 6.1, will be removed two versions later
120 */
121 public function injectBackend(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface $backend) {
122 $this->backend = $backend;
123 }
124
125 /**
126 * Injects the Persistence Session
127 *
128 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Session $session The persistence session
129 * @return void
130 * @deprecated since 6.1, will be removed two versions later
131 */
132 public function injectSession(\TYPO3\CMS\Extbase\Persistence\Generic\Session $session) {
133 $this->session = $session;
134 }
135
136 /**
137 * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
138 * @return void
139 */
140 public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager) {
141 $this->persistenceManager = $persistenceManager;
142 }
143
144 /**
145 * Adds an object to this repository
146 *
147 * @param object $object The object to add
148 * @throws Exception\IllegalObjectTypeException
149 * @return void
150 * @api
151 */
152 public function add($object) {
153 if (!$object instanceof $this->objectType) {
154 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
155 }
156 $this->persistenceManager->add($object);
157 }
158
159 /**
160 * Removes an object from this repository.
161 *
162 * @param object $object The object to remove
163 * @throws Exception\IllegalObjectTypeException
164 * @return void
165 * @api
166 */
167 public function remove($object) {
168 if (!$object instanceof $this->objectType) {
169 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
170 }
171 $this->persistenceManager->remove($object);
172 }
173
174 /**
175 * Replaces an object by another.
176 *
177 * @param object $existingObject The existing object
178 * @param object $newObject The new object
179 * @deprecated since 6.1, will be removed two versions later
180 */
181 public function replace($existingObject, $newObject) {
182 // Does nothing here as explicit update replaces objects in persistence session already
183 }
184
185 /**
186 * Replaces an existing object with the same identifier by the given object
187 *
188 * @param object $modifiedObject The modified object
189 * @throws Exception\UnknownObjectException
190 * @throws Exception\IllegalObjectTypeException
191 * @return void
192 * @api
193 */
194 public function update($modifiedObject) {
195 if (!$modifiedObject instanceof $this->objectType) {
196 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The modified object given to update() was not of the type (' . $this->objectType . ') this repository manages.', 1249479625);
197 }
198 $this->persistenceManager->update($modifiedObject);
199 }
200
201 /**
202 * Returns all objects of this repository.
203 *
204 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array
205 * @api
206 */
207 public function findAll() {
208 return $this->createQuery()->execute();
209 }
210
211 /**
212 * Returns the total number objects of this repository.
213 *
214 * @return integer The object count
215 * @api
216 */
217 public function countAll() {
218 return $this->createQuery()->execute()->count();
219 }
220
221 /**
222 * Removes all objects of this repository as if remove() was called for
223 * all of them.
224 *
225 * @return void
226 * @api
227 */
228 public function removeAll() {
229 foreach ($this->findAll() AS $object) {
230 $this->remove($object);
231 }
232 }
233
234 /**
235 * Finds an object matching the given identifier.
236 *
237 * @param integer $uid The identifier of the object to find
238 * @return object The matching object if found, otherwise NULL
239 * @api
240 */
241 public function findByUid($uid) {
242 return $this->persistenceManager->getObjectByIdentifier($uid, $this->objectType);
243 }
244
245 /**
246 * Finds an object matching the given identifier.
247 *
248 * @param mixed $identifier The identifier of the object to find
249 * @return object The matching object if found, otherwise NULL
250 * @api
251 */
252 public function findByIdentifier($identifier) {
253 return $this->persistenceManager->getObjectByIdentifier($identifier, $this->objectType);
254 }
255
256 /**
257 * Sets the property names to order the result by per default.
258 * Expected like this:
259 * array(
260 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
261 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
262 * )
263 *
264 * @param array $defaultOrderings The property names to order by
265 * @return void
266 * @api
267 */
268 public function setDefaultOrderings(array $defaultOrderings) {
269 $this->defaultOrderings = $defaultOrderings;
270 }
271
272 /**
273 * Sets the default query settings to be used in this repository
274 *
275 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
276 * @return void
277 * @api
278 */
279 public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings) {
280 $this->defaultQuerySettings = $defaultQuerySettings;
281 }
282
283 /**
284 * Returns a query for objects of this repository
285 *
286 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
287 * @api
288 */
289 public function createQuery() {
290 $query = $this->persistenceManager->createQueryForType($this->objectType);
291 if ($this->defaultOrderings !== array()) {
292 $query->setOrderings($this->defaultOrderings);
293 }
294 if ($this->defaultQuerySettings !== NULL) {
295 $query->setQuerySettings(clone $this->defaultQuerySettings);
296 }
297 return $query;
298 }
299
300 /**
301 * Dispatches magic methods (findBy[Property]())
302 *
303 * @param string $methodName The name of the magic method
304 * @param string $arguments The arguments of the magic method
305 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
306 * @return mixed
307 * @api
308 */
309 public function __call($methodName, $arguments) {
310 if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
311 $propertyName = lcfirst(substr($methodName, 6));
312 $query = $this->createQuery();
313 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
314 return $result;
315 } elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
316 $propertyName = lcfirst(substr($methodName, 9));
317 $query = $this->createQuery();
318
319 $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
320 if ($result instanceof \TYPO3\CMS\Extbase\Persistence\QueryResultInterface) {
321 return $result->getFirst();
322 } elseif (is_array($result)) {
323 return isset($result[0]) ? $result[0] : NULL;
324 }
325
326 } elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
327 $propertyName = lcfirst(substr($methodName, 7));
328 $query = $this->createQuery();
329 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
330 return $result;
331 }
332 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
333 }
334
335 /**
336 * Returns the class name of this class.
337 *
338 * @return string Class name of the repository.
339 */
340 protected function getRepositoryClassName() {
341 return get_class($this);
342 }
343 }
344
345 ?>