ClassMetadataFactory child classes can now filter the metadata when calling getAllMetadata #200

Closed
wants to merge 2 commits into
from
View
8 lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
@@ -95,20 +95,22 @@ public function getLoadedMetadata()
}
/**
- * Forces the factory to load the metadata of all classes known to the underlying
+ * Forces the factory to load the metadata of all or a subset of classes known to the underlying
* mapping driver.
*
+ * @param array $classNames An array that will be used as a filter
+ *
* @return array The ClassMetadata instances of all mapped classes.
*/
- public function getAllMetadata()
+ public function getAllMetadata(array $classNames = array())
{
if ( ! $this->initialized) {
$this->initialize();
}
$driver = $this->getDriver();
$metadata = array();
- foreach ($driver->getAllClassNames() as $className) {
+ foreach ($driver->getAllClassNames($classNames) as $className) {
$metadata[] = $this->getMetadataFor($className);
}
View
4 lib/Doctrine/Common/Persistence/Mapping/ClassMetadataFactory.php
@@ -34,9 +34,11 @@
* Forces the factory to load the metadata of all classes known to the underlying
* mapping driver.
*
+ * @param array $classNames An array that will be used as a filter
+ *
* @return array The ClassMetadata instances of all mapped classes.
*/
- function getAllMetadata();
+ function getAllMetadata(array $classNames = array());
/**
* Gets the class metadata descriptor for a class.
View
15 lib/Doctrine/Common/Persistence/Mapping/Driver/AnnotationDriver.php
@@ -161,7 +161,7 @@ public function isTransient($className)
/**
* {@inheritDoc}
*/
- public function getAllClassNames()
+ public function getAllClassNames(array $classes = array())
{
if ($this->classNames !== null) {
return $this->classNames;
@@ -171,7 +171,7 @@ public function getAllClassNames()
throw MappingException::pathRequired();
}
- $classes = array();
+ $foundClasses = array();
$includedFiles = array();
foreach ($this->paths as $path) {
@@ -198,17 +198,22 @@ public function getAllClassNames()
}
$declared = get_declared_classes();
+ $mustFilter = sizeof($classes) > 0;
@stof
Doctrine member
stof added a line comment Oct 7, 2012

Please don't use sizeof which is only an alias for count. and here, you could simply use ! empty($classes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
foreach ($declared as $className) {
$rc = new \ReflectionClass($className);
$sourceFile = $rc->getFileName();
if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
- $classes[] = $className;
+ if ($mustFilter && !in_array($className, $classes)) {
+ continue;
+ }
+
+ $foundClasses[] = $className;
}
}
- $this->classNames = $classes;
+ $this->classNames = $foundClasses;
- return $classes;
+ return $foundClasses;
}
}
View
10 lib/Doctrine/Common/Persistence/Mapping/Driver/FileDriver.php
@@ -141,9 +141,11 @@ public function isTransient($className)
/**
* Gets the names of all mapped classes known to this driver.
*
+ * @param array $classes An array of class names that will be used as a filter
+ *
* @return array The names of all mapped classes known to this driver.
*/
- public function getAllClassNames()
+ public function getAllClassNames(array $classes = array())
{
if ($this->classCache === null) {
$this->initialize();
@@ -152,6 +154,12 @@ public function getAllClassNames()
$classNames = (array)$this->locator->getAllClassNames($this->globalBasename);
if ($this->classCache) {
$classNames = array_merge(array_keys($this->classCache), $classNames);
+
+ if (sizeof($classes) > 0) {
+ $classNames = array_filter($classNames, function($className) use ($classes) {
+ return in_array($className, $classes);
+ });
+ }
}
return $classNames;
}
View
6 lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriver.php
@@ -38,11 +38,13 @@
function loadMetadataForClass($className, ClassMetadata $metadata);
/**
- * Gets the names of all mapped classes known to this driver.
+ * Gets the names of all mapped or a subset of classes known to this driver.
+ *
+ * @param array $classes An array of class names that will be used as a filter
*
* @return array The names of all mapped classes known to this driver.
*/
- function getAllClassNames();
+ function getAllClassNames(array $classes = array());
@stof
Doctrine member
stof added a line comment Oct 7, 2012

Changing an interface is a BC break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
/**
* Whether the class with the specified name should have its metadata loaded.
View
6 lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php
@@ -117,9 +117,11 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
/**
* Gets the names of all mapped classes known to this driver.
*
+ * @param array $classes An array of class names that will be used as a filter
+ *
* @return array The names of all mapped classes known to this driver.
*/
- public function getAllClassNames()
+ public function getAllClassNames(array $classes = array())
{
$classNames = array();
$driverClasses = array();
@@ -129,7 +131,7 @@ public function getAllClassNames()
$oid = spl_object_hash($driver);
if (!isset($driverClasses[$oid])) {
- $driverClasses[$oid] = $driver->getAllClassNames();
+ $driverClasses[$oid] = $driver->getAllClassNames($classes);
}
foreach ($driverClasses[$oid] AS $className) {
View
6 lib/Doctrine/Common/Persistence/Mapping/Driver/PHPDriver.php
@@ -25,9 +25,9 @@
* The PHPDriver includes php files which just populate ClassMetadataInfo
* instances with plain php code
*
- * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
- * @link www.doctrine-project.org
- * @since 2.0
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @link www.doctrine-project.org
+ * @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
View
7 lib/Doctrine/Common/Persistence/Mapping/Driver/StaticPHPDriver.php
@@ -82,7 +82,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
* {@inheritDoc}
* @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
*/
- public function getAllClassNames()
+ public function getAllClassNames(array $classNames = array())
{
if ($this->classNames !== null) {
return $this->classNames;
@@ -117,11 +117,16 @@ public function getAllClassNames()
}
$declared = get_declared_classes();
+ $mustFilter = sizeof($classNames) > 0;
foreach ($declared as $className) {
$rc = new \ReflectionClass($className);
$sourceFile = $rc->getFileName();
if (in_array($sourceFile, $includedFiles) && !$this->isTransient($className)) {
+ if ($mustFilter && !in_array($className, $classNames)) {
+ continue;
+ }
+
$classes[] = $className;
}
}
View
29 tests/Doctrine/Tests/Common/Persistence/Mapping/ChainDriverTest.php
@@ -75,6 +75,35 @@ public function testGatherAllClassNames()
), $chain->getAllClassNames());
}
+ public function testGatherAllClassNamesFilteringResults()
+ {
+ $className = 'Doctrine\Tests\Common\Persistence\Mapping\DriverChainEntity';
+ $classMetadata = $this->getMock('Doctrine\Common\Peristence\ClassMetadata');
+
+ $chain = new MappingDriverChain();
+ $filter = array('Doctrine\Tests\Models\Company\Foo', 'Doctrine\Tests\ORM\Mapping\Bar');
+
+ $driver1 = $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');
+ $driver1->expects($this->once())
+ ->method('getAllClassNames')
+ ->with($filter)
+ ->will($this->returnValue(array('Doctrine\Tests\Models\Company\Foo')));
+
+ $driver2 = $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');
+ $driver2->expects($this->once())
+ ->method('getAllClassNames')
+ ->with($filter)
+ ->will($this->returnValue(array('Doctrine\Tests\ORM\Mapping\Bar')));
+
+ $chain->addDriver($driver1, 'Doctrine\Tests\Models\Company');
+ $chain->addDriver($driver2, 'Doctrine\Tests\ORM\Mapping');
+
+ $this->assertEquals(array(
+ 'Doctrine\Tests\Models\Company\Foo',
+ 'Doctrine\Tests\ORM\Mapping\Bar'
+ ), $chain->getAllClassNames($filter));
+ }
+
/**
* @group DDC-706
*/
View
46 tests/Doctrine/Tests/Common/Persistence/Mapping/ClassMetadataFactoryTest.php
@@ -3,7 +3,6 @@
namespace Doctrine\Tests\Common\Persistence\Mapping;
use Doctrine\Tests\DoctrineTestCase;
-use Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator;
use Doctrine\Common\Persistence\Mapping\ReflectionService;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
@@ -23,14 +22,6 @@ public function setUp()
$this->cmf = new TestClassMetadataFactory($driver, $metadata);
}
- public function testGetCacheDriver()
- {
- $this->assertNull($this->cmf->getCacheDriver());
- $cache = new ArrayCache();
- $this->cmf->setCacheDriver($cache);
- $this->assertSame($cache, $this->cmf->getCacheDriver());
- }
-
public function testGetMetadataFor()
{
$metadata = $this->cmf->getMetadataFor('stdClass');
@@ -77,6 +68,43 @@ public function testGetAliasedMetadata()
$this->assertTrue($this->cmf->hasMetadataFor(__NAMESPACE__ . '\ChildEntity'));
$this->assertTrue($this->cmf->hasMetadataFor('prefix:ChildEntity'));
}
+
+ /**
+ * @covers Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory::getAllMetadata
+ */
+ public function testGetAllMetadata()
+ {
+ $driver = $this->cmf->driver;
+ $driver
+ ->expects($this->once())
+ ->method($this->equalTo('getAllClassNames'))
+ ->will($this->returnValue(array(__NAMESPACE__ . '\RootEntity', __NAMESPACE__ . '\ChildEntity')))
+ ;
+
+ $metadata = $this->cmf->getAllMetadata();
+ $this->assertCount(2, $metadata);
+ $this->assertTrue($this->cmf->hasMetadataFor(__NAMESPACE__ . '\RootEntity'));
+ $this->assertTrue($this->cmf->hasMetadataFor(__NAMESPACE__ . '\ChildEntity'));
+ }
+
+ /**
+ * @covers Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory::getAllMetadata
+ */
+ public function testGetAllMetadataWithFilter()
+ {
+ $driver = $this->cmf->driver;
+ $driver
+ ->expects($this->once())
+ ->method($this->equalTo('getAllClassNames'))
+ ->with($this->equalTo(array(__NAMESPACE__ . '\RootEntity')))
+ ->will($this->returnValue(array(__NAMESPACE__ . '\RootEntity')))
+ ;
+
+ $metadata = $this->cmf->getAllMetadata(array(__NAMESPACE__ . '\RootEntity'));
+ $this->assertCount(1, $metadata);
+ $this->assertTrue($this->cmf->hasMetadataFor(__NAMESPACE__ . '\RootEntity'));
+ $this->assertFalse($this->cmf->hasMetadataFor(__NAMESPACE__ . '\ChildEntity'));
+ }
}
class TestClassMetadataFactory extends AbstractClassMetadataFactory
View
17 tests/Doctrine/Tests/Common/Persistence/Mapping/FileDriverTest.php
@@ -80,6 +80,23 @@ public function testGetAllClassNamesBothSources()
$this->assertEquals(array('stdGlobal', 'stdGlobal2', 'stdClass'), $classNames);
}
+ public function testGetAllClassNamesBothSourcesWithFilteredResults()
+ {
+ $locator = $this->newLocator();
+ $locator->expects($this->any())
+ ->method('getAllClassNames')
+ ->with($this->equalTo('global'))
+ ->will($this->returnValue(array('stdClass')));
+
+ $driver = new TestFileDriver($locator);
+ $driver->setGlobalBasename("global");
+
+ $classNames = $driver->getAllClassNames(array('stdGlobal', 'stdGlobal2'));
+
+ $this->assertCount(2, $classNames);
+ $this->assertEquals(array('stdGlobal', 'stdGlobal2'), $classNames);
+ }
+
public function testIsNotTransient()
{
$locator = $this->newLocator();
View
9 tests/Doctrine/Tests/Common/Persistence/Mapping/StaticPHPDriverTest.php
@@ -24,6 +24,15 @@ public function testGetAllClassNames()
$this->assertContains(
'Doctrine\Tests\Common\Persistence\Mapping\TestEntity', $classNames);
}
+
+ public function testGetAllClassNamesFiltered()
+ {
+ $driver = new StaticPHPDriver(array(__DIR__));
+ $classNames = $driver->getAllClassNames(array('Doctrine\Tests\Common\Persistence\Mapping\TestEntity'));
+
+ $this->assertCount(1, $classNames);
+ $this->assertContains('Doctrine\Tests\Common\Persistence\Mapping\TestEntity', $classNames);
+ }
}
class TestEntity