DDC-510 - Refactored Metadata Driver to be an required option, even for Annotations - allowing to use the specified paths in ORM Tooling Commands
This commit is contained in:
parent
b2fe3820fc
commit
6e5b1bbe60
@ -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;
|
||||
}
|
||||
|
@ -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()));
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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'."
|
||||
|
@ -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.");
|
||||
|
@ -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 '<info>%s</info>' does not exist.", $dirName)
|
||||
);
|
||||
} else if ( ! is_readable($dirName)) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf("Mapping directory '<info>%s</info>' 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
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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();
|
||||
|
@ -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');
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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();
|
||||
@ -235,6 +235,9 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
|
||||
$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);
|
||||
|
||||
|
@ -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');
|
||||
|
Loading…
x
Reference in New Issue
Block a user