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