* added some fixtures for the unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / TX_EXTMVC_Persistence_Repository.php
1 <?php
2 declare(ENCODING = 'utf-8');
3
4 /* *
5 * This script belongs to the FLOW3 framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 require_once(PATH_t3lib . 'interfaces/interface.t3lib_singleton.php');
25 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Utility/TX_EXTMVC_Utility_Strings.php');
26 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Persistence_ObjectStorage.php');
27 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Persistence_RepositoryInterface.php');
28
29 /**
30 * The base repository - will usually be extended by a more concrete repository.
31 *
32 * @version $Id:$
33 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
34 */
35 class TX_EXTMVC_Persistence_Repository implements TX_EXTMVC_Persistence_RepositoryInterface, t3lib_Singleton {
36
37 // TODO make abstract
38
39 /**
40 * Class Name of the aggregate root
41 *
42 * @var string
43 */
44 protected $aggregateRootClassName;
45
46 /**
47 * Objects of this repository
48 *
49 * @var TX_EXTMVC_Persistence_ObjectStorage
50 */
51 protected $objects;
52
53 /**
54 * Contains the persistence session of the current extension
55 *
56 * @var TX_EXTMVC_Persistence_Session
57 */
58 protected $session;
59
60 /**
61 * Holds an array of allowed properties to be called via magig findBy methods
62 *
63 * @var array
64 */
65 protected $allowedFindByProperties = array();
66
67 /**
68 * The content object
69 *
70 * @var tslib_cObj
71 **/
72 protected $cObj;
73
74 /**
75 * Constructs a new Repository
76 *
77 * @author Karsten Dambekalns <karsten@typo3.org>
78 */
79 public function __construct() {
80 $this->objects = new TX_EXTMVC_Persistence_ObjectStorage();
81 $this->cObj = t3lib_div::makeInstance('tslib_cObj');
82 $repositoryClassName = get_class($this);
83 if (substr($repositoryClassName, -10) == 'Repository' && substr($repositoryClassName, -11, 1) != '_') {
84 $this->aggregateRootClassName = substr($repositoryClassName, 0, -10);
85 }
86 $this->dataMapper = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Mapper_TcaMapper'); // singleton
87 $this->session = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Session'); // singleton
88 $this->session->registerAggregateRootClassName($this->aggregateRootClassName);
89 // TODO auto resolve findBy properties
90 $this->allowedFindByProperties = array('name');
91 }
92
93 /**
94 * Sets the class name of the aggregare root
95 *
96 * @param string $aggregateRootClassName
97 * @return void
98 * @author Jochen Rau <jochen.rau@typoplanet.de>
99 */
100 public function setAggregateRootClassName($aggregateRootClassName) {
101 $this->aggregateRootClassName = $aggregateRootClassName;
102 $this->session->registerAggregateRootClassName($this->aggregateRootClassName);
103 }
104
105 /**
106 * Returns the class name of the aggregare root
107 *
108 * @return string The class name of the aggregate root
109 * @author Jochen Rau <jochen.rau@typoplanet.de>
110 */
111 public function getAggregateRootClassName() {
112 // TODO throw exception if not set
113 return $this->aggregateRootClassName;
114 }
115
116 /**
117 * Adds an object to this repository
118 *
119 * @param object $object The object to add
120 * @return void
121 * @author Robert Lemke <robert@typo3.org>
122 * @author Karsten Dambekalns <karsten@typo3.org>
123 */
124 public function add($object) {
125 if (!($object instanceof $this->aggregateRootClassName)) throw new TX_EXTMVC_Persistence_Exception_InvalidClass('The class "' . get_class($object) . '" is not supported by the repository.');
126 $this->objects->attach($object);
127 $this->session->registerAddedObject($object);
128 }
129
130 /**
131 * Removes an object from this repository.
132 *
133 * @param object $object The object to remove
134 * @return void
135 * @author Jochen Rau <jochen.rau@typoplanet.de>
136 */
137 public function remove($object) {
138 if (!($object instanceof $this->aggregateRootClassName)) throw new TX_EXTMVC_Persistence_Exception_InvalidClass('The class "' . get_class($object) . '" is not supported by the repository.');
139 $this->objects->detach($object);
140 $this->session->registerRemovedObject($object);
141 }
142
143 /**
144 * Dispatches magic methods (findByProperty())
145 *
146 * @param string $methodName The name of the magic method
147 * @param string $arguments The arguments of the magic method
148 * @throws TX_EXTMVC_Persistence_Exception_UnsupportedMethod
149 * @return void
150 * @author Jochen Rau <jochen.rau@typoplanet.de>
151 */
152 public function __call($methodName, $arguments) {
153 if (substr($methodName, 0, 6) === 'findBy') {
154 $propertyName = TX_EXTMVC_Utility_Strings::lowercaseFirst(substr($methodName,6));
155 if (in_array($propertyName, $this->allowedFindByProperties)) {
156 return $this->findByProperty($propertyName, $arguments[0]);
157 }
158 }
159 throw new TX_EXTMVC_Persistence_Exception_UnsupportedMethod('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
160 }
161
162 /**
163 * Returns all objects of this repository
164 *
165 * @return array An array of objects, empty if no objects found
166 * @author Jochen Rau <jochen.rau@typoplanet.de>
167 */
168 public function findAll() {
169 return $this->dataMapper->findWhere($this->aggregateRootClassName);
170 }
171
172 /**
173 * Finds objects matching 'property=xyz'
174 *
175 * @param string $propertyName The name of the property (will be checked by a white list)
176 * @param string $arguments The arguments of the magic findBy method
177 * @return void
178 * @author Jochen Rau <jochen.rau@typoplanet.de>
179 */
180 private function findByProperty($propertyName, $value) {
181 $where = $propertyName . '=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($value, 'foo');
182 return $this->dataMapper->findWhere($this->aggregateRootClassName, $where);
183 }
184
185 }
186 ?>