4182d6adacb98e48fffaac34ab35b508e7428601
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Repository.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 require_once(PATH_t3lib . 'interfaces/interface.t3lib_singleton.php');
26 require_once(PATH_tslib . 'class.tslib_content.php');
27
28 /**
29 * The base repository - will usually be extended by a more concrete repository.
30 *
31 * @package TYPO3
32 * @subpackage extbase
33 * @version $ID:$
34 */
35 abstract class Tx_ExtBase_Persistence_Repository implements Tx_ExtBase_Persistence_RepositoryInterface, t3lib_Singleton {
36
37 /**
38 * Class Name of the aggregate root
39 *
40 * @var string
41 */
42 protected $aggregateRootClassName;
43
44 /**
45 * Objects of this repository
46 *
47 * @var Tx_ExtBase_Persistence_ObjectStorage
48 */
49 protected $objects;
50
51 /**
52 * Contains the persistence session of the current extension
53 *
54 * @var Tx_ExtBase_Persistence_Session
55 */
56 protected $persistenceSession;
57
58 /**
59 * Constructs a new Repository
60 *
61 */
62 public function __construct($aggregateRootClassName = NULL) {
63 $this->objects = new Tx_ExtBase_Persistence_ObjectStorage();
64 $repositoryClassName = get_class($this);
65 $repositoryPosition = strrpos($repositoryClassName, 'Repository');
66 if ($aggregateRootClassName != NULL) {
67 $this->aggregateRootClassName = $aggregateRootClassName;
68 } elseif (substr($repositoryClassName, -10) == 'Repository' && substr($repositoryClassName, -11, 1) != '_') {
69 $this->aggregateRootClassName = substr($repositoryClassName, 0, -10);
70 }
71 if (empty($this->aggregateRootClassName)) {
72 throw new Tx_ExtBase_Exception('The domain repository wasn\'t able to resolve the aggregate root class to manage.', 1237897039);
73 }
74 if (!in_array('Tx_ExtBase_DomainObject_DomainObjectInterface', class_implements($this->aggregateRootClassName))) {
75 throw new Tx_ExtBase_Exception('The domain repository tried to manage objects which are not implementing the Tx_ExtBase_DomainObject_DomainObjectInterface.', 1237897039);
76 }
77 $this->dataMapper = t3lib_div::makeInstance('Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper'); // singleton
78 $this->persistenceSession = t3lib_div::makeInstance('Tx_ExtBase_Persistence_Session'); // singleton
79 $this->persistenceSession->registerAggregateRootClassName($this->aggregateRootClassName);
80 }
81
82 /**
83 * Adds an object to this repository
84 *
85 * @param object $object The object to add
86 * @return void
87 */
88 public function add($object) {
89 if (!($object instanceof $this->aggregateRootClassName)) throw new Tx_ExtBase_Persistence_Exception_InvalidClass('The class "' . get_class($object) . '" is not supported by the repository.');
90 $this->objects->attach($object);
91 $this->persistenceSession->registerAddedObject($object);
92 }
93
94 /**
95 * Removes an object from this repository.
96 *
97 * @param object $object The object to remove
98 * @return void
99 */
100 public function remove($object) {
101 if (!($object instanceof $this->aggregateRootClassName)) throw new Tx_ExtBase_Persistence_Exception_InvalidClass('The class "' . get_class($object) . '" is not supported by the repository.');
102 $this->objects->detach($object);
103 $this->persistenceSession->registerRemovedObject($object);
104 }
105
106 /**
107 * Dispatches magic methods (findBy[Property]())
108 *
109 * @param string $methodName The name of the magic method
110 * @param string $arguments The arguments of the magic method
111 * @throws Tx_ExtBase_Persistence_Exception_UnsupportedMethod
112 * @return void
113 */
114 public function __call($methodName, $arguments) {
115 if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
116 $propertyName = Tx_ExtBase_Utility_Strings::lowercaseFirst(substr($methodName,6));
117 return $this->find(array($propertyName => $arguments[0]));
118 } elseif (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
119 $propertyName = Tx_ExtBase_Utility_Strings::lowercaseFirst(substr($methodName,9));
120 $result = $this->find(array($propertyName => $arguments[0]), '', '', 1);
121 if (count($result) > 0) {
122 return $result[0];
123 } else {
124 return NULL;
125 }
126 }
127 throw new Tx_ExtBase_Persistence_Exception_UnsupportedMethod('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
128 }
129
130
131 /**
132 * Find objects by multiple conditions. Either as SQL parts or query by example.
133 *
134 * The following condition array would find entities with description like the given keyword and
135 * name equal to "foo".
136 *
137 * <pre>
138 * array(
139 * array('blog_description LIKE ?', $keword),
140 * 'blogName' => 'Foo'
141 * )
142 * </pre>
143 *
144 * Note: The SQL part uses the database columns names, the query by example syntax uses
145 * the object property name (camel-cased, without underscore).
146 *
147 * @param array|string $conditions The conditions as an array or SQL string
148 * @param string $groupBy Group by SQL part
149 * @param string $orderBy Order by SQL part
150 * @param string $limit Limit SQL part
151 * @param bool $useEnableFields Wether to automatically restrict the query by enable fields
152 * @return array An array of objects, empty if no objects found
153 */
154 public function find($conditions = '', $groupBy = '', $orderBy = '', $limit = '', $useEnableFields = TRUE) {
155 return $this->dataMapper->find($this->aggregateRootClassName, $conditions, $groupBy, $orderBy, $limit, $useEnableFields);
156 }
157
158 /**
159 * Returns all objects of this repository
160 *
161 * @return array An array of objects, empty if no objects found
162 */
163 public function findAll() {
164 return $this->find();
165 }
166 }
167 ?>