1
0
mirror of synced 2024-12-05 03:06:05 +03:00

Remove support to pass arguments to custom ID generator's constructor

This commit is contained in:
Vitali Yakavenka 2012-01-08 15:20:35 +03:00
parent b09201ae88
commit 53ecedf70a
12 changed files with 13 additions and 63 deletions

View File

@ -276,13 +276,7 @@
<xs:complexType name="custom-id-generator">
<xs:sequence>
<xs:element name="args" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="arg" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other"/>
</xs:sequence>
<xs:attribute name="class" type="xs:NMTOKEN" use="required" />
</xs:complexType>

View File

@ -520,16 +520,11 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
break;
case ClassMetadata::GENERATOR_TYPE_CUSTOM:
$definition = $class->customGeneratorDefinition;
try {
$reflection = new \ReflectionClass($definition['class']);
$args = isset($definition['args']) ?
$definition['args'] : array();
$generator = $reflection->newInstanceArgs($args);
$class->setIdGenerator($generator);
} catch (ReflectionException $e) {
if (!class_exists($definition['class'])) {
throw new ORMException("Can't instantiate custom generator : " .
$definition['class']);
}
$class->setIdGenerator(new $definition['class']);
break;
default:
throw new ORMException("Unknown generator type: " . $class->generatorType);

View File

@ -188,7 +188,6 @@ class ClassMetadataInfo implements ClassMetadata
* <code>
* array(
* 'class' => 'ClassName',
* 'args' => array("constructor", "arguments")
* )
* </code>
*

View File

@ -27,6 +27,4 @@ final class CustomIdGenerator implements Annotation
{
/** @var string */
public $class;
/** @var array */
public $args;
}

View File

@ -328,8 +328,7 @@ class AnnotationDriver implements Driver
throw MappingException::tableIdGeneratorNotImplemented($className);
} else if ($customGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\CustomIdGenerator')) {
$metadata->setCustomGeneratorDefinition(array(
'class' => $customGeneratorAnnot->class,
'args' => $customGeneratorAnnot->args
'class' => $customGeneratorAnnot->class
));
}
} else if ($oneToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToOne')) {

View File

@ -256,13 +256,8 @@ class XmlDriver extends AbstractFileDriver
));
} else if (isset($idElement->{'custom-id-generator'})) {
$customGenerator = $idElement->{'custom-id-generator'};
$args = array();
foreach ($customGenerator->args->children() as $argument) {
$args[] = (string) $argument;
}
$metadata->setCustomGeneratorDefinition(array(
'class' => (string) $customGenerator['class'],
'args' => $args
'class' => (string) $customGenerator['class']
));
} else if (isset($idElement->{'table-generator'})) {
throw MappingException::tableIdGeneratorNotImplemented($className);

View File

@ -198,8 +198,7 @@ class YamlDriver extends AbstractFileDriver
} else if (isset($idElement['customIdGenerator'])) {
$customGenerator = $idElement['customIdGenerator'];
$metadata->setCustomGeneratorDefinition(array(
'class' => (string) $customGenerator['class'],
'args' => $customGenerator['args']
'class' => (string) $customGenerator['class']
));
} else if (isset($idElement['tableGenerator'])) {
throw MappingException::tableIdGeneratorNotImplemented($className);

View File

@ -97,7 +97,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_CUSTOM,
$class->generatorType, "Generator Type");
$this->assertEquals(
array("class" => "stdClass", "args" => array("par1", "par2")),
array("class" => "stdClass"),
$class->customGeneratorDefinition,
"Custom Generator Definition");
}
@ -627,14 +627,14 @@ abstract class Animal
{
/**
* @Id @Column(type="string") @GeneratedValue(strategy="CUSTOM")
* @CustomIdGenerator(class="stdClass", args={"par1", "par2"})
* @CustomIdGenerator(class="stdClass")
*/
public $id;
public static function loadMetadata(ClassMetadataInfo $metadata)
{
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_CUSTOM);
$metadata->setCustomGeneratorDefinition(array("class" => "stdClass", "args" => array("par1", "par2")));
$metadata->setCustomGeneratorDefinition(array("class" => "stdClass"));
}
}

View File

@ -66,24 +66,6 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$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();
@ -261,11 +243,6 @@ 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)
{
}

View File

@ -27,4 +27,4 @@ $metadata->mapField(array(
'columnName' => 'id',
));
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_CUSTOM);
$metadata->setCustomGeneratorDefinition(array("class" => "stdClass", "args" => array("par1", "par2")));
$metadata->setCustomGeneratorDefinition(array("class" => "stdClass"));

View File

@ -9,13 +9,8 @@
<discriminator-mapping value="dog" class="Dog" />
</discriminator-map>
<id name="id" type="integer" column="id">
<generator strategy="CUSTOM"/>
<custom-id-generator class="stdClass">
<args>
<arg>par1</arg>
<arg>par2</arg>
</args>
</custom-id-generator>
<generator strategy="CUSTOM" />
<custom-id-generator class="stdClass" />
</id>
</entity>
</doctrine-mapping>

View File

@ -10,5 +10,4 @@ Doctrine\Tests\ORM\Mapping\Animal:
generator:
strategy: CUSTOM
customIdGenerator:
class: stdClass
args: [ par1, par2 ]
class: stdClass