diff --git a/lib/Doctrine/ORM/Mapping/Driver/DriverChain.php b/lib/Doctrine/ORM/Mapping/Driver/DriverChain.php index 321962d2c..da30f177a 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/DriverChain.php +++ b/lib/Doctrine/ORM/Mapping/Driver/DriverChain.php @@ -39,7 +39,34 @@ class DriverChain implements Driver /** * @var array */ - private $_drivers = array(); + private $drivers = array(); + + /** + * The default driver + * + * @var Driver + */ + private $defaultDriver; + + /** + * Get the default driver. + * + * @return Driver + */ + public function getDefaultDriver() + { + return $this->defaultDriver; + } + + /** + * Set the default driver. + * + * @param Driver $driver + */ + public function setDefaultDriver(Driver $driver) + { + $this->defaultDriver = $driver; + } /** * Add a nested driver. @@ -49,7 +76,7 @@ class DriverChain implements Driver */ public function addDriver(Driver $nestedDriver, $namespace) { - $this->_drivers[$namespace] = $nestedDriver; + $this->drivers[$namespace] = $nestedDriver; } /** @@ -59,7 +86,7 @@ class DriverChain implements Driver */ public function getDrivers() { - return $this->_drivers; + return $this->drivers; } /** @@ -70,13 +97,18 @@ class DriverChain implements Driver */ public function loadMetadataForClass($className, ClassMetadataInfo $metadata) { - foreach ($this->_drivers as $namespace => $driver) { + foreach ($this->drivers as $namespace => $driver) { if (strpos($className, $namespace) === 0) { $driver->loadMetadataForClass($className, $metadata); return; } } + if ($this->defaultDriver !== null) { + $this->defaultDriver->loadMetadataForClass($className, $metadata); + return; + } + throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); } @@ -89,7 +121,7 @@ class DriverChain implements Driver { $classNames = array(); $driverClasses = array(); - foreach ($this->_drivers AS $namespace => $driver) { + foreach ($this->drivers AS $namespace => $driver) { $oid = spl_object_hash($driver); if (!isset($driverClasses[$oid])) { $driverClasses[$oid] = $driver->getAllClassNames(); @@ -114,12 +146,16 @@ class DriverChain implements Driver */ public function isTransient($className) { - foreach ($this->_drivers AS $namespace => $driver) { + foreach ($this->drivers AS $namespace => $driver) { if (strpos($className, $namespace) === 0) { return $driver->isTransient($className); } } + if ($this->defaultDriver !== null) { + return $this->defaultDriver->isTransient($className); + } + // class isTransient, i.e. not an entity or mapped superclass return true; } diff --git a/tests/Doctrine/Tests/ORM/Mapping/DriverChainTest.php b/tests/Doctrine/Tests/ORM/Mapping/DriverChainTest.php index 2383db64c..35560f35a 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/DriverChainTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/DriverChainTest.php @@ -88,6 +88,42 @@ class DriverChainTest extends \Doctrine\Tests\OrmTestCase $this->assertTrue($chain->isTransient('stdClass'), "stdClass isTransient"); $this->assertFalse($chain->isTransient('Doctrine\Tests\Models\CMS\CmsUser'), "CmsUser is not Transient"); } + + /** + * @group DDC-1412 + */ + public function testDefaultDriver() + { + $companyDriver = $this->getMock('Doctrine\ORM\Mapping\Driver\Driver'); + $dafaultDriver = $this->getMock('Doctrine\ORM\Mapping\Driver\Driver'); + $entityClassName = 'Doctrine\Tests\ORM\Mapping\DriverChainEntity'; + $managerClassName = 'Doctrine\Tests\Models\Company\CompanyManager'; + $chain = new DriverChain(); + + $companyDriver->expects($this->never()) + ->method('loadMetadataForClass'); + $companyDriver->expects($this->once()) + ->method('isTransient') + ->with($this->equalTo($managerClassName)) + ->will($this->returnValue(false)); + + $dafaultDriver->expects($this->never()) + ->method('loadMetadataForClass'); + $dafaultDriver->expects($this->once()) + ->method('isTransient') + ->with($this->equalTo($entityClassName)) + ->will($this->returnValue(true)); + + $this->assertNull($chain->getDefaultDriver()); + + $chain->setDefaultDriver($dafaultDriver); + $chain->addDriver($companyDriver, 'Doctrine\Tests\Models\Company'); + + $this->assertSame($dafaultDriver, $chain->getDefaultDriver()); + + $this->assertTrue($chain->isTransient($entityClassName)); + $this->assertFalse($chain->isTransient($managerClassName)); + } } class DriverChainEntity