From 6e5b1bbe60a24dec888d99881aa4960d23dfaf9d Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sat, 10 Apr 2010 17:35:18 +0200 Subject: [PATCH] DDC-510 - Refactored Metadata Driver to be an required option, even for Annotations - allowing to use the specified paths in ORM Tooling Commands --- lib/Doctrine/ORM/Configuration.php | 32 +++++---- lib/Doctrine/ORM/EntityManager.php | 4 +- .../ORM/Mapping/Driver/AnnotationDriver.php | 72 +++++++++++-------- lib/Doctrine/ORM/Mapping/MappingException.php | 6 ++ lib/Doctrine/ORM/ORMException.php | 6 ++ .../Command/SchemaTool/AbstractCommand.php | 31 +------- .../Console/Helper/EntityManagerHelper.php | 18 +---- .../Tests/Mocks/EntityManagerMock.php | 1 + .../Tests/ORM/Functional/QueryCacheTest.php | 4 +- .../Tests/ORM/Functional/ResultCacheTest.php | 2 +- .../Doctrine/Tests/OrmFunctionalTestCase.php | 5 +- tests/Doctrine/Tests/OrmTestCase.php | 4 ++ 12 files changed, 89 insertions(+), 96 deletions(-) diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 6d5964c8c..4f7de0b4e 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -21,6 +21,9 @@ namespace Doctrine\ORM; +use Doctrine\Common\Cache\Cache, + Doctrine\ORM\Mapping\Driver\Driver; + /** * Configuration container for all configuration options of Doctrine. * It combines all configuration options from DBAL & ORM. @@ -118,11 +121,11 @@ class Configuration extends \Doctrine\DBAL\Configuration /** * Sets the cache driver implementation that is used for metadata caching. * - * @param object $driverImpl + * @param Driver $driverImpl * @todo Force parameter to be a Closure to ensure lazy evaluation * (as soon as a metadata cache is in effect, the driver never needs to initialize). */ - public function setMetadataDriverImpl($driverImpl) + public function setMetadataDriverImpl(Driver $driverImpl) { $this->_attributes['metadataDriverImpl'] = $driverImpl; } @@ -168,14 +171,13 @@ class Configuration extends \Doctrine\DBAL\Configuration /** * Gets the cache driver implementation that is used for the mapping metadata. * - * @return object + * @throws ORMException + * @return Mapping\Driver\Driver */ public function getMetadataDriverImpl() { if ($this->_attributes['metadataDriverImpl'] == null) { - $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache); - $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); - $this->_attributes['metadataDriverImpl'] = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader); + throw ORMException::missingMappingDriverImpl(); } return $this->_attributes['metadataDriverImpl']; @@ -184,7 +186,7 @@ class Configuration extends \Doctrine\DBAL\Configuration /** * Gets the cache driver implementation that is used for query result caching. * - * @return object + * @return \Doctrine\Common\Cache\Cache */ public function getResultCacheImpl() { @@ -194,9 +196,9 @@ class Configuration extends \Doctrine\DBAL\Configuration /** * Sets the cache driver implementation that is used for query result caching. * - * @param object $cacheImpl + * @param \Doctrine\Common\Cache\Cache $cacheImpl */ - public function setResultCacheImpl($cacheImpl) + public function setResultCacheImpl(Cache $cacheImpl) { $this->_attributes['resultCacheImpl'] = $cacheImpl; } @@ -204,7 +206,7 @@ class Configuration extends \Doctrine\DBAL\Configuration /** * Gets the cache driver implementation that is used for the query cache (SQL cache). * - * @return object + * @return \Doctrine\Common\Cache\Cache */ public function getQueryCacheImpl() { @@ -214,9 +216,9 @@ class Configuration extends \Doctrine\DBAL\Configuration /** * Sets the cache driver implementation that is used for the query cache (SQL cache). * - * @param object $cacheImpl + * @param \Doctrine\Common\Cache\Cache $cacheImpl */ - public function setQueryCacheImpl($cacheImpl) + public function setQueryCacheImpl(Cache $cacheImpl) { $this->_attributes['queryCacheImpl'] = $cacheImpl; } @@ -224,7 +226,7 @@ class Configuration extends \Doctrine\DBAL\Configuration /** * Gets the cache driver implementation that is used for metadata caching. * - * @return object + * @return \Doctrine\Common\Cache\Cache */ public function getMetadataCacheImpl() { @@ -234,9 +236,9 @@ class Configuration extends \Doctrine\DBAL\Configuration /** * Sets the cache driver implementation that is used for metadata caching. * - * @param object $cacheImpl + * @param \Doctrine\Common\Cache\Cache $cacheImpl */ - public function setMetadataCacheImpl($cacheImpl) + public function setMetadataCacheImpl(Cache $cacheImpl) { $this->_attributes['metadataCacheImpl'] = $cacheImpl; } diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 897569b8c..d915f1d63 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -593,9 +593,9 @@ class EntityManager * @param EventManager $eventManager The EventManager instance to use. * @return EntityManager The created EntityManager. */ - public static function create($conn, Configuration $config = null, EventManager $eventManager = null) + public static function create($conn, Configuration $config, EventManager $eventManager = null) { - $config = $config ?: new Configuration(); + $config->getMetadataDriverImpl(); // assert this is set if (is_array($conn)) { $conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, ($eventManager ?: new EventManager())); diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index 8e01479f7..2f669cf07 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -428,40 +428,41 @@ class AnnotationDriver implements Driver return $this->_classNames; } + if (count($this->_paths) == 0) { + throw MappingException::pathRequired(); + } + $classes = array(); + $includedFiles = array(); - if ($this->_paths) { - $includedFiles = array(); - - foreach ((array) $this->_paths as $path) { - if ( ! is_dir($path)) { - throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath(); - } - - $iterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($path), - \RecursiveIteratorIterator::LEAVES_ONLY - ); - - foreach ($iterator as $file) { - if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) { - continue; - } - - $sourceFile = realpath($file->getPathName()); - require_once $sourceFile; - $includedFiles[] = $sourceFile; - } + foreach ($this->_paths as $path) { + if ( ! is_dir($path)) { + throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath(); } - $declared = get_declared_classes(); + $iterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($path), + \RecursiveIteratorIterator::LEAVES_ONLY + ); - foreach ($declared as $className) { - $rc = new \ReflectionClass($className); - $sourceFile = $rc->getFileName(); - if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) { - $classes[] = $className; + foreach ($iterator as $file) { + if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) { + continue; } + + $sourceFile = realpath($file->getPathName()); + require_once $sourceFile; + $includedFiles[] = $sourceFile; + } + } + + $declared = get_declared_classes(); + + foreach ($declared as $className) { + $rc = new \ReflectionClass($className); + $sourceFile = $rc->getFileName(); + if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) { + $classes[] = $className; } } @@ -470,4 +471,19 @@ class AnnotationDriver implements Driver return $classes; } + /** + * Factory method for the Annotation Driver + * + * @param array|string $paths + * @param AnnotationReader $reader + * @return AnnotationDriver + */ + static public function create($paths = array(), AnnotationReader $reader = null) + { + if ($reader == null) { + $reader = new AnnotationReader(); + $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); + } + return new self($reader, $paths); + } } diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index ae0907728..8881a3c71 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -28,6 +28,12 @@ namespace Doctrine\ORM\Mapping; */ class MappingException extends \Doctrine\ORM\ORMException { + public static function pathRequired() + { + return new self("Specifying the paths to your entities is required ". + "in the AnnotationDriver to retrieve all class names."); + } + public static function identifierRequired($entityName) { return new self("No identifier/primary key specified for Entity '$entityName'." diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index 82a8a877e..4d39d1e47 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -10,6 +10,12 @@ namespace Doctrine\ORM; */ class ORMException extends \Exception { + public static function missingMappingDriverImpl() + { + return new self("It's a requirement to specify a Metadata Driver and pass it ". + "to Doctrine\ORM\Configuration::setMetadataDriverImpl()."); + } + public static function entityMissingAssignedId($entity) { return new self("Entity of type " . get_class($entity) . " is missing an assigned ID."); diff --git a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php index 12712383f..84267fbd7 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php @@ -50,36 +50,7 @@ abstract class AbstractCommand extends Command /* @var $em \Doctrine\ORM\EntityManager */ $em = $emHelper->getEntityManager(); - $reader = new \Doctrine\ORM\Tools\ClassMetadataReader(); - $reader->setEntityManager($em); - - $metadataDriver = $em->getConfiguration()->getMetadataDriverImpl(); - if ($metadataDriver instanceof AbstractFileDriver) { - $reader->addMappingSource($metadataDriver); - } - - // Process source directories - if ($emHelper->hasAdditionalMappingPathInformation()) { - - foreach ($emHelper->getMappingPaths() as $dirName) { - $dirName = realpath($dirName); - - if ( ! file_exists($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not exist.", $dirName) - ); - } else if ( ! is_readable($dirName)) { - throw new \InvalidArgumentException( - sprintf("Mapping directory '%s' does not have read permissions.", $dirName) - ); - } - - $reader->addMappingSource($dirName); - } - } - - // Retrieving ClassMetadatas, autoloading required since we need access to the Reflection stuff. - $metadatas = $reader->getMetadatas(true); + $metadatas = $em->getMetadataFactory()->getAllMetadata(); if ( ! empty($metadatas)) { // Create SchemaTool diff --git a/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php b/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php index e706d69ac..2636006e3 100644 --- a/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php +++ b/lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php @@ -44,20 +44,14 @@ class EntityManagerHelper extends Helper */ protected $_em; - /** - * @var array - */ - protected $_mappingPaths = array(); - /** * Constructor * * @param Connection $connection Doctrine Database Connection */ - public function __construct(EntityManager $em, $mappingPaths = array()) + public function __construct(EntityManager $em) { $this->_em = $em; - $this->_mappingPaths = $mappingPaths; } /** @@ -70,16 +64,6 @@ class EntityManagerHelper extends Helper return $this->_em; } - public function hasAdditionalMappingPathInformation() - { - return count($this->_mappingPaths); - } - - public function getMappingPaths() - { - return $this->_mappingPaths; - } - /** * @see Helper */ diff --git a/tests/Doctrine/Tests/Mocks/EntityManagerMock.php b/tests/Doctrine/Tests/Mocks/EntityManagerMock.php index dd4a36ca4..4006ebda7 100644 --- a/tests/Doctrine/Tests/Mocks/EntityManagerMock.php +++ b/tests/Doctrine/Tests/Mocks/EntityManagerMock.php @@ -78,6 +78,7 @@ class EntityManagerMock extends \Doctrine\ORM\EntityManager $config = new \Doctrine\ORM\Configuration(); $config->setProxyDir(__DIR__ . '/../Proxies'); $config->setProxyNamespace('Doctrine\Tests\Proxies'); + $config->setMetadataDriverImpl(\Doctrine\ORM\Mapping\Driver\AnnotationDriver::create()); } if (is_null($eventManager)) { $eventManager = new \Doctrine\Common\EventManager(); diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryCacheTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryCacheTest.php index 46681ac05..34d317d2b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryCacheTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryCacheTest.php @@ -83,7 +83,7 @@ class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase public function testQueryCache_NoHitSaveParserResult() { - $this->_em->getConfiguration()->setQueryCacheImpl(null); + $this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache()); $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); @@ -103,7 +103,7 @@ class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase public function testQueryCache_HitDoesNotSaveParserResult() { - $this->_em->getConfiguration()->setQueryCacheImpl(null); + $this->_em->getConfiguration()->setQueryCacheImpl(new ArrayCache()); $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); diff --git a/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php b/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php index 05bd8333d..3c5f00982 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php @@ -82,7 +82,7 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertTrue($cache->contains('testing_result_cache_id')); - $this->_em->getConfiguration()->setResultCacheImpl(null); + $this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); } public function testNativeQueryResultCaching() diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 9e02cb285..b4ca023aa 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -221,7 +221,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase } if (is_null(self::$_queryCacheImpl)) { - self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache; + self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache; } $this->_sqlLoggerStack = new \Doctrine\DBAL\Logging\DebugStack(); @@ -234,6 +234,9 @@ abstract class OrmFunctionalTestCase extends OrmTestCase $config->setQueryCacheImpl(self::$_queryCacheImpl); $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('Doctrine\Tests\Proxies'); + + $driverImpl = \Doctrine\ORM\Mapping\Driver\AnnotationDriver::create(); + $config->setMetadataDriverImpl($driverImpl); $conn = $this->sharedFixture['conn']; $conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack); diff --git a/tests/Doctrine/Tests/OrmTestCase.php b/tests/Doctrine/Tests/OrmTestCase.php index 5ddf146dc..1167ad0b8 100644 --- a/tests/Doctrine/Tests/OrmTestCase.php +++ b/tests/Doctrine/Tests/OrmTestCase.php @@ -30,6 +30,10 @@ abstract class OrmTestCase extends DoctrineTestCase } else { $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache); } + + $driverImpl = \Doctrine\ORM\Mapping\Driver\AnnotationDriver::create(); + $config->setMetadataDriverImpl($driverImpl); + $config->setQueryCacheImpl(self::getSharedQueryCacheImpl()); $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('Doctrine\Tests\Proxies');