Selaa lähdekoodia

removed interface requirement for timestampable by default

gediminasm 14 vuotta sitten
vanhempi
commit
be653ccd48

+ 3 - 2
lib/DoctrineExtensions/Timestampable/Timestampable.php

@@ -3,8 +3,9 @@
 namespace DoctrineExtensions\Timestampable;
 
 /**
- * This interface must be implemented for all entities
- * to activate the Timestampable behavior
+ * This interface is not necessary but can be implemented for
+ * Entities which in some cases needs to be identified as
+ * Timestampable
  * 
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  * @package DoctrineExtensions.Timestampable

+ 51 - 76
lib/DoctrineExtensions/Timestampable/TimestampableListener.php

@@ -42,14 +42,6 @@ class TimestampableListener implements EventSubscriber
         'datetime'
     );
     
-    /**
-     * If set to true it will check only Timestampable
-     * entities for annotations
-     * 
-     * @var boolean
-     */
-    private $_requireInterface = true;
-    
     /**
      * Specifies the list of events to listen
      * 
@@ -64,18 +56,6 @@ class TimestampableListener implements EventSubscriber
         );
     }
     
-    /**
-     * If set to false it will scan all entities
-     * for timestampable annotations
-     * 
-     * @param boolean $bool
-     * @return void
-     */
-    public function setRequiresInterface($bool)
-    {
-        $this->_requireInterface = (boolean)$bool;
-    }
-    
     /**
      * Get the configuration for specific entity class
      * if cache driver is present it scans it also
@@ -114,59 +94,56 @@ class TimestampableListener implements EventSubscriber
         $meta = $eventArgs->getClassMetadata();
         
         $class = $meta->getReflectionClass();
-        if ($class->implementsInterface('DoctrineExtensions\Timestampable\Timestampable') || !$this->_requireInterface) {
-            require_once __DIR__ . '/Mapping/Annotations.php';
-            $reader = new AnnotationReader();
-            $reader->setAnnotationNamespaceAlias(
-                'DoctrineExtensions\Timestampable\Mapping\\', 'Timestampable'
-            );
+        require_once __DIR__ . '/Mapping/Annotations.php';
+        $reader = new AnnotationReader();
+        $reader->setAnnotationNamespaceAlias(
+            'DoctrineExtensions\Timestampable\Mapping\\', 'Timestampable'
+        );
     
-            // property annotations
-            foreach ($class->getProperties() as $property) {
-                // on create
-                $onCreate = $reader->getPropertyAnnotation(
-                    $property, 
-                    'DoctrineExtensions\Timestampable\Mapping\OnCreate'
-                );
-                if ($onCreate) {
-                    $field = $property->getName();
-                    if (!$this->_isValidField($meta, $field)) {
-                        throw Exception::notValidFieldType($field, $meta->name);
-                    }
-                    $this->_configurations[$meta->name]['onCreate'][] = $field;
+        // property annotations
+        foreach ($class->getProperties() as $property) {
+            // on create
+            $onCreate = $reader->getPropertyAnnotation(
+                $property, 
+                'DoctrineExtensions\Timestampable\Mapping\OnCreate'
+            );
+            if ($onCreate) {
+                $field = $property->getName();
+                if (!$this->_isValidField($meta, $field)) {
+                    throw Exception::notValidFieldType($field, $meta->name);
                 }
-                // on update
-                $onUpdate = $reader->getPropertyAnnotation(
-                    $property, 
-                    'DoctrineExtensions\Timestampable\Mapping\OnUpdate'
-                );
-                if ($onUpdate) {
-                    $field = $property->getName();
-                    if (!$this->_isValidField($meta, $field)) {
-                        throw Exception::notValidFieldType($field, $meta->name);
-                    }
-                    $this->_configurations[$meta->name]['onUpdate'][] = $field;
+                $this->_configurations[$meta->name]['onCreate'][] = $field;
+            }
+            // on update
+            $onUpdate = $reader->getPropertyAnnotation(
+                $property, 
+                'DoctrineExtensions\Timestampable\Mapping\OnUpdate'
+            );
+            if ($onUpdate) {
+                $field = $property->getName();
+                if (!$this->_isValidField($meta, $field)) {
+                    throw Exception::notValidFieldType($field, $meta->name);
                 }
-                
-                // on change
-                $onChange = $reader->getPropertyAnnotation(
-                    $property, 
-                    'DoctrineExtensions\Timestampable\Mapping\OnChange'
-                );
-                if ($onChange) {
-                    $field = $property->getName();
-                    if (!$this->_isValidField($meta, $field)) {
-                        throw Exception::notValidFieldType($field, $meta->name);
-                    }
-                    if (isset($onChange->field) && isset($onChange->value)) {
-                        $this->_configurations[$meta->name]['onChange'][] = array(
-                            'field' => $field,
-                            'trackedField' => $onChange->field,
-                            'value' => $onChange->value 
-                        );
-                    } else {
-                        throw Exception::parametersMissing($meta->name);
-                    }
+                $this->_configurations[$meta->name]['onUpdate'][] = $field;
+            }    
+            // on change
+            $onChange = $reader->getPropertyAnnotation(
+                $property, 
+                'DoctrineExtensions\Timestampable\Mapping\OnChange'
+            );
+            if ($onChange) {
+                $field = $property->getName();
+                if (!$this->_isValidField($meta, $field)) {
+                    throw Exception::notValidFieldType($field, $meta->name);
+                }
+                if (isset($onChange->field) && isset($onChange->value)) {
+                    $this->_configurations[$meta->name]['onChange'][] = array(
+                        'field' => $field,
+                        'trackedField' => $onChange->field,
+                        'value' => $onChange->value 
+                    );
+                } else {
+                    throw Exception::parametersMissing($meta->name);
                 }
             }
         }
@@ -192,12 +169,11 @@ class TimestampableListener implements EventSubscriber
         $uow = $em->getUnitOfWork();
         // check all scheduled updates
         foreach ($uow->getScheduledEntityUpdates() as $entity) {
-            if ($entity instanceof Timestampable || !$this->_requireInterface) {
-                $entityClass = get_class($entity);
+            $entityClass = get_class($entity);
+            if ($config = $this->getConfiguration($em, $entityClass)) {
                 $meta = $em->getClassMetadata($entityClass);
                 $needChanges = false;
                 
-                $config = $this->getConfiguration($em, $entityClass);
                 if (isset($config['onUpdate'])) {
                     $needChanges = true;
                     foreach ($config['onUpdate'] as $field) {
@@ -257,10 +233,9 @@ class TimestampableListener implements EventSubscriber
     {
         $em = $args->getEntityManager();
         $entity = $args->getEntity();
-        $uow = $em->getUnitOfWork();
         
-        if ($entity instanceof Timestampable || !$this->_requireInterface) {
-            $entityClass = get_class($entity);
+        $entityClass = get_class($entity);
+        if ($config = $this->getConfiguration($em, $entityClass)) {
             $meta = $em->getClassMetadata($entityClass);
 
             $config = $this->getConfiguration($em, $entityClass);

+ 0 - 1
tests/DoctrineExtensions/Timestampable/NoInterfaceTest.php

@@ -39,7 +39,6 @@ class NoInterfaceTest extends \PHPUnit_Framework_TestCase
         
         $evm = new \Doctrine\Common\EventManager();
         $timestampableListener = new TimestampableListener();
-        $timestampableListener->setRequiresInterface(false);
         $evm->addEventSubscriber($timestampableListener);
         $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);