[TASK] Move classAliasMaps into compatibility6 extension part 5
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / AbstractRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
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 * Abstract repository implementing the basic repository methods
18 *
19 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
20 * @author Ingmar Schlecht <ingmar@typo3.org>
21 */
22 abstract class AbstractRepository implements \TYPO3\CMS\Extbase\Persistence\RepositoryInterface, \TYPO3\CMS\Core\SingletonInterface {
23
24 /**
25 * @var string
26 */
27 protected $table = '';
28
29 /**
30 * @var ResourceFactory
31 */
32 protected $factory;
33
34 /**
35 * @var string
36 */
37 protected $typeField = '';
38
39 /**
40 * @var string
41 */
42 protected $type = '';
43
44 /**
45 * Creates this object.
46 */
47 public function __construct() {
48 $this->factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
49 }
50
51 /**
52 * Adds an object to this repository.
53 *
54 * @param object $object The object to add
55 * @return void
56 * @api
57 */
58 public function add($object) {
59
60 }
61
62 /**
63 * Removes an object from this repository.
64 *
65 * @param object $object The object to remove
66 * @return void
67 * @api
68 */
69 public function remove($object) {
70
71 }
72
73 /**
74 * Replaces an object by another.
75 *
76 * @param object $existingObject The existing object
77 * @param object $newObject The new object
78 * @return void
79 * @api
80 */
81 public function replace($existingObject, $newObject) {
82
83 }
84
85 /**
86 * Replaces an existing object with the same identifier by the given object
87 *
88 * @param object $modifiedObject The modified object
89 * @api
90 */
91 public function update($modifiedObject) {
92
93 }
94
95 /**
96 * Returns all objects of this repository add()ed but not yet persisted to
97 * the storage layer.
98 *
99 * @return array An array of objects
100 */
101 public function getAddedObjects() {
102
103 }
104
105 /**
106 * Returns an array with objects remove()d from the repository that
107 * had been persisted to the storage layer before.
108 *
109 * @return array
110 */
111 public function getRemovedObjects() {
112
113 }
114
115 /**
116 * Returns all objects of this repository.
117 *
118 * @return array An array of objects, empty if no objects found
119 * @api
120 */
121 public function findAll() {
122 $itemList = array();
123 $whereClause = '1=1';
124 if ($this->type != '') {
125 $whereClause .= ' AND ' . $this->typeField . ' = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->type, $this->table) . ' ';
126 }
127 $whereClause .= $this->getWhereClauseForEnabledFields();
128 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->table, $whereClause);
129 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
130 $itemList[] = $this->createDomainObject($row);
131 }
132 $GLOBALS['TYPO3_DB']->sql_free_result($res);
133 return $itemList;
134 }
135
136 /**
137 * Creates an object managed by this repository.
138 *
139 * @abstract
140 * @param array $databaseRow
141 * @return object
142 */
143 abstract protected function createDomainObject(array $databaseRow);
144
145 /**
146 * Returns the total number objects of this repository.
147 *
148 * @return int The object count
149 * @api
150 */
151 public function countAll() {
152
153 }
154
155 /**
156 * Removes all objects of this repository as if remove() was called for
157 * all of them.
158 *
159 * @return void
160 * @api
161 */
162 public function removeAll() {
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 *
171 * @throws \RuntimeException
172 * @throws \InvalidArgumentException
173 * @return object The matching object
174 * @api
175 */
176 public function findByUid($uid) {
177 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($uid)) {
178 throw new \InvalidArgumentException('uid has to be integer.', 1316779798);
179 }
180 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', $this->table, 'uid=' . (int)$uid . $this->getWhereClauseForEnabledFields());
181 if (empty($row) || !is_array($row)) {
182 throw new \RuntimeException('Could not find row with uid "' . $uid . '" in table ' . $this->table, 1314354065);
183 }
184 return $this->createDomainObject($row);
185 }
186
187 /**
188 * get the WHERE clause for the enabled fields of this TCA table
189 * depending on the context
190 *
191 * @return string the additional where clause, something like " AND deleted=0 AND hidden=0"
192 */
193 protected function getWhereClauseForEnabledFields() {
194 if ($this->getEnvironmentMode() === 'FE' && $GLOBALS['TSFE']->sys_page) {
195 // frontend context
196 $whereClause = $GLOBALS['TSFE']->sys_page->enableFields($this->table);
197 $whereClause .= $GLOBALS['TSFE']->sys_page->deleteClause($this->table);
198 } else {
199 // backend context
200 $whereClause = \TYPO3\CMS\Backend\Utility\BackendUtility::BEenableFields($this->table);
201 $whereClause .= \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($this->table);
202 }
203 return $whereClause;
204 }
205
206 /**
207 * Sets the property names to order the result by per default.
208 * Expected like this:
209 * array(
210 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
211 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
212 * )
213 *
214 * @param array $defaultOrderings The property names to order by
215 *
216 * @throws \BadMethodCallException
217 * @return void
218 * @api
219 */
220 public function setDefaultOrderings(array $defaultOrderings) {
221 throw new \BadMethodCallException('Repository does not support the setDefaultOrderings() method.', 1313185906);
222 }
223
224 /**
225 * Sets the default query settings to be used in this repository
226 *
227 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
228 *
229 * @throws \BadMethodCallException
230 * @return void
231 * @api
232 */
233 public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings) {
234 throw new \BadMethodCallException('Repository does not support the setDefaultQuerySettings() method.', 1313185907);
235 }
236
237 /**
238 * Returns a query for objects of this repository
239 *
240 * @throws \BadMethodCallException
241 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
242 * @api
243 */
244 public function createQuery() {
245 throw new \BadMethodCallException('Repository does not support the createQuery() method.', 1313185908);
246 }
247
248 /**
249 * Finds an object matching the given identifier.
250 *
251 * @param mixed $identifier The identifier of the object to find
252 * @return object The matching object if found, otherwise NULL
253 * @api
254 */
255 public function findByIdentifier($identifier) {
256 return $this->findByUid($identifier);
257 }
258
259 /**
260 * Magic call method for repository methods.
261 *
262 * @param string $method Name of the method
263 * @param array $arguments The arguments
264 *
265 * @throws \BadMethodCallException
266 * @return void
267 */
268 public function __call($method, $arguments) {
269 throw new \BadMethodCallException('Repository method "' . $method . '" is not implemented.', 1378918410);
270 }
271
272 /**
273 * Returns the object type this repository is managing.
274 *
275 * @return string
276 * @api
277 */
278 public function getEntityClassName() {
279 return $this->objectType;
280 }
281
282 /**
283 * Function to return the current TYPO3_MODE.
284 * This function can be mocked in unit tests to be able to test frontend behaviour.
285 *
286 * @return string
287 */
288 protected function getEnvironmentMode() {
289 return TYPO3_MODE;
290 }
291
292 /**
293 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
294 */
295 protected function getDatabaseConnection() {
296 return $GLOBALS['TYPO3_DB'];
297 }
298
299 }