Skip to content
Browse files

xml driver

  • Loading branch information...
1 parent 6be7a03 commit 7e54ae370249dcabed7a2971fbb048d8f120ca0a @FabioBatSilva committed with fabio.silva
View
10 lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
@@ -2499,6 +2499,16 @@ public function setLifecycleCallbacks(array $callbacks)
*/
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
14 lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
@@ -419,19 +419,19 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
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
15 lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
@@ -556,6 +556,21 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
$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
29 lib/Doctrine/ORM/Mapping/MappingException.php
@@ -686,6 +686,35 @@ public static function lifecycleCallbackMethodNotFound($className, $methodName)
/**
* @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
*
* @return MappingException
View
6 tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
@@ -758,7 +758,8 @@ public function testAttributeOverridesMapping()
*/
public function testEntityListeners()
{
- if ( ! ($this instanceof AnnotationDriverTest)) {
+ if ( ! ($this instanceof AnnotationDriverTest)
+ && ! ($this instanceof XmlMappingDriverTest)) {
$this->markTestIncomplete();
}
@@ -816,7 +817,8 @@ public function testEntityListeners()
*/
public function testCallEntityListeners()
{
- if ( ! ($this instanceof AnnotationDriverTest)) {
+ if ( ! ($this instanceof AnnotationDriverTest)
+ && ! ($this instanceof XmlMappingDriverTest)) {
$this->markTestIncomplete();
}
View
41 tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyContract.dcm.xml
@@ -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
16 ...s/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFixContract.dcm.xml
@@ -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
16 .../Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFlexContract.dcm.xml
@@ -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
36 ...rine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFlexUltraContract.dcm.xml
@@ -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>

0 comments on commit 7e54ae3

Please sign in to comment.
Something went wrong with that request. Please try again.