2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * The Extbase Persistence Manager
32 * @subpackage Persistence
33 * @version $Id: Manager.php 2293 2009-05-20 18:14:45Z robert $
36 class Tx_Extbase_Persistence_Manager
implements Tx_Extbase_Persistence_ManagerInterface
, t3lib_Singleton
{
39 * @var Tx_Extbase_Persistence_BackendInterface
44 * @var Tx_Extbase_Persistence_Session
49 * @var Tx_Extbase_Object_ManagerInterface
51 protected $objectManager;
54 * This is an array of registered repository class names.
58 protected $repositoryClassNames = array();
61 * Injects the Persistence Backend
63 * @param Tx_Extbase_Persistence_BackendInterface $backend The persistence backend
66 public function injectBackend(Tx_Extbase_Persistence_BackendInterface
$backend) {
67 $this->backend
= $backend;
72 * Injects the Persistence Session
74 * @param Tx_Extbase_Persistence_Session $session The persistence session
77 public function injectSession(Tx_Extbase_Persistence_Session
$session) {
78 $this->session
= $session;
82 * Injects the object manager
84 * @param Tx_Extbase_Object_ManagerInterface $objectManager
87 public function injectObjectManager(Tx_Extbase_Object_ManagerInterface
$objectManager) {
88 $this->objectManager
= $objectManager;
92 * Returns the current persistence session
94 * @return Tx_Extbase_Persistence_Session
96 public function getSession() {
97 return $this->session
;
101 * Returns the persistence backend
103 * @return Tx_Extbase_Persistence_BackendInterface
105 public function getBackend() {
106 return $this->backend
;
110 * Registers a repository
112 * @param string $className The class name of the repository to be reigistered
115 public function registerRepositoryClassName($className) {
116 $this->repositoryClassNames
[] = $className;
120 * Returns all repository class names
122 * @return array An array holding the registered repository class names
124 public function getRepositoryClassNames() {
125 return $this->repositoryClassNames
;
129 * Commits new objects and changes to objects in the current persistence
130 * session into the backend
135 public function persistAll() {
136 $aggregateRootObjects = new Tx_Extbase_Persistence_ObjectStorage();
137 $removedObjects = new Tx_Extbase_Persistence_ObjectStorage();
139 // fetch and inspect objects from all known repositories
140 $repositoryClassNames = $this->getRepositoryClassNames();
141 foreach ($repositoryClassNames as $repositoryClassName) {
142 $repository = $this->objectManager
->getObject($repositoryClassName);
143 $aggregateRootObjects->addAll($repository->getAddedObjects());
144 $removedObjects->addAll($repository->getRemovedObjects());
147 foreach ($this->session
->getReconstitutedObjects() as $reconstitutedObject) {
148 if (class_exists(str_replace('_Model_', '_Repository_', get_class($reconstitutedObject)) . 'Repository')) {
149 $aggregateRootObjects->attach($reconstitutedObject);
153 // hand in only aggregate roots, leaving handling of subobjects to
154 // the underlying storage layer
155 $this->backend
->setAggregateRootObjects($aggregateRootObjects);
156 $this->backend
->setDeletedObjects($removedObjects);
157 $this->backend
->commit();
159 // this needs to unregister more than just those, as at least some of
160 // the subobjects are supposed to go away as well...
161 // OTOH those do no harm, changes to the unused ones should not happen,
162 // so all they do is eat some memory.
163 foreach($removedObjects as $removedObject) {
164 $this->session
->unregisterReconstitutedObject($removedObject);