[TASK] Make Repository::findByIdentifier() call the storage-backend
[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 return $this->persistenceManager->getObjectByIdentifier($identifier, $this->objectType);
186 }
187
188 /**
189 * Sets the property names to order the result by per default.
190 * Expected like this:
191 * array(
192 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
193 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
194 * )
195 *
196 * @param array $defaultOrderings The property names to order by
197 * @return void
198 * @api
199 */
200 public function setDefaultOrderings(array $defaultOrderings) {
201 $this->defaultOrderings = $defaultOrderings;
202 }
203
204 /**
205 * Sets the default query settings to be used in this repository
206 *
207 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
208 * @return void
209 * @api
210 */
211 public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings) {
212 $this->defaultQuerySettings = $defaultQuerySettings;
213 }
214
215 /**
216 * Returns a query for objects of this repository
217 *
218 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
219 * @api
220 */
221 public function createQuery() {
222 $query = $this->persistenceManager->createQueryForType($this->objectType);
223 if ($this->defaultOrderings !== array()) {
224 $query->setOrderings($this->defaultOrderings);
225 }
226 if ($this->defaultQuerySettings !== NULL) {
227 $query->setQuerySettings(clone $this->defaultQuerySettings);
228 }
229 return $query;
230 }
231
232 /**
233 * Dispatches magic methods (findBy[Property]())
234 *
235 * @param string $methodName The name of the magic method
236 * @param string $arguments The arguments of the magic method
237 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
238 * @return mixed
239 * @api
240 */
241 public function __call($methodName, $arguments) {
242 if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
243 $propertyName = lcfirst(substr($methodName, 6));
244 $query = $this->createQuery();
245 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
246 return $result;
247 } elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
248 $propertyName = lcfirst(substr($methodName, 9));
249 $query = $this->createQuery();
250
251 $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
252 if ($result instanceof QueryResultInterface) {
253 return $result->getFirst();
254 } elseif (is_array($result)) {
255 return isset($result[0]) ? $result[0] : NULL;
256 }
257
258 } elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
259 $propertyName = lcfirst(substr($methodName, 7));
260 $query = $this->createQuery();
261 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
262 return $result;
263 }
264 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
265 }
266
267 /**
268 * Returns the class name of this class.
269 *
270 * @return string Class name of the repository.
271 */
272 protected function getRepositoryClassName() {
273 return get_class($this);
274 }
275
276 }