[+BUGFIX] Extbase (Persistence): Fixed a problem where the cache was cleared at every...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Manager.php
index e74ac40..4ac8fd4 100644 (file)
@@ -31,7 +31,7 @@
  * @package Extbase
  * @subpackage Persistence
  * @version $Id: Manager.php 2293 2009-05-20 18:14:45Z robert $
- *
+ * @api
  */
 class Tx_Extbase_Persistence_Manager implements Tx_Extbase_Persistence_ManagerInterface, t3lib_Singleton {
 
@@ -46,49 +46,55 @@ class Tx_Extbase_Persistence_Manager implements Tx_Extbase_Persistence_ManagerIn
        protected $session;
 
        /**
-        * @var Tx_Extbase_Persistence_DataMapper
+        * @var Tx_Extbase_Object_ManagerInterface
+        */
+       protected $objectManager;
+
+       /**
+        * This is an array of registered repository class names.
+        *
+        * @var array
         */
-       protected $dataMapper;
+       protected $repositoryClassNames = array();
 
        /**
-        * Constructor
+        * Injects the Persistence Backend
         *
-        * @param Tx_Extbase_Persistence_BackendInterface $backend the backend to use for persistence
-        * @internal
+        * @param Tx_Extbase_Persistence_BackendInterface $backend The persistence backend
+        * @return void
         */
-       public function __construct(Tx_Extbase_Persistence_BackendInterface $backend) {
+       public function injectBackend(Tx_Extbase_Persistence_BackendInterface $backend) {
                $this->backend = $backend;
        }
 
        /**
-        * Injects the persistence session
+        *
+        * Injects the Persistence Session
         *
         * @param Tx_Extbase_Persistence_Session $session The persistence session
         * @return void
-        * @internal
         */
        public function injectSession(Tx_Extbase_Persistence_Session $session) {
                $this->session = $session;
        }
 
        /**
-        * Returns the current persistence session
+        * Injects the object manager
         *
-        * @return Tx_Extbase_Persistence_Session
-        * @internal
+        * @param Tx_Extbase_Object_ManagerInterface $objectManager
+        * @return void
         */
-       public function getSession() {
-               return $this->session;
+       public function injectObjectManager(Tx_Extbase_Object_ManagerInterface $objectManager) {
+               $this->objectManager = $objectManager;
        }
 
        /**
-        * Returns the current Data Mapper
+        * Returns the current persistence session
         *
-        * @return Tx_Extbase_Persistence_Mapper_DataMapper
-        * @internal
+        * @return Tx_Extbase_Persistence_Session
         */
-       public function getDataMapper() {
-               return $this->dataMapper;
+       public function getSession() {
+               return $this->session;
        }
 
        /**
@@ -100,22 +106,64 @@ class Tx_Extbase_Persistence_Manager implements Tx_Extbase_Persistence_ManagerIn
                return $this->backend;
        }
 
+       /**
+        * Registers a repository
+        *
+        * @param string $className The class name of the repository to be reigistered
+        * @return void
+        */
+       public function registerRepositoryClassName($className) {
+               $this->repositoryClassNames[] = $className;
+       }
+
+       /**
+        * Returns all repository class names
+        *
+        * @return array An array holding the registered repository class names
+        */
+       public function getRepositoryClassNames() {
+               return $this->repositoryClassNames;
+       }
+
        /**
         * Commits new objects and changes to objects in the current persistence
         * session into the backend
         *
         * @return void
+        * @api
         */
        public function persistAll() {
                $aggregateRootObjects = new Tx_Extbase_Persistence_ObjectStorage();
-               $aggregateRootObjects->addAll($this->session->getAddedObjects());
-               $aggregateRootObjects->addAll($this->session->getReconstitutedObjects());
+               $removedObjects = new Tx_Extbase_Persistence_ObjectStorage();
 
-               $removedObjects = $this->session->getRemovedObjects();
+                       // fetch and inspect objects from all known repositories
+               $repositoryClassNames = $this->getRepositoryClassNames();
+               foreach ($repositoryClassNames as $repositoryClassName) {
+                       $repository = $this->objectManager->getObject($repositoryClassName);
+                       $aggregateRootObjects->addAll($repository->getAddedObjects());
+                       $removedObjects->addAll($repository->getRemovedObjects());
+               }
 
+               foreach ($this->session->getReconstitutedObjects() as $reconstitutedObject) {
+                       if (class_exists(str_replace('_Model_', '_Repository_', get_class($reconstitutedObject)) . 'Repository')) {
+                               $aggregateRootObjects->attach($reconstitutedObject);
+                       }
+               }
+
+                       // hand in only aggregate roots, leaving handling of subobjects to
+                       // the underlying storage layer
                $this->backend->setAggregateRootObjects($aggregateRootObjects);
                $this->backend->setDeletedObjects($removedObjects);
                $this->backend->commit();
+
+                       // this needs to unregister more than just those, as at least some of
+                       // the subobjects are supposed to go away as well...
+                       // OTOH those do no harm, changes to the unused ones should not happen,
+                       // so all they do is eat some memory.
+               foreach($removedObjects as $removedObject) {
+                       $this->session->unregisterReconstitutedObject($removedObject);
+               }
        }
+
 }
 ?>
\ No newline at end of file