TYPO3 API  SVNRelease
Manager.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * The Extbase Persistence Manager
00030  *
00031  * @package Extbase
00032  * @subpackage Persistence
00033  * @version $Id: Manager.php 1971 2010-03-08 16:59:04Z jocrau $
00034  * @api
00035  */
00036 class Tx_Extbase_Persistence_Manager implements Tx_Extbase_Persistence_ManagerInterface, t3lib_Singleton {
00037 
00038     /**
00039      * @var Tx_Extbase_Persistence_BackendInterface
00040      */
00041     protected $backend;
00042 
00043     /**
00044      * @var Tx_Extbase_Persistence_Session
00045      */
00046     protected $session;
00047 
00048     /**
00049      * @var Tx_Extbase_Object_ObjectManagerInterface
00050      */
00051     protected $objectManager;
00052 
00053     /**
00054      * This is an array of registered repository class names.
00055      *
00056      * @var array
00057      */
00058     protected $repositoryClassNames = array();
00059 
00060     /**
00061      * Injects the Persistence Backend
00062      *
00063      * @param Tx_Extbase_Persistence_BackendInterface $backend The persistence backend
00064      * @return void
00065      */
00066     public function injectBackend(Tx_Extbase_Persistence_BackendInterface $backend) {
00067         $this->backend = $backend;
00068     }
00069 
00070     /**
00071      *
00072      * Injects the Persistence Session
00073      *
00074      * @param Tx_Extbase_Persistence_Session $session The persistence session
00075      * @return void
00076      */
00077     public function injectSession(Tx_Extbase_Persistence_Session $session) {
00078         $this->session = $session;
00079     }
00080 
00081     /**
00082      * Injects the object manager
00083      *
00084      * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
00085      * @return void
00086      */
00087     public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
00088         $this->objectManager = $objectManager;
00089     }
00090 
00091     /**
00092      * Returns the current persistence session
00093      *
00094      * @return Tx_Extbase_Persistence_Session
00095      */
00096     public function getSession() {
00097         return $this->session;
00098     }
00099 
00100     /**
00101      * Returns the persistence backend
00102      *
00103      * @return Tx_Extbase_Persistence_BackendInterface
00104      */
00105     public function getBackend() {
00106         return $this->backend;
00107     }
00108 
00109     /**
00110      * Registers a repository
00111      *
00112      * @param string $className The class name of the repository to be reigistered
00113      * @return void
00114      */
00115     public function registerRepositoryClassName($className) {
00116         $this->repositoryClassNames[] = $className;
00117     }
00118 
00119     /**
00120      * Returns the number of records matching the query.
00121      *
00122      * @param Tx_Extbase_Persistence_QueryInterface $query
00123      * @return integer
00124      * @api
00125      */
00126     public function getObjectCountByQuery(Tx_Extbase_Persistence_QueryInterface $query) {
00127         return $this->backend->getObjectCountByQuery($query);
00128     }
00129 
00130     /**
00131      * Returns the object data matching the $query.
00132      *
00133      * @param Tx_Extbase_Persistence_QueryInterface $query
00134      * @return array
00135      * @api
00136      */
00137     public function getObjectDataByQuery(Tx_Extbase_Persistence_QueryInterface $query) {
00138         return $this->backend->getObjectDataByQuery($query);
00139     }
00140 
00141     /**
00142      * Commits new objects and changes to objects in the current persistence
00143      * session into the backend
00144      *
00145      * @return void
00146      * @api
00147      */
00148     public function persistAll() {
00149         $aggregateRootObjects = new Tx_Extbase_Persistence_ObjectStorage();
00150         $removedObjects = new Tx_Extbase_Persistence_ObjectStorage();
00151 
00152             // fetch and inspect objects from all known repositories
00153         foreach ($this->repositoryClassNames as $repositoryClassName) {
00154             $repository = $this->objectManager->get($repositoryClassName);
00155             $aggregateRootObjects->addAll($repository->getAddedObjects());
00156             $removedObjects->addAll($repository->getRemovedObjects());
00157         }
00158 
00159         foreach ($this->session->getReconstitutedObjects() as $reconstitutedObject) {
00160             if (class_exists(str_replace('_Model_', '_Repository_', get_class($reconstitutedObject)) . 'Repository')) {
00161                 $aggregateRootObjects->attach($reconstitutedObject);
00162             }
00163         }
00164 
00165             // hand in only aggregate roots, leaving handling of subobjects to
00166             // the underlying storage layer
00167         $this->backend->setAggregateRootObjects($aggregateRootObjects);
00168         $this->backend->setDeletedObjects($removedObjects);
00169         $this->backend->commit();
00170 
00171             // this needs to unregister more than just those, as at least some of
00172             // the subobjects are supposed to go away as well...
00173             // OTOH those do no harm, changes to the unused ones should not happen,
00174             // so all they do is eat some memory.
00175         foreach($removedObjects as $removedObject) {
00176             $this->session->unregisterReconstitutedObject($removedObject);
00177         }
00178     }
00179 
00180 }
00181 ?>