5ff4956fb2bd747b78819b7a053c44bd5a562f3e
[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\Session
28 * @deprecated since 6.1 will be removed two versions later, use the persistence manager instead
29 * @inject
30 */
31 protected $session;
32
33 /**
34 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
35 * @inject
36 */
37 protected $persistenceManager;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
41 */
42 protected $objectManager;
43
44 /**
45 * @var string
46 */
47 protected $objectType;
48
49 /**
50 * @var array
51 */
52 protected $defaultOrderings = array();
53
54 /**
55 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
56 */
57 protected $defaultQuerySettings = NULL;
58
59 /**
60 * Constructs a new Repository
61 *
62 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
63 */
64 public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
65 $this->objectManager = $objectManager;
66 $this->objectType = ClassNamingUtility::translateRepositoryNameToModelName($this->getRepositoryClassName());
67 }
68
69 /**
70 * Adds an object to this repository
71 *
72 * @param object $object The object to add
73 * @throws Exception\IllegalObjectTypeException
74 * @return void
75 * @api
76 */
77 public function add($object) {
78 if (!$object instanceof $this->objectType) {
79 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
80 }
81 $this->persistenceManager->add($object);
82 }
83
84 /**
85 * Removes an object from this repository.
86 *
87 * @param object $object The object to remove
88 * @throws Exception\IllegalObjectTypeException
89 * @return void
90 * @api
91 */
92 public function remove($object) {
93 if (!$object instanceof $this->objectType) {
94 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363336);
95 }
96 $this->persistenceManager->remove($object);
97 }
98
99 /**
100 * Replaces an object by another.
101 *
102 * @param object $existingObject The existing object
103 * @param object $newObject The new object
104 * @deprecated since 6.1, will be removed two versions later
105 */
106 public function replace($existingObject, $newObject) {
107 // Does nothing here as explicit update replaces objects in persistence session already
108 }
109
110 /**
111 * Replaces an existing object with the same identifier by the given object
112 *
113 * @param object $modifiedObject The modified object
114 * @throws Exception\UnknownObjectException
115 * @throws Exception\IllegalObjectTypeException
116 * @return void
117 * @api
118 */
119 public function update($modifiedObject) {
120 if (!$modifiedObject instanceof $this->objectType) {
121 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);
122 }
123 $this->persistenceManager->update($modifiedObject);
124 }
125
126 /**
127 * Returns all objects of this repository.
128 *
129 * @return QueryResultInterface|array
130 * @api
131 */
132 public function findAll() {
133 return $this->createQuery()->execute();
134 }
135
136 /**
137 * Returns the total number objects of this repository.
138 *
139 * @return int The object count
140 * @api
141 */
142 public function countAll() {
143 return $this->createQuery()->execute()->count();
144 }
145
146 /**
147 * Removes all objects of this repository as if remove() was called for
148 * all of them.
149 *
150 * @return void
151 * @api
152 */
153 public function removeAll() {
154 foreach ($this->findAll() AS $object) {
155 $this->remove($object);
156 }
157 }
158
159 /**
160 * Finds an object matching the given identifier.
161 *
162 * @param int $uid The identifier of the object to find
163 * @return object The matching object if found, otherwise NULL
164 * @api
165 */
166 public function findByUid($uid) {
167 return $this->findByIdentifier($uid);
168 }
169
170 /**
171 * Finds an object matching the given identifier.
172 *
173 * @param mixed $identifier The identifier of the object to find
174 * @return object The matching object if found, otherwise NULL
175 * @api
176 */
177 public function findByIdentifier($identifier) {
178 return $this->persistenceManager->getObjectByIdentifier($identifier, $this->objectType);
179 }
180
181 /**
182 * Sets the property names to order the result by per default.
183 * Expected like this:
184 * array(
185 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
186 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
187 * )
188 *
189 * @param array $defaultOrderings The property names to order by
190 * @return void
191 * @api
192 */
193 public function setDefaultOrderings(array $defaultOrderings) {
194 $this->defaultOrderings = $defaultOrderings;
195 }
196
197 /**
198 * Sets the default query settings to be used in this repository
199 *
200 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
201 * @return void
202 * @api
203 */
204 public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings) {
205 $this->defaultQuerySettings = $defaultQuerySettings;
206 }
207
208 /**
209 * Returns a query for objects of this repository
210 *
211 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
212 * @api
213 */
214 public function createQuery() {
215 $query = $this->persistenceManager->createQueryForType($this->objectType);
216 if ($this->defaultOrderings !== array()) {
217 $query->setOrderings($this->defaultOrderings);
218 }
219 if ($this->defaultQuerySettings !== NULL) {
220 $query->setQuerySettings(clone $this->defaultQuerySettings);
221 }
222 return $query;
223 }
224
225 /**
226 * Dispatches magic methods (findBy[Property]())
227 *
228 * @param string $methodName The name of the magic method
229 * @param string $arguments The arguments of the magic method
230 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
231 * @return mixed
232 * @api
233 */
234 public function __call($methodName, $arguments) {
235 if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
236 $propertyName = lcfirst(substr($methodName, 6));
237 $query = $this->createQuery();
238 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
239 return $result;
240 } elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
241 $propertyName = lcfirst(substr($methodName, 9));
242 $query = $this->createQuery();
243
244 $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
245 if ($result instanceof QueryResultInterface) {
246 return $result->getFirst();
247 } elseif (is_array($result)) {
248 return isset($result[0]) ? $result[0] : NULL;
249 }
250
251 } elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
252 $propertyName = lcfirst(substr($methodName, 7));
253 $query = $this->createQuery();
254 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
255 return $result;
256 }
257 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
258 }
259
260 /**
261 * Returns the class name of this class.
262 *
263 * @return string Class name of the repository.
264 */
265 protected function getRepositoryClassName() {
266 return get_class($this);
267 }
268
269 }