[TASK] Replace inject methods with @inject
[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 * @inject
41 */
42 protected $identityMap;
43
44 /**
45 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
46 * @deprecated since 6.1, will be removed two versions later, use the persistence manager instead
47 * @inject
48 */
49 protected $backend;
50
51 /**
52 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
53 * @deprecated since 6.1 will be removed two versions later, use the persistence manager instead
54 * @inject
55 */
56 protected $session;
57
58 /**
59 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
60 * @inject
61 */
62 protected $persistenceManager;
63
64 /**
65 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
66 */
67 protected $objectManager;
68
69 /**
70 * @var string
71 */
72 protected $objectType;
73
74 /**
75 * @var array
76 */
77 protected $defaultOrderings = array();
78
79 /**
80 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
81 */
82 protected $defaultQuerySettings = NULL;
83
84 /**
85 * Constructs a new Repository
86 *
87 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
88 * @deprecated since Extbase 6.0.0; will be removed in Extbase 6.2 - Use objectManager to instantiate repository objects instead of GeneralUtility::makeInstance
89 */
90 public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager = NULL) {
91 $nsSeparator = strpos($this->getRepositoryClassName(), '\\') !== FALSE ? '\\\\' : '_';
92 $this->objectType = preg_replace(array('/' . $nsSeparator . 'Repository' . $nsSeparator . '(?!.*' . $nsSeparator . 'Repository' . $nsSeparator . ')/', '/Repository$/'), array($nsSeparator . 'Model' . $nsSeparator, ''), $this->getRepositoryClassName());
93 if ($objectManager === NULL) {
94 // Legacy creation, in case the object manager is NOT injected
95 // If ObjectManager IS there, then all properties are automatically injected
96 // @deprecated since Extbase 6.0.0, will be removed in Extbase 6.2
97 \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
98
99 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
100 $this->identityMap = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\IdentityMap');
101 $this->persistenceManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager');
102 $this->backend = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\BackendInterface');
103 $this->session = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Session');
104 } else {
105 $this->objectManager = $objectManager;
106 }
107 }
108
109 /**
110 * Adds an object to this repository
111 *
112 * @param object $object The object to add
113 * @throws Exception\IllegalObjectTypeException
114 * @return void
115 * @api
116 */
117 public function add($object) {
118 if (!$object instanceof $this->objectType) {
119 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
120 }
121 $this->persistenceManager->add($object);
122 }
123
124 /**
125 * Removes an object from this repository.
126 *
127 * @param object $object The object to remove
128 * @throws Exception\IllegalObjectTypeException
129 * @return void
130 * @api
131 */
132 public function remove($object) {
133 if (!$object instanceof $this->objectType) {
134 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
135 }
136 $this->persistenceManager->remove($object);
137 }
138
139 /**
140 * Replaces an object by another.
141 *
142 * @param object $existingObject The existing object
143 * @param object $newObject The new object
144 * @deprecated since 6.1, will be removed two versions later
145 */
146 public function replace($existingObject, $newObject) {
147 // Does nothing here as explicit update replaces objects in persistence session already
148 }
149
150 /**
151 * Replaces an existing object with the same identifier by the given object
152 *
153 * @param object $modifiedObject The modified object
154 * @throws Exception\UnknownObjectException
155 * @throws Exception\IllegalObjectTypeException
156 * @return void
157 * @api
158 */
159 public function update($modifiedObject) {
160 if (!$modifiedObject instanceof $this->objectType) {
161 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);
162 }
163 $this->persistenceManager->update($modifiedObject);
164 }
165
166 /**
167 * Returns all objects of this repository.
168 *
169 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array
170 * @api
171 */
172 public function findAll() {
173 return $this->createQuery()->execute();
174 }
175
176 /**
177 * Returns the total number objects of this repository.
178 *
179 * @return integer The object count
180 * @api
181 */
182 public function countAll() {
183 return $this->createQuery()->execute()->count();
184 }
185
186 /**
187 * Removes all objects of this repository as if remove() was called for
188 * all of them.
189 *
190 * @return void
191 * @api
192 */
193 public function removeAll() {
194 foreach ($this->findAll() AS $object) {
195 $this->remove($object);
196 }
197 }
198
199 /**
200 * Finds an object matching the given identifier.
201 *
202 * @param integer $uid The identifier of the object to find
203 * @return object The matching object if found, otherwise NULL
204 * @api
205 */
206 public function findByUid($uid) {
207 return $this->findByIdentifier($uid);
208 }
209
210 /**
211 * Finds an object matching the given identifier.
212 *
213 * @param mixed $identifier The identifier of the object to find
214 * @return object The matching object if found, otherwise NULL
215 * @api
216 */
217 public function findByIdentifier($identifier) {
218 /**
219 * @todo: This method must be changed again in 6.2 + 1
220 * This is marked @deprecated to be found in cleanup sessions.
221 *
222 * The repository should directly talk to the backend which
223 * does not respect query settings of the repository as
224 * findByIdentifier is strictly defined by finding an
225 * undeleted object by its identifier regardless if it
226 * is hidden/visible or a versioning/translation overlay.
227 *
228 * As a consequence users will be forced to overwrite this method
229 * and mimic this behaviour to be able to find objects by identifier
230 * respecting their query settings from 6.1 + 1 on.
231 */
232 if ($this->session->hasIdentifier($identifier, $this->objectType)) {
233 $object = $this->session->getObjectByIdentifier($identifier, $this->objectType);
234 } else {
235 $query = $this->createQuery();
236 $query->getQuerySettings()->setRespectStoragePage(FALSE);
237 $query->getQuerySettings()->setRespectSysLanguage(FALSE);
238 $object = $query->matching($query->equals('uid', $identifier))->execute()->getFirst();
239 }
240
241 return $object;
242 }
243
244 /**
245 * Sets the property names to order the result by per default.
246 * Expected like this:
247 * array(
248 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
249 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
250 * )
251 *
252 * @param array $defaultOrderings The property names to order by
253 * @return void
254 * @api
255 */
256 public function setDefaultOrderings(array $defaultOrderings) {
257 $this->defaultOrderings = $defaultOrderings;
258 }
259
260 /**
261 * Sets the default query settings to be used in this repository
262 *
263 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
264 * @return void
265 * @api
266 */
267 public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings) {
268 $this->defaultQuerySettings = $defaultQuerySettings;
269 }
270
271 /**
272 * Returns a query for objects of this repository
273 *
274 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
275 * @api
276 */
277 public function createQuery() {
278 $query = $this->persistenceManager->createQueryForType($this->objectType);
279 if ($this->defaultOrderings !== array()) {
280 $query->setOrderings($this->defaultOrderings);
281 }
282 if ($this->defaultQuerySettings !== NULL) {
283 $query->setQuerySettings(clone $this->defaultQuerySettings);
284 }
285 return $query;
286 }
287
288 /**
289 * Dispatches magic methods (findBy[Property]())
290 *
291 * @param string $methodName The name of the magic method
292 * @param string $arguments The arguments of the magic method
293 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
294 * @return mixed
295 * @api
296 */
297 public function __call($methodName, $arguments) {
298 if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
299 $propertyName = lcfirst(substr($methodName, 6));
300 $query = $this->createQuery();
301 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
302 return $result;
303 } elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
304 $propertyName = lcfirst(substr($methodName, 9));
305 $query = $this->createQuery();
306
307 $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
308 if ($result instanceof \TYPO3\CMS\Extbase\Persistence\QueryResultInterface) {
309 return $result->getFirst();
310 } elseif (is_array($result)) {
311 return isset($result[0]) ? $result[0] : NULL;
312 }
313
314 } elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
315 $propertyName = lcfirst(substr($methodName, 7));
316 $query = $this->createQuery();
317 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
318 return $result;
319 }
320 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
321 }
322
323 /**
324 * Returns the class name of this class.
325 *
326 * @return string Class name of the repository.
327 */
328 protected function getRepositoryClassName() {
329 return get_class($this);
330 }
331 }
332
333 ?>