TYPO3 API  SVNRelease
LazyLoadingProxy.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: LazyLoadingProxy.php 2262 2010-05-02 09:37:34Z jocrau $
00035  */
00036 class Tx_Extbase_Persistence_LazyLoadingProxy implements Iterator, Tx_Extbase_Persistence_LoadingStrategyInterface {
00037 
00038     /**
00039      * @var Tx_Extbase_Persistence_DataMapper
00040      */
00041     protected $dataMapper;
00042 
00043     /**
00044      * The object this property is contained in.
00045      *
00046      * @var object
00047      */
00048     private $parentObject;
00049 
00050     /**
00051      * The name of the property represented by this proxy.
00052      *
00053      * @var string
00054      */
00055     private $propertyName;
00056 
00057     /**
00058      * The raw field value.
00059      *
00060      * @var mixed
00061      */
00062     private $fieldValue;
00063 
00064     /**
00065      * Constructs this proxy instance.
00066      *
00067      * @param object $parentObject The object instance this proxy is part of
00068      * @param string $propertyName The name of the proxied property in it's parent
00069      * @param mixed $fieldValue The raw field value.
00070      */
00071     public function __construct($parentObject, $propertyName, $fieldValue) {
00072         $this->parentObject = $parentObject;
00073         $this->propertyName = $propertyName;
00074         $this->fieldValue = $fieldValue;
00075     }
00076 
00077     /**
00078      * Injects the DataMapper to map nodes to objects
00079      *
00080      * @param Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper
00081      * @return void
00082      */
00083     public function injectDataMapper(Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper) {
00084         $this->dataMapper = $dataMapper;
00085     }
00086 
00087     /**
00088      * Populate this proxy by asking the $population closure.
00089      *
00090      * @return object The instance (hopefully) returned
00091      */
00092     public function _loadRealInstance() {
00093         // this check safeguards against a proxy being activated multiple times
00094         // usually that does not happen, but if the proxy is held from outside
00095         // it's parent... the result would be weird.
00096         if ($this->parentObject->_getProperty($this->propertyName) instanceof Tx_Extbase_Persistence_LazyLoadingProxy) {
00097             
00098             $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, FALSE, FALSE);
00099             $propertyValue = $this->dataMapper->mapResultToPropertyValue($this->parentObject, $this->propertyName, $objects);
00100             $this->parentObject->_setProperty($this->propertyName, $propertyValue);
00101             $this->parentObject->_memorizeCleanState($this->propertyName);
00102             return $propertyValue;
00103         } else {
00104             return $this->parentObject->_getProperty($this->propertyName);
00105         }
00106     }
00107 
00108     /**
00109      * Magic method call implementation.
00110      *
00111      * @param string $methodName The name of the property to get
00112      * @param array $arguments The arguments given to the call
00113      * @return mixed
00114      */
00115     public function __call($methodName, $arguments) {
00116         $realInstance = $this->_loadRealInstance();
00117         if (!is_object($realInstance)) {
00118             return NULL;
00119         }
00120         return call_user_func_array(array($realInstance, $methodName), $arguments);
00121     }
00122 
00123     /**
00124      * Magic get call implementation.
00125      *
00126      * @param string $propertyName The name of the property to get
00127      * @return mixed
00128      */
00129     public function __get($propertyName) {
00130         $realInstance = $this->_loadRealInstance();
00131         return $realInstance->$propertyName;
00132     }
00133 
00134     /**
00135      * Magic set call implementation.
00136      *
00137      * @param string $propertyName The name of the property to set
00138      * @param mixed $value The value for the property to set
00139      * @return void
00140      */
00141     public function __set($propertyName, $value) {
00142         $realInstance = $this->_loadRealInstance();
00143         $realInstance->$propertyName = $value;
00144     }
00145 
00146     /**
00147      * Magic isset call implementation.
00148      *
00149      * @param string $propertyName The name of the property to check
00150      * @return boolean
00151      */
00152     public function __isset($propertyName) {
00153         $realInstance = $this->_loadRealInstance();
00154         return isset($realInstance->$propertyName);
00155     }
00156 
00157     /**
00158      * Magic unset call implementation.
00159      *
00160      * @param string $propertyName The name of the property to unset
00161      * @return void
00162      */
00163     public function __unset($propertyName) {
00164         $realInstance = $this->_loadRealInstance();
00165         unset($realInstance->$propertyName);
00166     }
00167 
00168     /**
00169      * Magic toString call implementation.
00170      *
00171      * @return void
00172      */
00173     public function __toString() {
00174         $realInstance = $this->_loadRealInstance();
00175         return $realInstance->__toString();
00176     }
00177 
00178     /**
00179      * Returns the current value of the storage array
00180      *
00181      * @return void
00182      */
00183     public function current() {
00184         $realInstance = $this->_loadRealInstance();
00185         return current($realInstance);
00186     }
00187 
00188     /**
00189      * Returns the current key storage array
00190      *
00191      * @return void
00192      */
00193     public function key() {
00194         $realInstance = $this->_loadRealInstance();
00195         return key($realInstance);
00196     }
00197 
00198     /**
00199      * Returns the next position of the storage array
00200      *
00201      * @return void
00202      */
00203     public function next() {
00204         $realInstance = $this->_loadRealInstance();
00205         next($realInstance);
00206     }
00207 
00208     /**
00209      * Resets the array pointer of the storage
00210      *
00211      * @return void
00212      */
00213     public function rewind() {
00214         $realInstance = $this->_loadRealInstance();
00215         reset($realInstance);
00216     }
00217 
00218     /**
00219      * Checks if the array pointer of the storage points to a valid position
00220      *
00221      * @return void
00222      */
00223     public function valid() {
00224         return $this->current() !== FALSE;
00225     }
00226 
00227 
00228 
00229 }
00230 ?>