Changeset 4470 for trunk/lib/Doctrine

Show
Ignore:
Timestamp:
06/05/08 20:01:58 (7 months ago)
Author:
romanb
Message:

Refactorings and initial commit/draft of new event handling / configuration / bootstrapping. Still need to remove all the static EntityManagerFactory::getManager lookup calls from the Production classes. The production classes need to have access to the EntityManager? of the Query that constructed the Parser. It should be injected into the Parser during construction.

Location:
trunk/lib/Doctrine
Files:
6 added
24 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Doctrine/AuditLog.php

    r4364 r4470  
    2020 */ 
    2121 
     22#namespace Doctrine::Behaviors::AuditLog; 
     23 
    2224/** 
    2325 * Doctrine_AuditLog 
     
    3032 * @version     $Revision$ 
    3133 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi> 
     34 * @todo Move to "Doctrine Behaviors" package. Separate download. 
    3235 */ 
    3336class Doctrine_AuditLog extends Doctrine_Record_Generator 
  • trunk/lib/Doctrine/Collection.php

    r4456 r4470  
    111111        if (is_string($entityBaseType)) { 
    112112            $this->_entityBaseType = $entityBaseType; 
    113             $mapper = Doctrine_EntityManager::getManager($entityBaseType)->getEntityPersister($entityBaseType); 
     113            $mapper = Doctrine_EntityManagerFactory::getManager($entityBaseType) 
     114                    ->getEntityPersister($entityBaseType); 
    114115        } 
    115116        $this->_mapper = $mapper; 
     
    211212    public function unserialize($serialized) 
    212213    { 
    213         $manager = Doctrine_EntityManager::getManager(); 
     214        $manager = Doctrine_EntityManagerFactory::getManager(); 
    214215        $connection = $manager->getConnection(); 
    215216         
  • trunk/lib/Doctrine/Compiler.php

    r3570 r4470  
    1919 * <http://www.phpdoctrine.org>. 
    2020 */ 
     21 
     22#namespace Doctrine::ORM::Tools; 
    2123 
    2224/** 
  • trunk/lib/Doctrine/Connection.php

    r4456 r4470  
    5656 * @author      Lukas Smith <smith@pooteeweet.org> (MDB2 library) 
    5757 * @author      Roman Borschel <roman@code-factory.org> 
    58  * @todo Split up into Doctrine::DBAL::Connection & Doctrine::ORM::EntityManager. 
    59  *       Doctrine::DBAL::Connection must have no dependencies on ORM components since 
    60  *       it sits one layer below. 
    61  *       Right now, this is the unification of these two classes. 
     58 * @todo 
     59 * 1) REPLICATION SUPPORT 
     60 * Replication support should be tackled at this layer (DBAL). 
     61 * There can be options that look like: 
     62 *       'slaves' => array( 
     63 *           'slave1' => array( 
     64 *                user, pass etc. 
     65 *           ), 
     66 *           'slave2' => array( 
     67 *                user, pass etc. 
     68 *           )), 
     69 *       'slaveConnectionResolver' => new MySlaveConnectionResolver(), 
     70 *       'masters' => array(...), 
     71 *       'masterConnectionResolver' => new MyMasterConnectionResolver() 
     72 *  
     73 * Doctrine::DBAL could ship with a simple standard resolver that uses a primitive 
     74 * round-robin approach to distribution. User can provide its own resolvers. 
    6275 */ 
    6376abstract class Doctrine_Connection implements Doctrine_Configurable, Countable 
     
    6679     * The PDO database handle.  
    6780     * 
    68      * @var PDO                  
     81     * @var PDO 
     82     * @todo Rename to $pdo.               
    6983     */ 
    7084    protected $dbh; 
    7185     
    7286    /** 
     87     * The Configuration. 
     88     * 
     89     * @var Configuration 
     90     */ 
     91    protected $_config; 
     92     
     93    /** 
     94     * The EventManager. 
     95     * 
     96     * @var EventManager 
     97     */ 
     98    protected $_eventManager; 
     99     
     100    /** 
    73101     * The attributes. 
    74102     * 
     
    78106     
    79107    /** 
    80      * $_name 
    81      * 
    82108     * Name of the connection 
    83109     * 
     
    129155     
    130156    /** 
    131      * 
    132      */ 
    133     protected $options = array(); 
     157     * The parameters used during creation of the Connection. 
     158     */ 
     159    protected $_params = array(); 
    134160     
    135161    /** 
     
    197223     * @param Doctrine_Manager $manager                 the manager object 
    198224     * @param PDO|Doctrine_Adapter_Interface $adapter   database driver 
    199      * @todo Remove the dependency on the Manager for DBAL/ORM separation. 
    200      */ 
    201     public function __construct($adapter, $user = null, $pass = null) 
    202     { 
    203         if (is_object($adapter)) { 
    204             if ( ! $adapter instanceof PDO) { 
    205                 throw new Doctrine_Connection_Exception( 
    206                         'First argument should be an instance of PDO or implement Doctrine_Adapter_Interface'); 
    207             } 
    208             $this->dbh = $adapter; 
     225     */ 
     226    public function __construct(array $params) 
     227    { 
     228        if (isset($params['pdo'])) { 
     229            $this->dbh = $pdo; 
    209230            $this->isConnected = true; 
    210         } else if (is_array($adapter)) { 
    211             $this->pendingAttributes[Doctrine::ATTR_DRIVER_NAME] = $adapter['scheme']; 
    212  
    213             $this->options['dsn']      = $adapter['dsn']; 
    214             $this->options['username'] = $adapter['user']; 
    215             $this->options['password'] = $adapter['pass']; 
    216  
    217             $this->options['other'] = array(); 
    218             if (isset($adapter['other'])) { 
    219                 $this->options['other'] = array(Doctrine::ATTR_PERSISTENT => $adapter['persistent']); 
    220             } 
    221         } 
     231        } 
     232        $this->_params = $params; 
     233    } 
     234     
     235    /** 
     236     * Sets the Configuration used by the Connection. 
     237     * 
     238     * @param Doctrine_Configuration $config 
     239     */ 
     240    public function setConfiguration(Doctrine_Configuration $config) 
     241    { 
     242        $this->_config = $config; 
     243    } 
     244     
     245    /** 
     246     * Gets the Configuration used by the Connection. 
     247     * 
     248     * @return Configuration 
     249     */ 
     250    public function getConfiguration() 
     251    { 
     252        if ( ! $this->_config) { 
     253            $this->_config = new Doctrine_Configuration(); 
     254        } 
     255        return $this->_config; 
     256    } 
     257     
     258    /** 
     259     * Sets the EventManager used by the Connection. 
     260     * 
     261     * @param Doctrine_EventManager $eventManager 
     262     */ 
     263    public function setEventManager(Doctrine_EventManager $eventManager) 
     264    { 
     265        $this->_eventManager = $eventManager; 
     266    } 
     267     
     268    /** 
     269     * Gets the EventManager used by the Connection. 
     270     * 
     271     * @return EventManager 
     272     */ 
     273    public function getEventManager() 
     274    { 
     275        if ( ! $this->_eventManager) { 
     276            $this->_eventManager = new Doctrine_EventManager(); 
     277        } 
     278        return $this->_eventManager; 
    222279    } 
    223280     
     
    318375        //$this->getListener()->postConnect($event); 
    319376        return true; 
     377    } 
     378     
     379    /** 
     380     * Constructs the PDO DSN for use in the PDO constructor. 
     381     * Concrete connection implementations override this implementation to 
     382     * create the proper DSN. 
     383     *  
     384     * @return string 
     385     * @todo make abstract, implement in subclasses. 
     386     */ 
     387    protected function _constructPdoDsn() 
     388    { 
     389         
    320390    } 
    321391     
     
    10751145    } 
    10761146     
    1077      
    1078      
    1079     /* 
    1080      * -----------   EntityManager methods    --------------- 
    1081      */ 
    1082      
    10831147    /* 
    10841148     * -----------   Mixed methods (need to figure out where they go)   --------------- 
  • trunk/lib/Doctrine/Connection/Common.php

    r4328 r4470  
    3030 * @version     $Revision$ 
    3131 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi> 
     32 * @TODO Remove this class and move the modifyLimitQuery implementation to Connection. 
    3233 */ 
    3334class Doctrine_Connection_Common extends Doctrine_Connection 
  • trunk/lib/Doctrine/Connection/Mock.php

    r4425 r4470  
    1919 * <http://www.phpdoctrine.org>. 
    2020 */ 
    21 Doctrine::autoload('Doctrine_Connection_Common'); 
     21 
     22#namespace Doctrine::DBAL::Connections; 
     23 
    2224/** 
    2325 * Doctrine_Connection_Mysql 
  • trunk/lib/Doctrine/Connection/Mysql.php

    r4463 r4470  
    1919 * <http://www.phpdoctrine.org>. 
    2020 */ 
    21 Doctrine::autoload('Doctrine_Connection_Common'); 
     21 
     22#namespace Doctrine::DBAL::Connections; 
     23 
    2224/** 
    2325 * Doctrine_Connection_Mysql 
     
    5153 
    5254        $this->supported = array( 
    53                           'sequences'            => 'emulated', 
    54                           'indexes'              => true, 
    55                           'affected_rows'        => true, 
    56                           'transactions'         => true, 
    57                           'savepoints'           => false, 
    58                           'summary_functions'    => true, 
    59                           'order_by_text'        => true, 
    60                           'current_id'           => 'emulated', 
    61                           'limit_queries'        => true, 
    62                           'LOBs'                 => true, 
    63                           'replace'              => true, 
    64                           'sub_selects'          => true, 
    65                           'auto_increment'       => true, 
    66                           'primary_key'          => true, 
    67                           'result_introspection' => true, 
    68                           'prepared_statements'  => 'emulated', 
    69                           'identifier_quoting'   => true, 
    70                           'pattern_escaping'     => true 
    71                           ); 
    72  
    73         $this->properties['string_quoting'] = array('start' => "'", 
    74                                                     'end' => "'", 
    75                                                     'escape' => '\\', 
    76                                                     'escape_pattern' => '\\'); 
    77  
    78         $this->properties['identifier_quoting'] = array('start' => '`', 
    79                                                         'end' => '`', 
    80                                                         'escape' => '`'); 
     55                'sequences'            => 'emulated', 
     56                'indexes'              => true, 
     57                'affected_rows'        => true, 
     58                'transactions'         => true, 
     59                'savepoints'           => false, 
     60                'summary_functions'    => true, 
     61                'order_by_text'        => true, 
     62                'current_id'           => 'emulated', 
     63                'limit_queries'        => true, 
     64                'LOBs'                 => true, 
     65                'replace'              => true, 
     66                'sub_selects'          => true, 
     67                'auto_increment'       => true, 
     68                'primary_key'          => true, 
     69                'result_introspection' => true, 
     70                'prepared_statements'  => 'emulated', 
     71                'identifier_quoting'   => true, 
     72                'pattern_escaping'     => true 
     73                ); 
     74 
     75        $this->properties['string_quoting'] = array( 
     76                'start' => "'", 
     77                'end' => "'", 
     78                'escape' => '\\', 
     79                'escape_pattern' => '\\'); 
     80 
     81        $this->properties['identifier_quoting'] = array( 
     82                'start' => '`', 
     83                'end' => '`', 
     84                'escape' => '`'); 
    8185 
    8286        $this->properties['sql_comments'] = array( 
    83                                             array('start' => '-- ', 'end' => "\n", 'escape' => false), 
    84                                             array('start' => '#', 'end' => "\n", 'escape' => false), 
    85                                             array('start' => '/*', 'end' => '*/', 'escape' => false), 
    86                                             ); 
     87                array('start' => '-- ', 'end' => "\n", 'escape' => false), 
     88                array('start' => '#', 'end' => "\n", 'escape' => false), 
     89                array('start' => '/*', 'end' => '*/', 'escape' => false), 
     90                ); 
    8791 
    8892        $this->properties['varchar_max_length'] = 255; 
     
    98102    public function setCharset($charset) 
    99103    { 
    100         $query = 'SET NAMES ' . $this->quote($charset); 
    101  
    102         $this->exec($query); 
     104        $this->exec('SET NAMES ' . $this->quote($charset)); 
    103105    } 
    104106 
  • trunk/lib/Doctrine/Connection/Pgsql.php

    r4422 r4470  
    1919 * <http://www.phpdoctrine.org>. 
    2020 */ 
    21 Doctrine::autoload("Doctrine_Connection_Common"); 
     21 
     22#namespace Doctrine::DBAL::Connections; 
     23 
    2224/** 
    23  * Doctrine_Connection_Pgsql 
     25 * PgsqlConnection 
    2426 * 
    2527 * @package     Doctrine 
  • trunk/lib/Doctrine/Connection/UnitOfWork.php

    r4456 r4470  
    9696     
    9797    /** 
    98      * The dbal connection used by the unit of work. 
    99      * 
    100      * @var Doctrine_Connection 
    101      * @todo Not needed in the future. Remove. 
    102      */ 
    103     protected $_conn; 
     98     * The calculator used to calculate the order in which changes to 
     99     * entities need to be written to the database. 
     100     * 
     101     * @var unknown_type 
     102     * @todo Implementation. Replace buildFlushTree(). 
     103     */ 
     104    protected $_commitOrderCalculator; 
    104105     
    105106    /** 
  • trunk/lib/Doctrine/Entity.php

    r4464 r4470  
    219219    { 
    220220        $this->_entityName = get_class($this); 
    221         $this->_em = Doctrine_EntityManager::getManager($this->_entityName); 
     221        $this->_em = Doctrine_EntityManagerFactory::getManager($this->_entityName); 
    222222        $this->_class = $this->_em->getClassMetadata($this->_entityName); 
    223223        $this->_oid = self::$_index++; 
     
    592592    public function serialize() 
    593593    { 
    594         $event = new Doctrine_Event($this, Doctrine_Event::RECORD_SERIALIZE); 
    595         $this->preSerialize($event); 
     594        //$event = new Doctrine_Event($this, Doctrine_Event::RECORD_SERIALIZE); 
     595        //$this->preSerialize($event); 
    596596 
    597597        $vars = get_object_vars($this); 
     
    630630        $str = serialize($vars); 
    631631 
    632         $this->postSerialize($event); 
     632        //$this->postSerialize($event); 
    633633 
    634634        return $str; 
     
    645645    public function unserialize($serialized) 
    646646    { 
    647         $event = new Doctrine_Event($this, Doctrine_Event::RECORD_UNSERIALIZE); 
    648  
    649         $this->preUnserialize($event); 
     647        //$event = new Doctrine_Event($this, Doctrine_Event::RECORD_UNSERIALIZE); 
     648        //$this->preUnserialize($event); 
    650649 
    651650        $this->_entityName = get_class($this); 
    652         $manager = Doctrine_EntityManager::getManager($this->_entityName); 
     651        $manager = Doctrine_EntityManagerFactory::getManager($this->_entityName); 
    653652        $connection = $manager->getConnection(); 
    654653 
     
    685684        $this->_extractIdentifier($this->exists()); 
    686685         
    687         $this->postUnserialize($event); 
     686        //$this->postUnserialize($event); 
    688687    } 
    689688 
  • trunk/lib/Doctrine/EntityManager.php

    r4466 r4470  
    2020 */ 
    2121 
    22 #namespace org::phpdoctrine::orm; 
     22#namespace Doctrine::ORM; 
     23 
     24#use Doctrine::Common::Configuration; 
     25#use Doctrine::Common::EventManager; 
     26#use Doctrine::Common::NullObject; 
     27#use Doctrine::DBAL::Connections::Connection; 
     28#use Doctrine::ORM::Exceptions::EntityManagerException; 
     29#use Doctrine::ORM::Internal::UnitOfWork; 
     30#use Doctrine::ORM::Mapping::ClassMetadata; 
     31 
    2332 
    2433/** 
     
    3443 * @todo package:orm 
    3544 */ 
    36 class Doctrine_EntityManager 
     45class Doctrine_EntityManager implements Doctrine_Configurable 
    3746{ 
    3847    /** 
     
    4352     */ 
    4453    private $_name; 
     54     
     55    /** 
     56     * The used Configuration. 
     57     * 
     58     * @var Configuration 
     59     */ 
     60    private $_configuration; 
     61     
     62     
     63    // -- configuration stuff to be removed. replaced by Configuration. 
     64    private $_nullObject; 
     65    /** 
     66     * The attributes. 
     67     * 
     68     * @var array 
     69     */ 
     70    private $_attributes = array( 
     71            'quoteIdentifier' => false, 
     72            'indexNameFormat' => '%s_idx', 
     73            'sequenceNameFormat' => '%s_seq', 
     74            'tableNameFormat' => '%s', 
     75            'resultCache' => null, 
     76            'resultCacheLifeSpan' => null, 
     77            'queryCache' => null, 
     78            'queryCacheLifeSpan' => null, 
     79            'metadataCache' => null, 
     80            'metadataCacheLifeSpan' => null 
     81    ); 
    4582     
    4683    /** 
     
    94131     
    95132    /** 
    96      * Map of all EntityManagers, keys are the names. 
    97      * 
    98      * @var array 
    99      */ 
    100     private static $_ems = array(); 
    101      
    102     /** 
    103      * EntityManager to Entity bindings. 
    104      * 
    105      * @var array 
    106      */ 
    107     private static $_emBindings = array(); 
    108      
    109     /** 
    110133     * The unit of work. 
    111134     * 
     
    141164                $this, new Doctrine_ClassMetadata_CodeDriver()); 
    142165        $this->_unitOfWork = new Doctrine_Connection_UnitOfWork($conn); 
    143         //$this->_eventManager = new Doctrine_EventManager(); 
    144          
    145         if ($name !== null) { 
    146             self::$_ems[$name] = $this; 
    147         } else { 
    148             self::$_ems[] = $this; 
    149         } 
    150     } 
    151      
    152     /** 
    153      * Gets the EntityManager that is responsible for the Entity. 
    154      * 
    155      * @param string $entityName 
    156      * @return EntityManager 
    157      * @throws Doctrine_EntityManager_Exception  If a suitable manager can not be found. 
    158      */ 
    159     public static function getManager($entityName = null) 
    160     { 
    161         if ( ! is_null($entityName) && isset(self::$_emBindings[$entityName])) { 
    162             $emName = self::$_emBindings[$entityName]; 
    163             if (isset(self::$_ems[$emName])) { 
    164                 return self::$_ems[$emName]; 
    165             } else { 
    166                 throw Doctrine_EntityManager_Exception::noManagerWithName($emName);    
     166        $this->_nullObject = Doctrine_Null::$INSTANCE; 
     167        $this->_initAttributes(); 
     168    } 
     169     
     170    private function _initAttributes() 
     171    { 
     172        // Change null default values to references to the Null object to allow 
     173        // fast isset() checks instead of array_key_exists(). 
     174        foreach ($this->_attributes as $key => $value) { 
     175            if ($value === null) { 
     176                $this->_attributes[$key] = $this->_nullObject; 
    167177            } 
    168         } else if (self::$_ems) { 
    169             return current(self::$_ems); 
    170         } else { 
    171             throw Doctrine_EntityManager_Exception::noEntityManagerAvailable();    
    172         } 
    173     } 
    174      
    175     /** 
    176      * Binds an Entity to a specific EntityManager. 
    177      * 
    178      * @param string $entityName 
    179      * @param string $emName 
    180      */ 
    181     public static function bindEntityToManager($entityName, $emName) 
    182     { 
    183         if (isset(self::$_emBindings[$entityName])) { 
    184             throw Doctrine_EntityManager_Exception::entityAlreadyBound($entityName); 
    185         } 
    186         self::$_emBindings[$entityName] = $emName; 
    187     } 
    188      
    189     /** 
    190      * Clears all bindings between Entities and EntityManagers. 
    191      */ 
    192     public static function unbindAllManagers() 
    193     { 
    194         self::$_emBindings = array(); 
    195     } 
    196      
    197     /** 
    198      * Releases all EntityManagers. 
    199      * 
    200      */ 
    201     public static function releaseAllManagers() 
    202     { 
    203         self::$_ems = array(); 
     178        } 
    204179    } 
    205180     
     
    573548        return $this->_eventManager; 
    574549    } 
     550     
     551    /** 
     552     * Sets the EventManager used by the EntityManager. 
     553     * 
     554     * @param Doctrine_EventManager $eventManager 
     555     */ 
     556    public function setEventManager(Doctrine_EventManager $eventManager)