TYPO3 API  SVNRelease
LazyObjectStorage.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  * A proxy that can replace any object and replaces itself in it's parent on
00030  * first access (call, get, set, isset, unset).
00031  *
00032  * @package Extbase
00033  * @subpackage Persistence
00034  * @version $Id$
00035  */
00036 class Tx_Extbase_Persistence_LazyObjectStorage extends Tx_Extbase_Persistence_ObjectStorage implements Tx_Extbase_Persistence_LoadingStrategyInterface {
00037 
00038     /**
00039      * This field is only needed to make debugging easier:
00040      * If you call current() on a class that implements Iterator, PHP will return the first field of the object
00041      * instead of calling the current() method of the interface.
00042      * We use this unusual behavior of PHP to return the warning below in this case.
00043      *
00044      * @var string
00045      */
00046     private $warning = 'You should never see this warning. If you do, you probably used PHP array functions like current() on the Tx_Extbase_Persistence_LazyObjectStorage. To retrieve the first result, you can use the getFirst() method.';
00047 
00048     /**
00049      * @var Tx_Extbase_Persistence_DataMapper
00050      */
00051     protected $dataMapper;
00052 
00053     /**
00054      * The object this property is contained in.
00055      *
00056      * @var object
00057      */
00058     protected $parentObject;
00059 
00060     /**
00061      * The name of the property represented by this proxy.
00062      *
00063      * @var string
00064      */
00065     protected $propertyName;
00066 
00067     /**
00068      * The raw field value.
00069      *
00070      * @var mixed
00071      */
00072     protected $fieldValue;
00073 
00074     /**
00075      *
00076      * @var bool
00077      */
00078     protected $isInitialized = FALSE;
00079 
00080     /**
00081      * Returns the state of the initialization
00082      *
00083      * @return void
00084      */
00085     public function isInitialized() {
00086         return $this->isInitialized;
00087     }
00088     
00089     /**
00090      * Constructs this proxy instance.
00091      *
00092      * @param object $parentObject The object instance this proxy is part of
00093      * @param string $propertyName The name of the proxied property in it's parent
00094      * @param mixed $fieldValue The raw field value.
00095      */
00096     public function __construct($parentObject, $propertyName, $fieldValue) {
00097         $this->parentObject = $parentObject;
00098         $this->propertyName = $propertyName;
00099         $this->fieldValue = $fieldValue;
00100     }
00101 
00102     /**
00103      * Injects the DataMapper to map nodes to objects
00104      *
00105      * @param Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper
00106      * @return void
00107      */
00108     public function injectDataMapper(Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper) {
00109         $this->dataMapper = $dataMapper;
00110     }
00111 
00112     /**
00113      * This is a function lazy load implementation. 
00114      *
00115      * @return void
00116      */
00117     protected function initialize() {
00118         if (!$this->isInitialized) {
00119             $this->isInitialized = TRUE;
00120 
00121             $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, FALSE);
00122             foreach ($objects as $object) {
00123                 parent::attach($object);
00124             }
00125             $this->_memorizeCleanState();
00126             $this->parentObject->_memorizeCleanState($this->propertyName);
00127         }
00128     }
00129         
00130     // Delegation to the ObjectStorage methods below
00131 
00132     /**
00133      * @see Tx_Extbase_Persistence_ObjectStorage::addAll
00134      */
00135     public function addAll($storage) {
00136         $this->initialize();
00137         parent::addAll($storage);
00138     }
00139 
00140     /**
00141      * @see Tx_Extbase_Persistence_ObjectStorage::attach
00142      */
00143     public function attach($object, $data = NULL) {
00144         $this->initialize();
00145         parent::attach($object, $data);
00146     }
00147 
00148     /**
00149      * @see Tx_Extbase_Persistence_ObjectStorage::contains
00150      */
00151     public function contains($object) {
00152         $this->initialize();
00153         return parent::contains($object);
00154     }
00155 
00156     /**
00157      * Counts the elements in the storage array
00158      *
00159      * @return int The number of elements in the ObjectStorage
00160      */
00161     public function count() {
00162         $columnMap = $this->dataMapper->getDataMap(get_class($this->parentObject))->getColumnMap($this->propertyName);
00163         $numberOfElements = NULL;
00164         if ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
00165             $numberOfElements = $this->dataMapper->countRelated($this->parentObject, $this->propertyName, $this->fieldValue);
00166         } else {
00167             $this->initialize();
00168             $numberOfElements = count($this->storage);          
00169         }
00170         if (is_null($numberOfElements)) {
00171             throw new Tx_Extbase_Persistence_Exception('The number of elements could not be determined.', 1252514486);
00172         }
00173         return $numberOfElements;
00174     }
00175 
00176     /**
00177      * @see Tx_Extbase_Persistence_ObjectStorage::current
00178      */
00179     public function current() {
00180         $this->initialize();
00181         return parent::current();
00182     }
00183 
00184     /**
00185      * @see Tx_Extbase_Persistence_ObjectStorage::detach
00186      */
00187     public function detach($object) {
00188         $this->initialize();
00189         parent::detach($object);
00190     }
00191 
00192     /**
00193      * @see Tx_Extbase_Persistence_ObjectStorage::key
00194      */
00195     public function key() {
00196         $this->initialize();
00197         return parent::key();
00198     }
00199 
00200     /**
00201      * @see Tx_Extbase_Persistence_ObjectStorage::next
00202      */
00203     public function next() {
00204         $this->initialize();
00205         parent::next();
00206     }
00207 
00208     /**
00209      * @see Tx_Extbase_Persistence_ObjectStorage::offsetExists
00210      */
00211     public function offsetExists($object) {
00212         $this->initialize();
00213         return parent::offsetExists($object);
00214     }
00215 
00216     /**
00217      * @see Tx_Extbase_Persistence_ObjectStorage::offsetGet
00218      */
00219     public function offsetGet($object) {
00220         $this->initialize();
00221         return parent::offsetGet($object);
00222     }
00223 
00224     /**
00225      * @see Tx_Extbase_Persistence_ObjectStorage::offsetSet
00226      */
00227     public function offsetSet($object , $info) {
00228         $this->initialize();
00229         parent::offsetSet($object, $info);
00230     }
00231 
00232     /**
00233      * @see Tx_Extbase_Persistence_ObjectStorage::offsetUnset
00234      */
00235     public function offsetUnset($object) {
00236         $this->initialize();
00237         parent::offsetUnset($object);
00238     }
00239 
00240     /**
00241      * @see Tx_Extbase_Persistence_ObjectStorage::removeAll
00242      */
00243     public function removeAll($storage) {
00244         $this->initialize();
00245         parent::removeAll($storage);
00246     }
00247 
00248     /**
00249      * @see Tx_Extbase_Persistence_ObjectStorage::rewind
00250      */
00251     public function rewind() {
00252         $this->initialize();
00253         parent::rewind();
00254     }
00255 
00256     /**
00257      * @see Tx_Extbase_Persistence_ObjectStorage::valid
00258      */
00259     public function valid() {
00260         $this->initialize();
00261         return parent::valid();
00262     }
00263     
00264     /**
00265      * @see Tx_Extbase_Persistence_ObjectStorage::toArray
00266      */
00267     public function toArray() {
00268         $this->initialize();
00269         return parent::toArray();
00270     }
00271         
00272 }
00273 ?>