1
0
mirror of synced 2025-02-20 06:03:15 +03:00

xml driver

This commit is contained in:
Fabio B. Silva 2012-07-31 22:20:21 -03:00 committed by fabio.silva
parent 6be7a03b72
commit 7e54ae3702
9 changed files with 174 additions and 9 deletions

View File

@ -2499,6 +2499,16 @@ class ClassMetadataInfo implements ClassMetadata
*/
public function addEntityListener($eventName, $class, $method)
{
$class = $this->fullyQualifiedClassName($class);
if ( ! class_exists($class)) {
throw MappingException::entityListenerClassNotFound($class, $this->name);
}
if ( !method_exists($class, $method)) {
throw MappingException::entityListenerMethodNotFound($class, $method, $this->name);
}
$this->entityListeners[$eventName][] = array(
'class' => $class,
'method' => $method

View File

@ -419,19 +419,19 @@ class AnnotationDriver extends AbstractAnnotationDriver
if (isset($classAnnotations['Doctrine\ORM\Mapping\EntityListeners'])) {
$entityListenersAnnot = $classAnnotations['Doctrine\ORM\Mapping\EntityListeners'];
foreach ($entityListenersAnnot->value as $listener) {
$listener = $metadata->fullyQualifiedClassName($listener);
foreach ($entityListenersAnnot->value as $item) {
$listenerClassName = $metadata->fullyQualifiedClassName($item);
if ( ! class_exists($listener)) {
throw new \InvalidArgumentException("Indefined class \"$listener\"");
if ( ! class_exists($listenerClassName)) {
throw MappingException::entityListenerClassNotFound($listenerClassName, $className);
}
$listener = new \ReflectionClass($listener);
$listenerClass = new \ReflectionClass($listenerClassName);
/* @var $method \ReflectionMethod */
foreach ($listener->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
foreach ($listenerClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
// find method callbacks.
foreach ($this->getMethodCallbacks($method) as $value) {
$metadata->addEntityListener($value[1], $listener->name, $value[0]);
$metadata->addEntityListener($value[1], $listenerClassName, $value[0]);
}
}
}

View File

@ -556,6 +556,21 @@ class XmlDriver extends FileDriver
$metadata->addLifecycleCallback((string)$lifecycleCallback['method'], constant('Doctrine\ORM\Events::' . (string)$lifecycleCallback['type']));
}
}
// Evaluate entity listener
if (isset($xmlRoot->{'entity-listeners'})) {
foreach ($xmlRoot->{'entity-listeners'}->{'entity-listener'} as $listenerElement) {
$className = (string) $listenerElement['class'];
foreach ($listenerElement as $type => $callbackElement) {
list($prefix, $suffix) = explode('-', $type);
$eventName = $prefix . ucfirst($suffix);
$methodName = (string) $callbackElement['method'];
$metadata->addEntityListener($eventName, $className, $methodName);
}
}
}
}
/**

View File

@ -684,6 +684,35 @@ class MappingException extends \Doctrine\ORM\ORMException
return new self("Entity '" . $className . "' has no method '" . $methodName . "' to be registered as lifecycle callback.");
}
/**
* @param string $className
* @param string $methodName
*
* @return \Doctrine\ORM\Mapping\MappingException
*/
public static function entityListenerClassNotFound($listenerName, $className)
{
return new self(sprintf(
'Entity Listener "%s" declared on "%s" not found.',
$listenerName, $className
));
}
/**
* @param string $listenerName
* @param string $methodName
* @param string $className
*
* @return \Doctrine\ORM\Mapping\MappingException
*/
public static function entityListenerMethodNotFound($listenerName, $methodName, $className)
{
return new self(sprintf(
'Entity Listener "%s" declared on "%s" has no method "%s".',
$listenerName, $className, $methodName
));
}
/**
* @param string $className
* @param string $annotation

View File

@ -758,7 +758,8 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
*/
public function testEntityListeners()
{
if ( ! ($this instanceof AnnotationDriverTest)) {
if ( ! ($this instanceof AnnotationDriverTest)
&& ! ($this instanceof XmlMappingDriverTest)) {
$this->markTestIncomplete();
}
@ -816,7 +817,8 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
*/
public function testCallEntityListeners()
{
if ( ! ($this instanceof AnnotationDriverTest)) {
if ( ! ($this instanceof AnnotationDriverTest)
&& ! ($this instanceof XmlMappingDriverTest)) {
$this->markTestIncomplete();
}

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\Company\CompanyContract" table="company_contracts" inheritance-type="SINGLE_TABLE">
<discriminator-map>
<discriminator-mapping value="fix" class="CompanyFixContract" />
<discriminator-mapping value="flexible" class="CompanyFlexContract" />
<discriminator-mapping value="flexultra" class="CompanyFlexUltraContract" />
</discriminator-map>
<entity-listeners>
<entity-listener class="ContractSubscriber">
<pre-flush method="preFlushHandler"/>
<post-load method="postLoadHandler"/>
<post-persist method="postPersistHandler"/>
<pre-persist method="prePersistHandler"/>
<post-update method="postPersistHandler"/>
<pre-update method="preUpdateHandler"/>
<post-remove method="postRemoveHandler"/>
<pre-remove method="preRemoveHandler"/>
</entity-listener>
</entity-listeners>
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="completed" column="completed" type="boolean"/>
<!-- Other mappings -->
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\Company\CompanyFixContract">
<field name="fixPrice" column="fixPrice" type="integer"/>
<!-- Other mappings -->
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\Company\CompanyFlexContract">
<field name="hoursWorked" column="hoursWorked" type="integer"/>
<field name="pricePerHour" column="pricePerHour" type="integer"/>
<!-- Other mappings -->
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\Company\CompanyFlexUltraContract">
<entity-listeners>
<entity-listener class="ContractSubscriber">
<pre-flush method="preFlushHandler"/>
<post-load method="postLoadHandler"/>
<post-persist method="postPersistHandler"/>
<pre-persist method="prePersistHandler"/>
<post-update method="postPersistHandler"/>
<pre-update method="preUpdateHandler"/>
<post-remove method="postRemoveHandler"/>
<pre-remove method="preRemoveHandler"/>
</entity-listener>
<entity-listener class="FlexUltraContractSubscriber">
<pre-persist method="prePersistHandler1"/>
<pre-persist method="prePersistHandler2"/>
</entity-listener>
</entity-listeners>
<field name="maxPrice" column="maxPrice" type="integer"/>
<!-- Other mappings -->
</entity>
</doctrine-mapping>