From 7e54ae370249dcabed7a2971fbb048d8f120ca0a Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Tue, 31 Jul 2012 22:20:21 -0300 Subject: [PATCH] xml driver --- .../ORM/Mapping/ClassMetadataInfo.php | 10 +++++ .../ORM/Mapping/Driver/AnnotationDriver.php | 14 +++---- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php | 15 +++++++ lib/Doctrine/ORM/Mapping/MappingException.php | 29 +++++++++++++ .../ORM/Mapping/AbstractMappingDriverTest.php | 6 ++- ...sts.Models.Company.CompanyContract.dcm.xml | 41 +++++++++++++++++++ ....Models.Company.CompanyFixContract.dcm.xml | 16 ++++++++ ...Models.Company.CompanyFlexContract.dcm.xml | 16 ++++++++ ...s.Company.CompanyFlexUltraContract.dcm.xml | 36 ++++++++++++++++ 9 files changed, 174 insertions(+), 9 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyContract.dcm.xml create mode 100644 tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFixContract.dcm.xml create mode 100644 tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFlexContract.dcm.xml create mode 100644 tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFlexUltraContract.dcm.xml diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index f99ab54c0..51a04aa3f 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -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 diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index eaad6576b..30f664104 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -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]); } } } diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index b8b5e13b2..f22db0745 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -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); + } + } + } } /** diff --git a/lib/Doctrine/ORM/Mapping/MappingException.php b/lib/Doctrine/ORM/Mapping/MappingException.php index 3c32eebd5..a2688a177 100644 --- a/lib/Doctrine/ORM/Mapping/MappingException.php +++ b/lib/Doctrine/ORM/Mapping/MappingException.php @@ -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 diff --git a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php index 822872651..5c6e04a69 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php @@ -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(); } diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyContract.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyContract.dcm.xml new file mode 100644 index 000000000..990b519b6 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyContract.dcm.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFixContract.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFixContract.dcm.xml new file mode 100644 index 000000000..bd1019d68 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFixContract.dcm.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFlexContract.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFlexContract.dcm.xml new file mode 100644 index 000000000..91ef0bb1c --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFlexContract.dcm.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFlexUltraContract.dcm.xml b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFlexUltraContract.dcm.xml new file mode 100644 index 000000000..8b0075367 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/xml/Doctrine.Tests.Models.Company.CompanyFlexUltraContract.dcm.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file