a3232b319ae9f42e1b2834e9e901a153369f33be
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Repository.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\ClassNamingUtility;
18
19 /**
20 * The base repository - will usually be extended by a more concrete repository.
21 *
22 * @api
23 */
24 class Repository implements RepositoryInterface, \TYPO3\CMS\Core\SingletonInterface {
25
26 /**
27 * @var \TYPO3\CMS\Extbase\Persistence\Generic\IdentityMap
28 * @deprecated since 6.1 will be removed two versions later, use the persistence session instead
29 * @inject
30 */
31 protected $identityMap;
32
33 /**
34 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Session
35 * @deprecated since 6.1 will be removed two versions later, use the persistence manager instead
36 * @inject
37 */
38 protected $session;
39
40 /**
41 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
42 * @inject
43 */
44 protected $persistenceManager;
45
46 /**
47 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
48 */
49 protected $objectManager;
50
51 /**
52 * @var string
53 */
54 protected $objectType;
55
56 /**
57 * @var array
58 */
59 protected $defaultOrderings = array();
60
61 /**
62 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
63 */
64 protected $defaultQuerySettings = NULL;
65
66 /**
67 * Constructs a new Repository
68 *
69 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
70 */
71 public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
72 $this->objectManager = $objectManager;
73 $this->objectType = ClassNamingUtility::translateRepositoryNameToModelName($this->getRepositoryClassName());
74 }
75
76 /**
77 * Adds an object to this repository
78 *
79 * @param object $object The object to add
80 * @throws Exception\IllegalObjectTypeException
81 * @return void
82 * @api
83 */
84 public function add($object) {
85 if (!$object instanceof $this->objectType) {
86 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
87 }
88 $this->persistenceManager->add($object);
89 }
90
91 /**
92 * Removes an object from this repository.
93 *
94 * @param object $object The object to remove
95 * @throws Exception\IllegalObjectTypeException
96 * @return void
97 * @api
98 */
99 public function remove($object) {
100 if (!$object instanceof $this->objectType) {
101 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363336);
102 }
103 $this->persistenceManager->remove($object);
104 }
105
106 /**
107 * Replaces an object by another.
108 *
109 * @param object $existingObject The existing object
110 * @param object $newObject The new object
111 * @deprecated since 6.1, will be removed two versions later
112 */
113 public function replace($existingObject, $newObject) {
114 // Does nothing here as explicit update replaces objects in persistence session already
115 }
116
117 /**
118 * Replaces an existing object with the same identifier by the given object
119 *
120 * @param object $modifiedObject The modified object
121 * @throws Exception\UnknownObjectException
122 * @throws Exception\IllegalObjectTypeException
123 * @return void
124 * @api
125 */
126 public function update($modifiedObject) {
127 if (!$modifiedObject instanceof $this->objectType) {
128 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);
129 }
130 $this->persistenceManager->update($modifiedObject);
131 }
132
133 /**
134 * Returns all objects of this repository.
135 *
136 * @return QueryResultInterface|array
137 * @api
138 */
139 public function findAll() {
140 return $this->createQuery()->execute();
141 }
142
143 /**
144 * Returns the total number objects of this repository.
145 *
146 * @return int The object count
147 * @api
148 */
149 public function countAll() {
150 return $this->createQuery()->execute()->count();
151 }
152
153 /**
154 * Removes all objects of this repository as if remove() was called for
155 * all of them.
156 *
157 * @return void
158 * @api
159 */
160 public function removeAll() {
161 foreach ($this->findAll() AS $object) {
162 $this->remove($object);
163 }
164 }
165
166 /**
167 * Finds an object matching the given identifier.
168 *
169 * @param int $uid The identifier of the object to find
170 * @return object The matching object if found, otherwise NULL
171 * @api
172 */
173 public function findByUid($uid) {
174 return $this->findByIdentifier($uid);
175 }
176
177 /**
178 * Finds an object matching the given identifier.
179 *
180 * @param mixed $identifier The identifier of the object to find
181 * @return object The matching object if found, otherwise NULL
182 * @api
183 */
184 public function findByIdentifier($identifier) {
185 /**
186 * @todo: This method must be changed again in 6.2 + 1
187 * This is marked @deprecated to be found in cleanup sessions.
188 *
189 * The repository should directly talk to the backend which
190 * does not respect query settings of the repository as
191 * findByIdentifier is strictly defined by finding an
192 * undeleted object by its identifier regardless if it
193 * is hidden/visible or a versioning/translation overlay.
194 *
195 * As a consequence users will be forced to overwrite this method
196 * and mimic this behaviour to be able to find objects by identifier
197 * respecting their query settings from 6.1 + 1 on.
198 */
199 if ($this->session->hasIdentifier($identifier, $this->objectType)) {
200 $object = $this->session->getObjectByIdentifier($identifier, $this->objectType);
201 } else {
202 $query = $this->createQuery();
203 $query->getQuerySettings()->setRespectStoragePage(FALSE);
204 $query->getQuerySettings()->setRespectSysLanguage(FALSE);
205 $object = $query->matching($query->equals('uid', $identifier))->execute()->getFirst();
206 }
207
208 return $object;
209 }
210
211 /**
212 * Sets the property names to order the result by per default.
213 * Expected like this:
214 * array(
215 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
216 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
217 * )
218 *
219 * @param array $defaultOrderings The property names to order by
220 * @return void
221 * @api
222 */
223 public function setDefaultOrderings(array $defaultOrderings) {
224 $this->defaultOrderings = $defaultOrderings;
225 }
226
227 /**
228 * Sets the default query settings to be used in this repository
229 *
230 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
231 * @return void
232 * @api
233 */
234 public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings) {
235 $this->defaultQuerySettings = $defaultQuerySettings;
236 }
237
238 /**
239 * Returns a query for objects of this repository
240 *
241 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
242 * @api
243 */
244 public function createQuery() {
245 $query = $this->persistenceManager->createQueryForType($this->objectType);
246 if ($this->defaultOrderings !== array()) {
247 $query->setOrderings($this->defaultOrderings);
248 }
249 if ($this->defaultQuerySettings !== NULL) {
250 $query->setQuerySettings(clone $this->defaultQuerySettings);
251 }
252 return $query;
253 }
254
255 /**
256 * Dispatches magic methods (findBy[Property]())
257 *
258 * @param string $methodName The name of the magic method
259 * @param string $arguments The arguments of the magic method
260 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
261 * @return mixed
262 * @api
263 */
264 public function __call($methodName, $arguments) {
265 if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
266 $propertyName = lcfirst(substr($methodName, 6));
267 $query = $this->createQuery();
268 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
269 return $result;
270 } elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
271 $propertyName = lcfirst(substr($methodName, 9));
272 $query = $this->createQuery();
273
274 $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
275 if ($result instanceof QueryResultInterface) {
276 return $result->getFirst();
277 } elseif (is_array($result)) {
278 return isset($result[0]) ? $result[0] : NULL;
279 }
280
281 } elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
282 $propertyName = lcfirst(substr($methodName, 7));
283 $query = $this->createQuery();
284 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
285 return $result;
286 }
287 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
288 }
289
290 /**
291 * Returns the class name of this class.
292 *
293 * @return string Class name of the repository.
294 */
295 protected function getRepositoryClassName() {
296 return get_class($this);
297 }
298
299 }