From ad2443057e2c40f4dd38d9274cc19771c27e73f0 Mon Sep 17 00:00:00 2001 From: romanb Date: Thu, 5 Jun 2008 19:01:58 +0000 Subject: [PATCH] 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. --- lib/Doctrine.php | 5 +- lib/Doctrine/AuditLog.php | 5 +- lib/Doctrine/Collection.php | 5 +- lib/Doctrine/Compiler.php | 2 + lib/Doctrine/Configuration.php | 77 ++++ lib/Doctrine/Connection.php | 132 +++++-- lib/Doctrine/Connection/Common.php | 3 +- lib/Doctrine/Connection/Mock.php | 4 +- lib/Doctrine/Connection/Mysql.php | 70 ++-- lib/Doctrine/Connection/Pgsql.php | 6 +- lib/Doctrine/Connection/UnitOfWork.php | 9 +- lib/Doctrine/ConnectionFactory.php | 62 ++++ lib/Doctrine/ConnectionFactory/Exception.php | 39 ++ lib/Doctrine/Entity.php | 17 +- lib/Doctrine/EntityManager.php | 166 +++++---- lib/Doctrine/EntityManager/Exception.php | 9 +- lib/Doctrine/EntityManagerFactory.php | 180 ++++++++++ lib/Doctrine/Event.php | 334 +++--------------- lib/Doctrine/EventListener.php | 2 +- lib/Doctrine/EventManager.php | 43 +++ lib/Doctrine/Manager.php | 2 - lib/Doctrine/Query/Parser.php | 2 +- lib/Doctrine/Query/Production.php | 2 +- .../Query/Production/PathExpression.php | 2 +- .../PathExpressionEndingWithAsterisk.php | 2 +- .../Production/RangeVariableDeclaration.php | 4 +- .../Query/Production/SelectExpression.php | 2 +- .../Query/Production/UpdateClause.php | 2 +- .../Query/Production/VariableDeclaration.php | 4 +- lib/Doctrine/Relation.php | 4 +- tests/Orm/AllTests.php | 2 + tests/Orm/EntityManagerFactoryTest.php | 41 +++ tests/Orm/EntityManagerTest.php | 49 --- tests/Orm/Hydration/BasicHydrationTest.php | 5 +- tests/Orm/Query/DeleteSqlGenerationTest.php | 2 +- tests/Orm/Query/DqlGenerationTest.php | 2 +- tests/Orm/Query/IdentifierRecognitionTest.php | 8 +- tests/Orm/Query/LanguageRecognitionTest.php | 4 +- tests/Orm/Query/SelectSqlGenerationTest.php | 2 +- tests/Orm/Query/UpdateSqlGenerationTest.php | 2 +- tests/lib/Doctrine_OrmTestCase.php | 4 +- tests/lib/Doctrine_OrmTestSuite.php | 18 +- 42 files changed, 795 insertions(+), 540 deletions(-) create mode 100644 lib/Doctrine/Configuration.php create mode 100644 lib/Doctrine/ConnectionFactory.php create mode 100644 lib/Doctrine/ConnectionFactory/Exception.php create mode 100644 lib/Doctrine/EntityManagerFactory.php create mode 100644 lib/Doctrine/EventManager.php create mode 100644 tests/Orm/EntityManagerFactoryTest.php delete mode 100644 tests/Orm/EntityManagerTest.php diff --git a/lib/Doctrine.php b/lib/Doctrine.php index daeb08253..515232c0f 100644 --- a/lib/Doctrine.php +++ b/lib/Doctrine.php @@ -30,6 +30,8 @@ * @link www.phpdoctrine.org * @since 1.0 * @version $Revision$ + * @todo Remove all the constants, attributes to the new attribute system, + * All methods to separate classes. */ final class Doctrine { @@ -183,7 +185,6 @@ final class Doctrine const ATTR_EXPORT = 140; // manager/session attribute const ATTR_DECIMAL_PLACES = 141; // manager/session attribute const ATTR_PORTABILITY = 106; // manager/session attribute - const ATTR_VALIDATE = 107; // manager/session attribute const ATTR_COLL_KEY = 108; // class attribute const ATTR_QUERY_LIMIT = 109; // manager/session attribute const ATTR_DEFAULT_TABLE_TYPE = 112; // manager/session attribute @@ -197,9 +198,7 @@ final class Doctrine const ATTR_NAME_PREFIX = 121; // ?? const ATTR_CREATE_TABLES = 122; // manager/session attribute const ATTR_COLL_LIMIT = 123; // manager/session attribute - const ATTR_CACHE = 150; // deprecated const ATTR_RESULT_CACHE = 150; // manager/session attribute - const ATTR_CACHE_LIFESPAN = 151; // deprecated const ATTR_RESULT_CACHE_LIFESPAN = 151; // manager/session attribute const ATTR_LOAD_REFERENCES = 153; // class attribute const ATTR_RECORD_LISTENER = 154; diff --git a/lib/Doctrine/AuditLog.php b/lib/Doctrine/AuditLog.php index 111ccc80a..c32a07beb 100644 --- a/lib/Doctrine/AuditLog.php +++ b/lib/Doctrine/AuditLog.php @@ -18,6 +18,8 @@ * and is licensed under the LGPL. For more information, see * . */ + +#namespace Doctrine::Behaviors::AuditLog; /** * Doctrine_AuditLog @@ -28,7 +30,8 @@ * @link www.phpdoctrine.org * @since 1.0 * @version $Revision$ - * @author Konsta Vesterinen + * @author Konsta Vesterinen + * @todo Move to "Doctrine Behaviors" package. Separate download. */ class Doctrine_AuditLog extends Doctrine_Record_Generator { diff --git a/lib/Doctrine/Collection.php b/lib/Doctrine/Collection.php index 88fa0c6a7..dfb5a0ca1 100644 --- a/lib/Doctrine/Collection.php +++ b/lib/Doctrine/Collection.php @@ -110,7 +110,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator { if (is_string($entityBaseType)) { $this->_entityBaseType = $entityBaseType; - $mapper = Doctrine_EntityManager::getManager($entityBaseType)->getEntityPersister($entityBaseType); + $mapper = Doctrine_EntityManagerFactory::getManager($entityBaseType) + ->getEntityPersister($entityBaseType); } $this->_mapper = $mapper; @@ -210,7 +211,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator */ public function unserialize($serialized) { - $manager = Doctrine_EntityManager::getManager(); + $manager = Doctrine_EntityManagerFactory::getManager(); $connection = $manager->getConnection(); $array = unserialize($serialized); diff --git a/lib/Doctrine/Compiler.php b/lib/Doctrine/Compiler.php index 45260fd9e..f1ec7f5cd 100644 --- a/lib/Doctrine/Compiler.php +++ b/lib/Doctrine/Compiler.php @@ -18,6 +18,8 @@ * and is licensed under the LGPL. For more information, see * . */ + +#namespace Doctrine::ORM::Tools; /** * Doctrine_Compiler diff --git a/lib/Doctrine/Configuration.php b/lib/Doctrine/Configuration.php new file mode 100644 index 000000000..ec881ace6 --- /dev/null +++ b/lib/Doctrine/Configuration.php @@ -0,0 +1,77 @@ + false, + 'indexNameFormat' => '%s_idx', + 'sequenceNameFormat' => '%s_seq', + 'tableNameFormat' => '%s', + 'resultCache' => null, + 'resultCacheLifeSpan' => null, + 'queryCache' => null, + 'queryCacheLifeSpan' => null, + 'metadataCache' => null, + 'metadataCacheLifeSpan' => null + ); + + public function __construct() + { + $this->_nullObject = Doctrine_Null::$INSTANCE; + $this->_initAttributes(); + } + + private function _initAttributes() + { + // Change null default values to references to the Null object to allow + // fast isset() checks instead of array_key_exists(). + foreach ($this->_attributes as $key => $value) { + if ($value === null) { + $this->_attributes[$key] = $this->_nullObject; + } + } + } + + public function get($name) + { + if ( ! $this->hasAttribute($name)) { + throw Doctrine_Configuration_Exception::unknownAttribute($name); + } + if ($this->_attributes[$name] === $this->_nullObject) { + return null; + } + return $this->_attributes[$name]; + } + + public function set($name, $value) + { + if ( ! $this->hasAttribute($name)) { + throw Doctrine_Configuration_Exception::unknownAttribute($name); + } + // TODO: do some value checking depending on the attribute + $this->_attributes[$name] = $value; + } + + public function has($name) + { + return isset($this->_attributes[$name]); + } +} \ No newline at end of file diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index 6bbce578e..7342f381b 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -55,20 +55,48 @@ * @author Konsta Vesterinen * @author Lukas Smith (MDB2 library) * @author Roman Borschel - * @todo Split up into Doctrine::DBAL::Connection & Doctrine::ORM::EntityManager. - * Doctrine::DBAL::Connection must have no dependencies on ORM components since - * it sits one layer below. - * Right now, this is the unification of these two classes. + * @todo + * 1) REPLICATION SUPPORT + * Replication support should be tackled at this layer (DBAL). + * There can be options that look like: + * 'slaves' => array( + * 'slave1' => array( + * user, pass etc. + * ), + * 'slave2' => array( + * user, pass etc. + * )), + * 'slaveConnectionResolver' => new MySlaveConnectionResolver(), + * 'masters' => array(...), + * 'masterConnectionResolver' => new MyMasterConnectionResolver() + * + * Doctrine::DBAL could ship with a simple standard resolver that uses a primitive + * round-robin approach to distribution. User can provide its own resolvers. */ abstract class Doctrine_Connection implements Doctrine_Configurable, Countable { /** * The PDO database handle. * - * @var PDO + * @var PDO + * @todo Rename to $pdo. */ protected $dbh; + /** + * The Configuration. + * + * @var Configuration + */ + protected $_config; + + /** + * The EventManager. + * + * @var EventManager + */ + protected $_eventManager; + /** * The attributes. * @@ -77,8 +105,6 @@ abstract class Doctrine_Connection implements Doctrine_Configurable, Countable protected $_attributes = array(); /** - * $_name - * * Name of the connection * * @var string $_name @@ -128,9 +154,9 @@ abstract class Doctrine_Connection implements Doctrine_Configurable, Countable protected $serverInfo = array(); /** - * + * The parameters used during creation of the Connection. */ - protected $options = array(); + protected $_params = array(); /** * List of all available drivers. @@ -196,29 +222,60 @@ abstract class Doctrine_Connection implements Doctrine_Configurable, Countable * * @param Doctrine_Manager $manager the manager object * @param PDO|Doctrine_Adapter_Interface $adapter database driver - * @todo Remove the dependency on the Manager for DBAL/ORM separation. */ - public function __construct($adapter, $user = null, $pass = null) + public function __construct(array $params) { - if (is_object($adapter)) { - if ( ! $adapter instanceof PDO) { - throw new Doctrine_Connection_Exception( - 'First argument should be an instance of PDO or implement Doctrine_Adapter_Interface'); - } - $this->dbh = $adapter; + if (isset($params['pdo'])) { + $this->dbh = $pdo; $this->isConnected = true; - } else if (is_array($adapter)) { - $this->pendingAttributes[Doctrine::ATTR_DRIVER_NAME] = $adapter['scheme']; - - $this->options['dsn'] = $adapter['dsn']; - $this->options['username'] = $adapter['user']; - $this->options['password'] = $adapter['pass']; - - $this->options['other'] = array(); - if (isset($adapter['other'])) { - $this->options['other'] = array(Doctrine::ATTR_PERSISTENT => $adapter['persistent']); - } } + $this->_params = $params; + } + + /** + * Sets the Configuration used by the Connection. + * + * @param Doctrine_Configuration $config + */ + public function setConfiguration(Doctrine_Configuration $config) + { + $this->_config = $config; + } + + /** + * Gets the Configuration used by the Connection. + * + * @return Configuration + */ + public function getConfiguration() + { + if ( ! $this->_config) { + $this->_config = new Doctrine_Configuration(); + } + return $this->_config; + } + + /** + * Sets the EventManager used by the Connection. + * + * @param Doctrine_EventManager $eventManager + */ + public function setEventManager(Doctrine_EventManager $eventManager) + { + $this->_eventManager = $eventManager; + } + + /** + * Gets the EventManager used by the Connection. + * + * @return EventManager + */ + public function getEventManager() + { + if ( ! $this->_eventManager) { + $this->_eventManager = new Doctrine_EventManager(); + } + return $this->_eventManager; } /** @@ -319,6 +376,19 @@ abstract class Doctrine_Connection implements Doctrine_Configurable, Countable return true; } + /** + * Constructs the PDO DSN for use in the PDO constructor. + * Concrete connection implementations override this implementation to + * create the proper DSN. + * + * @return string + * @todo make abstract, implement in subclasses. + */ + protected function _constructPdoDsn() + { + + } + /** * @todo Remove. Breaks encapsulation. */ @@ -1074,12 +1144,6 @@ abstract class Doctrine_Connection implements Doctrine_Configurable, Countable return Doctrine_Lib::getConnectionAsString($this); } - - - /* - * ----------- EntityManager methods --------------- - */ - /* * ----------- Mixed methods (need to figure out where they go) --------------- */ diff --git a/lib/Doctrine/Connection/Common.php b/lib/Doctrine/Connection/Common.php index 98364eef2..876d9b306 100644 --- a/lib/Doctrine/Connection/Common.php +++ b/lib/Doctrine/Connection/Common.php @@ -28,7 +28,8 @@ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @since 1.0 * @version $Revision$ - * @author Konsta Vesterinen + * @author Konsta Vesterinen + * @TODO Remove this class and move the modifyLimitQuery implementation to Connection. */ class Doctrine_Connection_Common extends Doctrine_Connection { diff --git a/lib/Doctrine/Connection/Mock.php b/lib/Doctrine/Connection/Mock.php index 546e7a7e1..81f353c7f 100644 --- a/lib/Doctrine/Connection/Mock.php +++ b/lib/Doctrine/Connection/Mock.php @@ -18,7 +18,9 @@ * and is licensed under the LGPL. For more information, see * . */ -Doctrine::autoload('Doctrine_Connection_Common'); + +#namespace Doctrine::DBAL::Connections; + /** * Doctrine_Connection_Mysql * diff --git a/lib/Doctrine/Connection/Mysql.php b/lib/Doctrine/Connection/Mysql.php index 131db1633..a3f4159b2 100644 --- a/lib/Doctrine/Connection/Mysql.php +++ b/lib/Doctrine/Connection/Mysql.php @@ -18,7 +18,9 @@ * and is licensed under the LGPL. For more information, see * . */ -Doctrine::autoload('Doctrine_Connection_Common'); + +#namespace Doctrine::DBAL::Connections; + /** * Doctrine_Connection_Mysql * @@ -50,40 +52,42 @@ class Doctrine_Connection_Mysql extends Doctrine_Connection_Common $this->setAttribute(Doctrine::ATTR_DEFAULT_TABLE_TYPE, 'INNODB'); $this->supported = array( - 'sequences' => 'emulated', - 'indexes' => true, - 'affected_rows' => true, - 'transactions' => true, - 'savepoints' => false, - 'summary_functions' => true, - 'order_by_text' => true, - 'current_id' => 'emulated', - 'limit_queries' => true, - 'LOBs' => true, - 'replace' => true, - 'sub_selects' => true, - 'auto_increment' => true, - 'primary_key' => true, - 'result_introspection' => true, - 'prepared_statements' => 'emulated', - 'identifier_quoting' => true, - 'pattern_escaping' => true - ); + 'sequences' => 'emulated', + 'indexes' => true, + 'affected_rows' => true, + 'transactions' => true, + 'savepoints' => false, + 'summary_functions' => true, + 'order_by_text' => true, + 'current_id' => 'emulated', + 'limit_queries' => true, + 'LOBs' => true, + 'replace' => true, + 'sub_selects' => true, + 'auto_increment' => true, + 'primary_key' => true, + 'result_introspection' => true, + 'prepared_statements' => 'emulated', + 'identifier_quoting' => true, + 'pattern_escaping' => true + ); - $this->properties['string_quoting'] = array('start' => "'", - 'end' => "'", - 'escape' => '\\', - 'escape_pattern' => '\\'); + $this->properties['string_quoting'] = array( + 'start' => "'", + 'end' => "'", + 'escape' => '\\', + 'escape_pattern' => '\\'); - $this->properties['identifier_quoting'] = array('start' => '`', - 'end' => '`', - 'escape' => '`'); + $this->properties['identifier_quoting'] = array( + 'start' => '`', + 'end' => '`', + 'escape' => '`'); $this->properties['sql_comments'] = array( - array('start' => '-- ', 'end' => "\n", 'escape' => false), - array('start' => '#', 'end' => "\n", 'escape' => false), - array('start' => '/*', 'end' => '*/', 'escape' => false), - ); + array('start' => '-- ', 'end' => "\n", 'escape' => false), + array('start' => '#', 'end' => "\n", 'escape' => false), + array('start' => '/*', 'end' => '*/', 'escape' => false), + ); $this->properties['varchar_max_length'] = 255; @@ -97,9 +101,7 @@ class Doctrine_Connection_Mysql extends Doctrine_Connection_Common */ public function setCharset($charset) { - $query = 'SET NAMES ' . $this->quote($charset); - - $this->exec($query); + $this->exec('SET NAMES ' . $this->quote($charset)); } /** diff --git a/lib/Doctrine/Connection/Pgsql.php b/lib/Doctrine/Connection/Pgsql.php index 0b969042f..5654196fa 100644 --- a/lib/Doctrine/Connection/Pgsql.php +++ b/lib/Doctrine/Connection/Pgsql.php @@ -18,9 +18,11 @@ * and is licensed under the LGPL. For more information, see * . */ -Doctrine::autoload("Doctrine_Connection_Common"); + +#namespace Doctrine::DBAL::Connections; + /** - * Doctrine_Connection_Pgsql + * PgsqlConnection * * @package Doctrine * @subpackage Connection diff --git a/lib/Doctrine/Connection/UnitOfWork.php b/lib/Doctrine/Connection/UnitOfWork.php index 7771c7e55..9d561942e 100644 --- a/lib/Doctrine/Connection/UnitOfWork.php +++ b/lib/Doctrine/Connection/UnitOfWork.php @@ -95,12 +95,13 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module protected $_em; /** - * The dbal connection used by the unit of work. + * The calculator used to calculate the order in which changes to + * entities need to be written to the database. * - * @var Doctrine_Connection - * @todo Not needed in the future. Remove. + * @var unknown_type + * @todo Implementation. Replace buildFlushTree(). */ - protected $_conn; + protected $_commitOrderCalculator; /** * Commits the unit of work, executing all operations that have been postponed diff --git a/lib/Doctrine/ConnectionFactory.php b/lib/Doctrine/ConnectionFactory.php new file mode 100644 index 000000000..e2f296671 --- /dev/null +++ b/lib/Doctrine/ConnectionFactory.php @@ -0,0 +1,62 @@ + 'Doctrine_Connection_Mysql', + 'sqlite' => 'Doctrine_Connection_Sqlite', + 'pgsql' => 'Doctrine_Connection_Pgsql', + 'oci' => 'Doctrine_Connection_Oracle', + 'oci8' => 'Doctrine_Connection_Oracle', + 'oracle' => 'Doctrine_Connection_Oracle', + 'mssql' => 'Doctrine_Connection_Mssql', + 'dblib' => 'Doctrine_Connection_Mssql', + 'firebird' => 'Doctrine_Connection_Firebird', + 'informix' => 'Doctrine_Connection_Informix', + 'mock' => 'Doctrine_Connection_Mock'); + + public function __construct() + { + + } + + public function createConnection(array $params) + { + $this->_checkParams($params); + $className = $this->_drivers[$params['driver']]; + return new $className($params); + } + + private function _checkParams(array $params) + { + // check existance of mandatory parameters + + // driver + if ( ! isset($params['driver'])) { + throw Doctrine_ConnectionFactory_Exception::driverRequired(); + } + // user + if ( ! isset($params['user'])) { + throw Doctrine_ConnectionFactory_Exception::userRequired(); + } + // password + if ( ! isset($params['password'])) { + throw Doctrine_ConnectionFactory_Exception::passwordRequired(); + } + + // check validity of parameters + + // driver + if ( ! isset($this->_drivers[$params['driver']])) { + throw Doctrine_ConnectionFactory_Exception::unknownDriver($driverName); + } + // existing pdo object + if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) { + throw Doctrine_ConnectionFactory_Exception::invalidPDOInstance(); + } + } +} + +?> \ No newline at end of file diff --git a/lib/Doctrine/ConnectionFactory/Exception.php b/lib/Doctrine/ConnectionFactory/Exception.php new file mode 100644 index 000000000..2e6444099 --- /dev/null +++ b/lib/Doctrine/ConnectionFactory/Exception.php @@ -0,0 +1,39 @@ +. + */ + +/** + * Doctrine_ConnectionFactory_Exception + * + * @package Doctrine + * @subpackage Compiler + * @author Roman Borschel + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.org + * @since 2.0 + * @version $Revision$ + */ +class Doctrine_ConnectionFactory_Exception extends Doctrine_Exception +{ + public static function userRequired() + { + return new self("The 'user' option is mandatory."); + } +} \ No newline at end of file diff --git a/lib/Doctrine/Entity.php b/lib/Doctrine/Entity.php index d4f5ce0a3..68df602e8 100644 --- a/lib/Doctrine/Entity.php +++ b/lib/Doctrine/Entity.php @@ -218,7 +218,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite public function __construct($isNewEntry = true, array $data = array()) { $this->_entityName = get_class($this); - $this->_em = Doctrine_EntityManager::getManager($this->_entityName); + $this->_em = Doctrine_EntityManagerFactory::getManager($this->_entityName); $this->_class = $this->_em->getClassMetadata($this->_entityName); $this->_oid = self::$_index++; @@ -591,8 +591,8 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite */ public function serialize() { - $event = new Doctrine_Event($this, Doctrine_Event::RECORD_SERIALIZE); - $this->preSerialize($event); + //$event = new Doctrine_Event($this, Doctrine_Event::RECORD_SERIALIZE); + //$this->preSerialize($event); $vars = get_object_vars($this); @@ -629,7 +629,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite $str = serialize($vars); - $this->postSerialize($event); + //$this->postSerialize($event); return $str; } @@ -644,12 +644,11 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite */ public function unserialize($serialized) { - $event = new Doctrine_Event($this, Doctrine_Event::RECORD_UNSERIALIZE); - - $this->preUnserialize($event); + //$event = new Doctrine_Event($this, Doctrine_Event::RECORD_UNSERIALIZE); + //$this->preUnserialize($event); $this->_entityName = get_class($this); - $manager = Doctrine_EntityManager::getManager($this->_entityName); + $manager = Doctrine_EntityManagerFactory::getManager($this->_entityName); $connection = $manager->getConnection(); $this->_oid = self::$_index; @@ -684,7 +683,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite $this->cleanData($this->_data); $this->_extractIdentifier($this->exists()); - $this->postUnserialize($event); + //$this->postUnserialize($event); } /** diff --git a/lib/Doctrine/EntityManager.php b/lib/Doctrine/EntityManager.php index 793f8c0aa..180ec4aeb 100644 --- a/lib/Doctrine/EntityManager.php +++ b/lib/Doctrine/EntityManager.php @@ -19,7 +19,16 @@ * . */ -#namespace org::phpdoctrine::orm; +#namespace Doctrine::ORM; + +#use Doctrine::Common::Configuration; +#use Doctrine::Common::EventManager; +#use Doctrine::Common::NullObject; +#use Doctrine::DBAL::Connections::Connection; +#use Doctrine::ORM::Exceptions::EntityManagerException; +#use Doctrine::ORM::Internal::UnitOfWork; +#use Doctrine::ORM::Mapping::ClassMetadata; + /** * The EntityManager is a central access point to ORM functionality. @@ -33,7 +42,7 @@ * @author Roman Borschel * @todo package:orm */ -class Doctrine_EntityManager +class Doctrine_EntityManager implements Doctrine_Configurable { /** * The unique name of the EntityManager. The name is used to bind entity classes @@ -43,6 +52,34 @@ class Doctrine_EntityManager */ private $_name; + /** + * The used Configuration. + * + * @var Configuration + */ + private $_configuration; + + + // -- configuration stuff to be removed. replaced by Configuration. + private $_nullObject; + /** + * The attributes. + * + * @var array + */ + private $_attributes = array( + 'quoteIdentifier' => false, + 'indexNameFormat' => '%s_idx', + 'sequenceNameFormat' => '%s_seq', + 'tableNameFormat' => '%s', + 'resultCache' => null, + 'resultCacheLifeSpan' => null, + 'queryCache' => null, + 'queryCacheLifeSpan' => null, + 'metadataCache' => null, + 'metadataCacheLifeSpan' => null + ); + /** * The database connection used by the EntityManager. * @@ -92,20 +129,6 @@ class Doctrine_EntityManager */ private $_flushMode = 'commit'; - /** - * Map of all EntityManagers, keys are the names. - * - * @var array - */ - private static $_ems = array(); - - /** - * EntityManager to Entity bindings. - * - * @var array - */ - private static $_emBindings = array(); - /** * The unit of work. * @@ -140,69 +163,21 @@ class Doctrine_EntityManager $this->_metadataFactory = new Doctrine_ClassMetadata_Factory( $this, new Doctrine_ClassMetadata_CodeDriver()); $this->_unitOfWork = new Doctrine_Connection_UnitOfWork($conn); - //$this->_eventManager = new Doctrine_EventManager(); - - if ($name !== null) { - self::$_ems[$name] = $this; - } else { - self::$_ems[] = $this; - } + $this->_nullObject = Doctrine_Null::$INSTANCE; + $this->_initAttributes(); } - /** - * Gets the EntityManager that is responsible for the Entity. - * - * @param string $entityName - * @return EntityManager - * @throws Doctrine_EntityManager_Exception If a suitable manager can not be found. - */ - public static function getManager($entityName = null) + private function _initAttributes() { - if ( ! is_null($entityName) && isset(self::$_emBindings[$entityName])) { - $emName = self::$_emBindings[$entityName]; - if (isset(self::$_ems[$emName])) { - return self::$_ems[$emName]; - } else { - throw Doctrine_EntityManager_Exception::noManagerWithName($emName); + // Change null default values to references to the Null object to allow + // fast isset() checks instead of array_key_exists(). + foreach ($this->_attributes as $key => $value) { + if ($value === null) { + $this->_attributes[$key] = $this->_nullObject; } - } else if (self::$_ems) { - return current(self::$_ems); - } else { - throw Doctrine_EntityManager_Exception::noEntityManagerAvailable(); } } - /** - * Binds an Entity to a specific EntityManager. - * - * @param string $entityName - * @param string $emName - */ - public static function bindEntityToManager($entityName, $emName) - { - if (isset(self::$_emBindings[$entityName])) { - throw Doctrine_EntityManager_Exception::entityAlreadyBound($entityName); - } - self::$_emBindings[$entityName] = $emName; - } - - /** - * Clears all bindings between Entities and EntityManagers. - */ - public static function unbindAllManagers() - { - self::$_emBindings = array(); - } - - /** - * Releases all EntityManagers. - * - */ - public static function releaseAllManagers() - { - self::$_ems = array(); - } - /** * Gets the database connection object used by the EntityManager. * @@ -572,6 +547,53 @@ class Doctrine_EntityManager { return $this->_eventManager; } + + /** + * Sets the EventManager used by the EntityManager. + * + * @param Doctrine_EventManager $eventManager + */ + public function setEventManager(Doctrine_EventManager $eventManager) + { + $this->_eventManager = $eventManager; + } + + /** + * Sets the Configuration used by the EntityManager. + * + * @param Doctrine_Configuration $config + */ + public function setConfiguration(Doctrine_Configuration $config) + { + $this->_configuration = $config; + } + + /* Configurable implementation */ + + public function hasAttribute($name) + { + return isset($this->_attributes[$name]); + } + + public function getAttribute($name) + { + if ( ! $this->hasAttribute($name)) { + throw Doctrine_EntityManager_Exception::unknownAttribute($name); + } + if ($this->_attributes[$name] === $this->_nullObject) { + return null; + } + return $this->_attributes[$name]; + } + + public function setAttribute($name, $value) + { + if ( ! $this->hasAttribute($name)) { + throw Doctrine_EntityManager_Exception::unknownAttribute($name); + } + // TODO: do some value checking depending on the attribute + $this->_attributes[$name] = $value; + } } ?> \ No newline at end of file diff --git a/lib/Doctrine/EntityManager/Exception.php b/lib/Doctrine/EntityManager/Exception.php index 705a8dcbe..6e774b714 100644 --- a/lib/Doctrine/EntityManager/Exception.php +++ b/lib/Doctrine/EntityManager/Exception.php @@ -19,13 +19,11 @@ * . */ -#namespace Doctrine::ORM; +#namespace Doctrine::ORM::Exceptions; /** * Doctrine_EntityManager_Exception * - * @package Doctrine - * @subpackage Entity * @author Konsta Vesterinen * @author Roman Borschel * @license http://www.opensource.org/licenses/lgpl-license.php LGPL @@ -54,4 +52,9 @@ class Doctrine_EntityManager_Exception extends Doctrine_Exception { return new self("EntityManager named '$emName' not found."); } + + public static function unknownAttribute($name) + { + return new self("Unknown EntityManager attribute '$name'."); + } } \ No newline at end of file diff --git a/lib/Doctrine/EntityManagerFactory.php b/lib/Doctrine/EntityManagerFactory.php new file mode 100644 index 000000000..698f90289 --- /dev/null +++ b/lib/Doctrine/EntityManagerFactory.php @@ -0,0 +1,180 @@ +_connFactory = new Doctrine_ConnectionFactory(); + } + + public function setConfiguration(Doctrine_Configuration $config) + { + $this->_config = $config; + } + + public function setEventManager(Doctrine_EventManager $eventManager) + { + $this->_eventManager = $eventManager; + } + + public function createEntityManager($connParams, $name = null) + { + if ( ! $this->_config) { + $this->_config = new Doctrine_Configuration(); + } + if ( ! $this->_eventManager) { + $this->_eventManager = new Doctrine_EventManager(); + } + + $conn = $this->_connFactory->createConnection($connParams); + $conn->setEventManager($this->_eventManager); + $conn->setConfiguration($this->_config); + + $em = new Doctrine_EntityManager($conn); + $em->setEventManager($this->_eventManager); + $em->setConfiguration($this->_config); + + if ($name !== null) { + self::$_ems[$name] = $em; + } else { + self::$_ems[] = $em; + } + + return $em; + } + + /** + * Gets the EntityManager that is responsible for the Entity. + * Static method, so that ActiveEntities can look up the right EntityManager + * without having a reference to the factory at hand. + * + * @param string $entityName + * @return EntityManager + * @throws Doctrine_EntityManager_Exception If a suitable manager can not be found. + */ + public static function getManager($entityName = null) + { + if ( ! is_null($entityName) && isset(self::$_emBindings[$entityName])) { + $emName = self::$_emBindings[$entityName]; + if (isset(self::$_ems[$emName])) { + return self::$_ems[$emName]; + } else { + throw Doctrine_EntityManagerFactory_Exception::noManagerWithName($emName); + } + } else if (self::$_ems) { + return current(self::$_ems); + } else { + throw Doctrine_EntityManagerFactory_Exception::noEntityManagerAvailable(); + } + } + + /** + * Gets the EntityManager that is responsible for the Entity. + * + * @param unknown_type $entityName + * @return unknown + */ + public function getEntityManager($entityName = null) + { + return self::getManager($entityName); + } + + /** + * Binds an Entity to a specific EntityManager. + * + * @param string $entityName + * @param string $emName + */ + public function bindEntityToManager($entityName, $emName) + { + if (isset(self::$_emBindings[$entityName])) { + throw Doctrine_EntityManagerFactory_Exception::entityAlreadyBound($entityName); + } + self::$_emBindings[$entityName] = $emName; + } + + /** + * Clears all bindings between Entities and EntityManagers. + */ + public function unbindAllManagers() + { + self::$_emBindings = array(); + } + + /** + * Releases all EntityManagers. + * + */ + public function releaseAllManagers() + { + self::unbindAllManagers(); + self::$_ems = array(); + } + + public function releaseAllBindings() + { + self::$_emBindings = array(); + } + + public function releaseEntityManager($name) + { + if (isset(self::$_ems[$name])) { + unset(self::$_ems[$name]); + return true; + } + return false; + } + +} + +?> \ No newline at end of file diff --git a/lib/Doctrine/Event.php b/lib/Doctrine/Event.php index 49f17f13e..21c4cd066 100644 --- a/lib/Doctrine/Event.php +++ b/lib/Doctrine/Event.php @@ -32,301 +32,47 @@ * @since 1.0 * @version $Revision$ */ -class Doctrine_Event +class Doctrine_Event { /* Event callback constants */ const preDelete = 'preDelete'; const postDelete = 'postDelete'; - - - - /* - const CONN_QUERY = 1; - const CONN_EXEC = 2; - const CONN_PREPARE = 3; - const CONN_CONNECT = 4; - const CONN_CLOSE = 5; - const CONN_ERROR = 6; - - const STMT_EXECUTE = 10; - const STMT_FETCH = 11; - const STMT_FETCHALL = 12; - - const TX_BEGIN = 31; - const TX_COMMIT = 32; - const TX_ROLLBACK = 33; - const SAVEPOINT_CREATE = 34; - const SAVEPOINT_ROLLBACK = 35; - const SAVEPOINT_COMMIT = 36; - - const HYDRATE = 40; - - const RECORD_DELETE = 21; - const RECORD_SAVE = 22; - const RECORD_UPDATE = 23; - const RECORD_INSERT = 24; - const RECORD_SERIALIZE = 25; - const RECORD_UNSERIALIZE = 26; - */ - /** - * @var mixed $_invoker the handler which invoked this event - */ - //protected $_invoker; - - /** - * @var string $_query the sql query associated with this event (if any) - */ - //protected $_query; - - /** - * @var string $_params the parameters associated with the query (if any) - */ - //protected $_params; - - /** - * @see Doctrine_Event constants - * @var integer $_code the event code - */ - //protected $_code; - - /** - * @var integer $_startedMicrotime the time point in which this event was started - */ - //protected $_startedMicrotime; - - /** - * @var integer $_endedMicrotime the time point in which this event was ended - */ - //protected $_endedMicrotime; - - /** - * @var array $_options an array of options - */ - //protected $_options = array(); - - /** - * constructor - * - * @param Doctrine_Connection|Doctrine_Connection_Statement| - Doctrine_Connection_UnitOfWork|Doctrine_Transaction $invoker the handler which invoked this event - * @param integer $code the event code - * @param string $query the sql query associated with this event (if any) - */ - /*public function __construct($invoker, $code, $query = null, $params = array()) - { - $this->_invoker = $invoker; - $this->_code = $code; - $this->_query = $query; - $this->_params = $params; - }*/ - - /** - * getQuery - * - * @return string returns the query associated with this event (if any) - */ - /*public function getQuery() - { - return $this->_query; - }*/ - - /** - * getName - * returns the name of this event - * - * @return string the name of this event - */ - /*public function getName() - { - switch ($this->_code) { - case self::CONN_QUERY: - return 'query'; - case self::CONN_EXEC: - return 'exec'; - case self::CONN_PREPARE: - return 'prepare'; - case self::CONN_CONNECT: - return 'connect'; - case self::CONN_CLOSE: - return 'close'; - case self::CONN_ERROR: - return 'error'; - - case self::STMT_EXECUTE: - return 'execute'; - case self::STMT_FETCH: - return 'fetch'; - case self::STMT_FETCHALL: - return 'fetch all'; - - case self::TX_BEGIN: - return 'begin'; - case self::TX_COMMIT: - return 'commit'; - case self::TX_ROLLBACK: - return 'rollback'; - - case self::SAVEPOINT_CREATE: - return 'create savepoint'; - case self::SAVEPOINT_ROLLBACK: - return 'rollback savepoint'; - case self::SAVEPOINT_COMMIT: - return 'commit savepoint'; - - case self::RECORD_DELETE: - return 'delete record'; - case self::RECORD_SAVE: - return 'save record'; - case self::RECORD_UPDATE: - return 'update record'; - case self::RECORD_INSERT: - return 'insert record'; - case self::RECORD_SERIALIZE: - return 'serialize record'; - case self::RECORD_UNSERIALIZE: - return 'unserialize record'; - } - } - */ - /** - * getCode - * - * @return integer returns the code associated with this event - */ - /*public function getCode() - { - return $this->_code; - }*/ - - /** - * getOption - * returns the value of an option - * - * @param string $option the name of the option - * @return mixed - */ - /*public function __get($option) - { - if ( ! isset($this->_options[$option])) { - return null; - } - - return $this->_options[$option]; - }*/ - - /** - * skipOperation - * skips the next operation - * an alias for __set('skipOperation', true) - * - * @return Doctrine_Event this object - */ - /*public function skipOperation() - { - $this->_options['skipOperation'] = true; - - return $this; - }*/ - - /** - * setOption - * sets the value of an option - * - * @param string $option the name of the option - * @param mixed $value the value of the given option - * @return Doctrine_Event this object - */ - /*public function __set($option, $value) - { - $this->_options[$option] = $value; - - return $this; - }*/ - - /** - * setOption - * sets the value of an option by reference - * - * @param string $option the name of the option - * @param mixed $value the value of the given option - * @return Doctrine_Event this object - */ - /*public function set($option, &$value) - { - $this->_options[$option] =& $value; - - return $this; - }*/ - - /** - * start - * starts the internal timer of this event - * - * @return Doctrine_Event this object - */ - /*public function start() - { - $this->_startedMicrotime = microtime(true); - }*/ - - /** - * hasEnded - * whether or not this event has ended - * - * @return boolean - */ - /*public function hasEnded() - { - return ($this->_endedMicrotime != null); - }*/ - - /** - * end - * ends the internal timer of this event - * - * @return Doctrine_Event this object - */ - /*public function end() - { - $this->_endedMicrotime = microtime(true); - - return $this; - }*/ - - /** - * getInvoker - * returns the handler that invoked this event - * - * @return Doctrine_Connection|Doctrine_Connection_Statement| - * Doctrine_Connection_UnitOfWork|Doctrine_Transaction the handler that invoked this event - */ - /*public function getInvoker() - { - return $this->_invoker; - }*/ - - /** - * getParams - * returns the parameters of the query - * - * @return array parameters of the query - */ - /*public function getParams() - { - return $this->_params; - }*/ - - /** - * Get the elapsed time (in microseconds) that the event ran. If the event has - * not yet ended, return false. - * - * @return mixed - */ - /*public function getElapsedSecs() - { - if (is_null($this->_endedMicrotime)) { - return false; - } - return ($this->_endedMicrotime - $this->_startedMicrotime); - }*/ -} + //...more + + + protected $_type; + protected $_target; + protected $_defaultPrevented; + + + public function __construct($type, $target = null) + { + $this->_type = $type; + $this->_target = $target; + $this->_defaultPrevented = false; + } + + + public function getType() + { + return $this->_type; + } + + + public function preventDefault() + { + $this->_defaultPrevented = true; + } + + + public function getDefaultPrevented() + { + return $this->_defaultPrevented; + } + + + public function getTarget() + { + return $this->_target; + } +} diff --git a/lib/Doctrine/EventListener.php b/lib/Doctrine/EventListener.php index 5aff65ee9..754303642 100644 --- a/lib/Doctrine/EventListener.php +++ b/lib/Doctrine/EventListener.php @@ -18,7 +18,7 @@ * and is licensed under the LGPL. For more information, see * . */ -Doctrine::autoload('Doctrine_EventListener_Interface'); + /** * Doctrine_EventListener all event listeners extend this base class * the empty methods allow child classes to only implement the methods they need to implement diff --git a/lib/Doctrine/EventManager.php b/lib/Doctrine/EventManager.php new file mode 100644 index 000000000..804b0e648 --- /dev/null +++ b/lib/Doctrine/EventManager.php @@ -0,0 +1,43 @@ +getType(); + + if (isset($this->_listeners[$callback])) { + $event = $argIsCallback ? new Doctrine_Event($event) : $event; + foreach ($this->_listeners[$callback] as $listener) { + $listener->$callback($event); + } + } + + return ! $event->getDefaultPrevented(); + } + + public function getListeners($callback = null) { + return $callback ? $this->_listeners[$callback] : $this->_listeners; + } + + public function hasListeners($callback) { + return isset($this->_listeners[$callback]); + } + + public function registerEventListener($listener, $callbacks) { + // TODO: maybe check for duplicate registrations? + if (is_array($callbacks)) { + foreach ($callbacks as $callback) { + $this->_listeners[$callback] = $listener; + } + } else { + $this->_listeners[$callbacks] = $listener; + } + } +} + +?> \ No newline at end of file diff --git a/lib/Doctrine/Manager.php b/lib/Doctrine/Manager.php index 9ae27cb79..292315094 100644 --- a/lib/Doctrine/Manager.php +++ b/lib/Doctrine/Manager.php @@ -92,14 +92,12 @@ class Doctrine_Manager implements Doctrine_Configurable, Countable, IteratorAggr if ( ! $init) { $init = true; $attributes = array( - Doctrine::ATTR_CACHE => null, Doctrine::ATTR_RESULT_CACHE => null, Doctrine::ATTR_QUERY_CACHE => null, Doctrine::ATTR_LOAD_REFERENCES => true, Doctrine::ATTR_LISTENER => new Doctrine_EventListener(), Doctrine::ATTR_RECORD_LISTENER => null, Doctrine::ATTR_THROW_EXCEPTIONS => true, - Doctrine::ATTR_VALIDATE => Doctrine::VALIDATE_NONE, Doctrine::ATTR_QUERY_LIMIT => Doctrine::LIMIT_RECORDS, Doctrine::ATTR_IDXNAME_FORMAT => "%s_idx", Doctrine::ATTR_SEQNAME_FORMAT => "%s_seq", diff --git a/lib/Doctrine/Query/Parser.php b/lib/Doctrine/Query/Parser.php index be417c54f..01639fb6c 100644 --- a/lib/Doctrine/Query/Parser.php +++ b/lib/Doctrine/Query/Parser.php @@ -31,7 +31,7 @@ * @author Janne Vanhala * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link http://www.phpdoctrine.org - * @since 1.0 + * @since 2.0 * @version $Revision$ */ class Doctrine_Query_Parser diff --git a/lib/Doctrine/Query/Production.php b/lib/Doctrine/Query/Production.php index 150159168..dbfe2ab8d 100644 --- a/lib/Doctrine/Query/Production.php +++ b/lib/Doctrine/Query/Production.php @@ -30,7 +30,7 @@ * @author Janne Vanhala * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link http://www.phpdoctrine.org - * @since 1.0 + * @since 2.0 * @version $Revision$ */ abstract class Doctrine_Query_Production diff --git a/lib/Doctrine/Query/Production/PathExpression.php b/lib/Doctrine/Query/Production/PathExpression.php index 4fb9a5dc7..9f6fe049b 100644 --- a/lib/Doctrine/Query/Production/PathExpression.php +++ b/lib/Doctrine/Query/Production/PathExpression.php @@ -130,7 +130,7 @@ class Doctrine_Query_Production_PathExpression extends Doctrine_Query_Production $parserResult = $this->_parser->getParserResult(); // Retrieving connection - $manager = Doctrine_EntityManager::getManager(); + $manager = Doctrine_EntityManagerFactory::getManager(); $conn = $manager->getConnection(); // Looking for queryComponent to fetch diff --git a/lib/Doctrine/Query/Production/PathExpressionEndingWithAsterisk.php b/lib/Doctrine/Query/Production/PathExpressionEndingWithAsterisk.php index bfbf1cc4c..c24fdaae0 100644 --- a/lib/Doctrine/Query/Production/PathExpressionEndingWithAsterisk.php +++ b/lib/Doctrine/Query/Production/PathExpressionEndingWithAsterisk.php @@ -119,7 +119,7 @@ class Doctrine_Query_Production_PathExpressionEndingWithAsterisk extends Doctrin $parserResult = $this->_parser->getParserResult(); // Retrieving connection - $manager = Doctrine_EntityManager::getManager(); + $manager = Doctrine_EntityManagerFactory::getManager(); $conn = $manager->getConnection(); // Looking for componentAlias to fetch diff --git a/lib/Doctrine/Query/Production/RangeVariableDeclaration.php b/lib/Doctrine/Query/Production/RangeVariableDeclaration.php index 4c49cb67a..01ed5ff5b 100644 --- a/lib/Doctrine/Query/Production/RangeVariableDeclaration.php +++ b/lib/Doctrine/Query/Production/RangeVariableDeclaration.php @@ -116,7 +116,7 @@ class Doctrine_Query_Production_RangeVariableDeclaration extends Doctrine_Query_ // Get the connection for the component $conn = $this->_parser->getSqlBuilder()->getConnection(); - $manager = Doctrine_EntityManager::getManager(); + $manager = Doctrine_EntityManagerFactory::getManager(); $componentName = $this->_identifiers[0]; // Retrieving ClassMetadata and Mapper @@ -156,7 +156,7 @@ class Doctrine_Query_Production_RangeVariableDeclaration extends Doctrine_Query_ // Get the connection for the component $conn = $this->_parser->getSqlBuilder()->getConnection(); - $manager = Doctrine_EntityManager::getManager(); + $manager = Doctrine_EntityManagerFactory::getManager(); // Retrieve the base component try { diff --git a/lib/Doctrine/Query/Production/SelectExpression.php b/lib/Doctrine/Query/Production/SelectExpression.php index bf6d47c7d..150de0139 100644 --- a/lib/Doctrine/Query/Production/SelectExpression.php +++ b/lib/Doctrine/Query/Production/SelectExpression.php @@ -134,7 +134,7 @@ class Doctrine_Query_Production_SelectExpression extends Doctrine_Query_Producti $parserResult = $this->_parser->getParserResult(); // Retrieving connection - $manager = Doctrine_EntityManager::getManager(); + $manager = Doctrine_EntityManagerFactory::getManager(); $conn = $manager->getConnection(); switch (get_class($this->_leftExpression)) { diff --git a/lib/Doctrine/Query/Production/UpdateClause.php b/lib/Doctrine/Query/Production/UpdateClause.php index e9561e030..68c8d9592 100644 --- a/lib/Doctrine/Query/Production/UpdateClause.php +++ b/lib/Doctrine/Query/Production/UpdateClause.php @@ -28,7 +28,7 @@ * @author Janne Vanhala * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link http://www.phpdoctrine.org - * @since 1.0 + * @since 2.0 * @version $Revision$ */ class Doctrine_Query_Production_UpdateClause extends Doctrine_Query_Production diff --git a/lib/Doctrine/Query/Production/VariableDeclaration.php b/lib/Doctrine/Query/Production/VariableDeclaration.php index ebf6ec297..1528d6102 100644 --- a/lib/Doctrine/Query/Production/VariableDeclaration.php +++ b/lib/Doctrine/Query/Production/VariableDeclaration.php @@ -84,7 +84,7 @@ class Doctrine_Query_Production_VariableDeclaration extends Doctrine_Query_Produ // Get the connection for the component $conn = $this->_parser->getSqlBuilder()->getConnection(); - $manager = Doctrine_EntityManager::getManager(); + $manager = Doctrine_EntityManagerFactory::getManager(); // Retrieving ClassMetadata and Mapper try { @@ -124,7 +124,7 @@ class Doctrine_Query_Production_VariableDeclaration extends Doctrine_Query_Produ $queryComponent = $parserResult->getQueryComponent($this->_componentAlias); // Retrieving connection - $manager = Doctrine_EntityManager::getManager(); + $manager = Doctrine_EntityManagerFactory::getManager(); $conn = $manager->getConnection(); return $conn->quoteIdentifier($queryComponent['metadata']->getTableName()) . ' ' diff --git a/lib/Doctrine/Relation.php b/lib/Doctrine/Relation.php index eb527d382..0b5bddcec 100644 --- a/lib/Doctrine/Relation.php +++ b/lib/Doctrine/Relation.php @@ -245,7 +245,7 @@ abstract class Doctrine_Relation implements ArrayAccess */ final public function getTable() { - return Doctrine_EntityManager::getManager($this->definition['class']) + return Doctrine_EntityManagerFactory::getManager($this->definition['class']) ->getClassMetadata($this->definition['class']); } @@ -269,7 +269,7 @@ abstract class Doctrine_Relation implements ArrayAccess */ final public function getClassMetadata() { - return Doctrine_EntityManager::getManager($this->definition['class']) + return Doctrine_EntityManagerFactory::getManager($this->definition['class']) ->getClassMetadata($this->definition['class']); } diff --git a/tests/Orm/AllTests.php b/tests/Orm/AllTests.php index 472cfba97..7535e4afe 100644 --- a/tests/Orm/AllTests.php +++ b/tests/Orm/AllTests.php @@ -13,6 +13,7 @@ require_once 'Orm/Ticket/AllTests.php'; // Tests require_once 'Orm/UnitOfWorkTestCase.php'; +require_once 'Orm/EntityManagerFactoryTest.php'; class Orm_AllTests { @@ -26,6 +27,7 @@ class Orm_AllTests $suite = new Doctrine_OrmTestSuite('Doctrine Orm'); $suite->addTestSuite('Orm_UnitOfWorkTestCase'); + $suite->addTestSuite('Orm_EntityManagerFactoryTest'); //$suite->addTestSuite('Orm_ConfigurableTestCase'); $suite->addTest(Orm_Component_AllTests::suite()); diff --git a/tests/Orm/EntityManagerFactoryTest.php b/tests/Orm/EntityManagerFactoryTest.php new file mode 100644 index 000000000..5f508381b --- /dev/null +++ b/tests/Orm/EntityManagerFactoryTest.php @@ -0,0 +1,41 @@ + 'mock', 'user' => '', 'password' => ''); + + protected function setUp() { + parent::setUp(); + $this->_emf = $this->sharedFixture['emf']; + } + + protected function tearDown() { + parent::tearDown(); + } + + private function _createNamedManager($name) + { + return $this->_emf->createEntityManager($this->_mockOptions, $name); + } + + public function testBindingEntityToNamedManager() + { + $myEM = $this->_createNamedManager('myEM'); + $this->_emf->bindEntityToManager('SomeEntity', 'myEM'); + $this->assertSame($myEM, $this->_emf->getEntityManager('SomeEntity')); + $this->_emf->releaseEntityManager('myEM'); + } + + public function testStaticLookup() + { + $this->assertTrue(Doctrine_EntityManagerFactory::getManager() instanceof Doctrine_EntityManager); + } + +} \ No newline at end of file diff --git a/tests/Orm/EntityManagerTest.php b/tests/Orm/EntityManagerTest.php deleted file mode 100644 index 14fa2200b..000000000 --- a/tests/Orm/EntityManagerTest.php +++ /dev/null @@ -1,49 +0,0 @@ -assertSame($em, Doctrine_EntityManager::getManager('SomeEntity')); - } - - public function testStaticGetManagerThrowsExceptionIfNoManagerAvailable() - { - try { - Doctrine_EntityManager::getManager('SomeEntity'); - $this->fail("Expected exception not thrown."); - } catch (Doctrine_EntityManager_Exception $ex) {} - } - - public function testBindingValidEntityToNamedManager() - { - $em = new Doctrine_EntityManager(new Doctrine_Connection_Mock(null), 'myEM'); - Doctrine_EntityManager::bindEntityToManager('SomeEntity', 'myEM'); - $this->assertSame($em, Doctrine_EntityManager::getManager('SomeEntity')); - } - - public function testBindingEntityToInvalidManagerThrowsExceptionOnRetrieval() - { - // will work. we don't check the existence of the EM during binding - Doctrine_EntityManager::bindEntityToManager('SomeEntity', 'myEM'); - // exception on access - try { - Doctrine_EntityManager::getManager('SomeEntity'); - $this->fail(); - } catch (Doctrine_EntityManager_Exception $ex) {} - } - -} \ No newline at end of file diff --git a/tests/Orm/Hydration/BasicHydrationTest.php b/tests/Orm/Hydration/BasicHydrationTest.php index 475bf3f27..c7eecf758 100644 --- a/tests/Orm/Hydration/BasicHydrationTest.php +++ b/tests/Orm/Hydration/BasicHydrationTest.php @@ -6,13 +6,10 @@ require_once 'lib/DoctrineTestInit.php'; require_once 'lib/mocks/Doctrine_HydratorMockStatement.php'; class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase -{ - private $_em; - +{ protected function setUp() { parent::setUp(); - $this->_em = new Doctrine_EntityManager(new Doctrine_Connection_Mock()); } /** Getter for the hydration mode dataProvider */ diff --git a/tests/Orm/Query/DeleteSqlGenerationTest.php b/tests/Orm/Query/DeleteSqlGenerationTest.php index 65c13c7ea..aeb36d668 100755 --- a/tests/Orm/Query/DeleteSqlGenerationTest.php +++ b/tests/Orm/Query/DeleteSqlGenerationTest.php @@ -40,7 +40,7 @@ class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed) { try { - $entityManager = Doctrine_EntityManager::getManager(); + $entityManager = $this->sharedFixture['em']; $query = $entityManager->createQuery($dqlToBeTested); parent::assertEquals($sqlToBeConfirmed, $query->getSql()); diff --git a/tests/Orm/Query/DqlGenerationTest.php b/tests/Orm/Query/DqlGenerationTest.php index f37139daa..6b1202ef8 100755 --- a/tests/Orm/Query/DqlGenerationTest.php +++ b/tests/Orm/Query/DqlGenerationTest.php @@ -36,7 +36,7 @@ class Orm_Query_DqlGenerationTest extends Doctrine_OrmTestCase { protected function createQuery() { - $entityManager = Doctrine_EntityManager::getManager(); + $entityManager = $this->sharedFixture['em']; return $entityManager->createQuery(); } diff --git a/tests/Orm/Query/IdentifierRecognitionTest.php b/tests/Orm/Query/IdentifierRecognitionTest.php index 095b502da..338d32385 100755 --- a/tests/Orm/Query/IdentifierRecognitionTest.php +++ b/tests/Orm/Query/IdentifierRecognitionTest.php @@ -39,7 +39,7 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase public function testSingleAliasDeclarationIsSupported() { - $entityManager = Doctrine_EntityManager::getManager(); + $entityManager = $this->sharedFixture['em']; $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u'); $parserResult = $query->parse(); @@ -54,7 +54,7 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase public function testSingleAliasDeclarationWithIndexByIsSupported() { - $entityManager = Doctrine_EntityManager::getManager(); + $entityManager = $this->sharedFixture['em']; $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u INDEX BY id'); $parserResult = $query->parse(); @@ -69,7 +69,7 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase public function testQueryParserSupportsMultipleAliasDeclarations() { - $entityManager = Doctrine_EntityManager::getManager(); + $entityManager = $this->sharedFixture['em']; $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u INDEX BY id LEFT JOIN u.phonenumbers p'); $parserResult = $query->parse(); @@ -93,7 +93,7 @@ class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase public function testQueryParserSupportsMultipleAliasDeclarationsWithIndexBy() { - $entityManager = Doctrine_EntityManager::getManager(); + $entityManager = $this->sharedFixture['em']; $query = $entityManager->createQuery('SELECT u.* FROM CmsUser u INDEX BY id LEFT JOIN u.articles a INNER JOIN u.phonenumbers pn INDEX BY phonenumber'); $parserResult = $query->parse(); diff --git a/tests/Orm/Query/LanguageRecognitionTest.php b/tests/Orm/Query/LanguageRecognitionTest.php index cb0d8422c..97798bcc4 100755 --- a/tests/Orm/Query/LanguageRecognitionTest.php +++ b/tests/Orm/Query/LanguageRecognitionTest.php @@ -41,7 +41,7 @@ class Orm_Query_LanguageRecognitionTest extends Doctrine_OrmTestCase public function assertValidDql($dql) { try { - $entityManager = Doctrine_EntityManager::getManager(); + $entityManager = $this->sharedFixture['em']; $query = $entityManager->createQuery($dql); $parserResult = $query->parse(); } catch (Doctrine_Exception $e) { @@ -52,7 +52,7 @@ class Orm_Query_LanguageRecognitionTest extends Doctrine_OrmTestCase public function assertInvalidDql($dql) { try { - $entityManager = Doctrine_EntityManager::getManager(); + $entityManager = $this->sharedFixture['em']; $query = $entityManager->createQuery($dql); $query->setDql($dql); $parserResult = $query->parse(); diff --git a/tests/Orm/Query/SelectSqlGenerationTest.php b/tests/Orm/Query/SelectSqlGenerationTest.php index ac4ea3258..1dd6a6b65 100755 --- a/tests/Orm/Query/SelectSqlGenerationTest.php +++ b/tests/Orm/Query/SelectSqlGenerationTest.php @@ -40,7 +40,7 @@ class Orm_Query_SelectSqlGenerationTest extends Doctrine_OrmTestCase public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed) { try { - $entityManager = Doctrine_EntityManager::getManager(); + $entityManager = $this->sharedFixture['em']; $query = $entityManager->createQuery($dqlToBeTested); //echo print_r($query->parse()->getQueryFields(), true) . "\n"; diff --git a/tests/Orm/Query/UpdateSqlGenerationTest.php b/tests/Orm/Query/UpdateSqlGenerationTest.php index 7900a88e1..c8b6f5762 100755 --- a/tests/Orm/Query/UpdateSqlGenerationTest.php +++ b/tests/Orm/Query/UpdateSqlGenerationTest.php @@ -40,7 +40,7 @@ class Orm_Query_UpdateSqlGenerationTest extends Doctrine_OrmTestCase public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed) { try { - $entityManager = Doctrine_EntityManager::getManager(); + $entityManager = $this->sharedFixture['em']; $query = $entityManager->createQuery($dqlToBeTested); parent::assertEquals($sqlToBeConfirmed, $query->getSql()); diff --git a/tests/lib/Doctrine_OrmTestCase.php b/tests/lib/Doctrine_OrmTestCase.php index d044450be..d3a0fc2da 100644 --- a/tests/lib/Doctrine_OrmTestCase.php +++ b/tests/lib/Doctrine_OrmTestCase.php @@ -5,7 +5,9 @@ */ class Doctrine_OrmTestCase extends Doctrine_TestCase { + protected $_em; + protected function setUp() { - $em = new Doctrine_EntityManager(new Doctrine_Connection_Mock()); + $this->_em = new Doctrine_EntityManager(new Doctrine_Connection_Mock()); } } diff --git a/tests/lib/Doctrine_OrmTestSuite.php b/tests/lib/Doctrine_OrmTestSuite.php index 6d01925c4..534158631 100644 --- a/tests/lib/Doctrine_OrmTestSuite.php +++ b/tests/lib/Doctrine_OrmTestSuite.php @@ -8,5 +8,21 @@ */ class Doctrine_OrmTestSuite extends Doctrine_TestSuite { - + protected function setUp() + { + $emf = new Doctrine_EntityManagerFactory(); + $emf->setConfiguration(new Doctrine_Configuration()); + $emf->setEventManager(new Doctrine_EventManager()); + $connectionOptions = array( + 'driver' => 'mock', + 'user' => 'john', + 'password' => 'wayne' + ); + $em = $emf->createEntityManager($connectionOptions, 'mockEM'); + $this->sharedFixture['emf'] = $emf; + $this->sharedFixture['em'] = $em; + } + + protected function tearDown() + {} } \ No newline at end of file