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