Pass specified arguments to generator's constructor
This commit is contained in:
parent
cd0915deb5
commit
82daf651fb
@ -506,10 +506,14 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
|
||||
break;
|
||||
case ClassMetadata::GENERATOR_TYPE_CUSTOM:
|
||||
$definition = $class->customGeneratorDefinition;
|
||||
if (class_exists($definition['class'])) {
|
||||
$class->setIdGenerator(new $definition['class']);
|
||||
} else {
|
||||
throw new ORMException("Can't find custom generator class: " .
|
||||
try {
|
||||
$reflection = new \ReflectionClass($definition['class']);
|
||||
$args = isset($definition['args']) ?
|
||||
$definition['args'] : array();
|
||||
$generator = $reflection->newInstanceArgs($args);
|
||||
$class->setIdGenerator($generator);
|
||||
} catch (ReflectionException $e) {
|
||||
throw new ORMException("Can't instantiate custom generator : " .
|
||||
$definition['class']);
|
||||
}
|
||||
break;
|
||||
|
@ -180,6 +180,23 @@ class ClassMetadataInfo implements ClassMetadata
|
||||
*/
|
||||
public $rootEntityName;
|
||||
|
||||
/**
|
||||
* READ-ONLY: The definition of custom generator. Only used for CUSTOM
|
||||
* generator type
|
||||
*
|
||||
* The definition has the following structure:
|
||||
* <code>
|
||||
* array(
|
||||
* 'class' => 'ClassName',
|
||||
* 'args' => array("constructor", "arguments")
|
||||
* )
|
||||
* </code>
|
||||
*
|
||||
* @var array
|
||||
* @todo Merge with tableGeneratorDefinition into generic generatorDefinition
|
||||
*/
|
||||
public $customGeneratorDefinition;
|
||||
|
||||
/**
|
||||
* The name of the custom repository class used for the entity class.
|
||||
* (Optional).
|
||||
|
@ -61,10 +61,27 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
|
||||
|
||||
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_CUSTOM,
|
||||
$actual->generatorType);
|
||||
$this->assertInstanceOf("Doctrine\Tests\ORM\Mapping\CustomIdGenerator",
|
||||
$this->assertInstanceOf("Doctrine\Tests\ORM\Mapping\CustomIdGenerator",
|
||||
$actual->idGenerator);
|
||||
}
|
||||
|
||||
public function testGetMetadataFor_PasesArgumentsToGeneratorsConstructor() {
|
||||
$cm1 = $this->_createValidClassMetadata();
|
||||
$cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_CUSTOM);
|
||||
$cm1->customGeneratorDefinition = array(
|
||||
"class" => "Doctrine\Tests\ORM\Mapping\CustomIdGenerator",
|
||||
"args" => array("parameter"));
|
||||
$cmf = $this->_createTestFactory();
|
||||
$cmf->setMetadataForClass($cm1->name, $cm1);
|
||||
$expected = new CustomIdGenerator("parameter");
|
||||
|
||||
$actual = $cmf->getMetadataFor($cm1->name);
|
||||
|
||||
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_CUSTOM,
|
||||
$actual->generatorType);
|
||||
$this->assertEquals($expected, $actual->idGenerator);
|
||||
}
|
||||
|
||||
public function testGetMetadataFor_ThrowsExceptionOnUnknownCustomGeneratorClass() {
|
||||
$cm1 = $this->_createValidClassMetadata();
|
||||
$cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_CUSTOM);
|
||||
@ -75,6 +92,16 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
|
||||
|
||||
$actual = $cmf->getMetadataFor($cm1->name);
|
||||
}
|
||||
|
||||
public function testGetMetadataFor_ThrowsExceptionOnMissingCustomGeneratorDefinition() {
|
||||
$cm1 = $this->_createValidClassMetadata();
|
||||
$cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_CUSTOM);
|
||||
$cmf = $this->_createTestFactory();
|
||||
$cmf->setMetadataForClass($cm1->name, $cm1);
|
||||
$this->setExpectedException("Doctrine\ORM\ORMException");
|
||||
|
||||
$actual = $cmf->getMetadataFor($cm1->name);
|
||||
}
|
||||
|
||||
public function testHasGetMetadata_NamespaceSeperatorIsNotNormalized()
|
||||
{
|
||||
@ -227,5 +254,9 @@ class TestEntity1
|
||||
}
|
||||
|
||||
class CustomIdGenerator extends \Doctrine\ORM\Id\AbstractIdGenerator {
|
||||
public $parameter;
|
||||
public function __construct($parameter = null) {
|
||||
$this->parameter = $parameter;
|
||||
}
|
||||
public function generate(\Doctrine\ORM\EntityManager $em, $entity) {}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user