Skip to content

Commit

Permalink
DDC-510 - Refactored Metadata Driver to be an required option, even f…
Browse files Browse the repository at this point in the history
…or Annotations - allowing to use the specified paths in ORM Tooling Commands
  • Loading branch information
beberlei committed Apr 10, 2010
1 parent b2fe382 commit 6e5b1bb
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 94 deletions.
32 changes: 17 additions & 15 deletions lib/Doctrine/ORM/Configuration.php
Expand Up @@ -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.
Expand Down Expand Up @@ -118,11 +121,11 @@ public function setProxyNamespace($ns)
/**
* 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;
}
Expand Down Expand Up @@ -168,14 +171,13 @@ public function setEntityNamespaces(array $entityNamespaces)
/**
* 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'];
Expand All @@ -184,7 +186,7 @@ public function getMetadataDriverImpl()
/**
* Gets the cache driver implementation that is used for query result caching.
*
* @return object
* @return \Doctrine\Common\Cache\Cache
*/
public function getResultCacheImpl()
{
Expand All @@ -194,17 +196,17 @@ public function getResultCacheImpl()
/**
* 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;
}

/**
* Gets the cache driver implementation that is used for the query cache (SQL cache).
*
* @return object
* @return \Doctrine\Common\Cache\Cache
*/
public function getQueryCacheImpl()
{
Expand All @@ -214,17 +216,17 @@ public function getQueryCacheImpl()
/**
* 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;
}

/**
* Gets the cache driver implementation that is used for metadata caching.
*
* @return object
* @return \Doctrine\Common\Cache\Cache
*/
public function getMetadataCacheImpl()
{
Expand All @@ -234,9 +236,9 @@ public function getMetadataCacheImpl()
/**
* 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;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/EntityManager.php
Expand Up @@ -593,9 +593,9 @@ public function getProxyFactory()
* @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()));
Expand Down
68 changes: 42 additions & 26 deletions lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
Expand Up @@ -428,40 +428,41 @@ public function getAllClassNames()
return $this->_classNames;
}

$classes = array();
if (count($this->_paths) == 0) {
throw MappingException::pathRequired();
}

if ($this->_paths) {
$includedFiles = array();
$classes = array();
$includedFiles = array();

foreach ((array) $this->_paths as $path) {
if ( ! is_dir($path)) {
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath();
}
foreach ($this->_paths as $path) {
if ( ! is_dir($path)) {
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath();
}

$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path),
\RecursiveIteratorIterator::LEAVES_ONLY
);
$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 ($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();
$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;
}
foreach ($declared as $className) {
$rc = new \ReflectionClass($className);
$sourceFile = $rc->getFileName();
if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
$classes[] = $className;
}
}

Expand All @@ -470,4 +471,19 @@ public function getAllClassNames()
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);
}
}
6 changes: 6 additions & 0 deletions lib/Doctrine/ORM/Mapping/MappingException.php
Expand Up @@ -28,6 +28,12 @@
*/
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'."
Expand Down
6 changes: 6 additions & 0 deletions lib/Doctrine/ORM/ORMException.php
Expand Up @@ -10,6 +10,12 @@
*/
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.");
Expand Down
Expand Up @@ -50,36 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
/* @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
Expand Down
18 changes: 1 addition & 17 deletions lib/Doctrine/ORM/Tools/Console/Helper/EntityManagerHelper.php
Expand Up @@ -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;
}

/**
Expand All @@ -70,16 +64,6 @@ public function getEntityManager()
return $this->_em;
}

public function hasAdditionalMappingPathInformation()
{
return count($this->_mappingPaths);
}

public function getMappingPaths()
{
return $this->_mappingPaths;
}

/**
* @see Helper
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Doctrine/Tests/Mocks/EntityManagerMock.php
Expand Up @@ -78,6 +78,7 @@ public static function create($conn, \Doctrine\ORM\Configuration $config = null,
$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();
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/ORM/Functional/QueryCacheTest.php
Expand Up @@ -83,7 +83,7 @@ public function testQueryCache_DependsOnHydrationMode($query)

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');

Expand All @@ -103,7 +103,7 @@ public function testQueryCache_NoHitSaveParserResult()

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');

Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php
Expand Up @@ -82,7 +82,7 @@ public function testUseResultCache()

$this->assertTrue($cache->contains('testing_result_cache_id'));

$this->_em->getConfiguration()->setResultCacheImpl(null);
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache());
}

public function testNativeQueryResultCaching()
Expand Down
5 changes: 4 additions & 1 deletion tests/Doctrine/Tests/OrmFunctionalTestCase.php
Expand Up @@ -221,7 +221,7 @@ protected function _getEntityManager($config = null, $eventManager = null) {
}

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();
Expand All @@ -234,6 +234,9 @@ protected function _getEntityManager($config = null, $eventManager = null) {
$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);
Expand Down
4 changes: 4 additions & 0 deletions tests/Doctrine/Tests/OrmTestCase.php
Expand Up @@ -30,6 +30,10 @@ protected function _getTestEntityManager($conn = null, $conf = null, $eventManag
} 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');
Expand Down

0 comments on commit 6e5b1bb

Please sign in to comment.