Forráskód Böngészése

added an abstraction layer for additional metadata mapping in extension listeners

gediminasm 14 éve
szülő
commit
0ad62b785c

+ 104 - 0
lib/Gedmo/Mapping/MappedEventSubscriber.php

@@ -0,0 +1,104 @@
+<?php
+
+namespace Gedmo\Mapping;
+
+use Gedmo\Mapping\ExtensionMetadataFactory,
+    Doctrine\ORM\EntityManager,
+    Doctrine\ORM\Event\LoadClassMetadataEventArgs;
+
+/**
+ * This is extension of event subscriber class and is
+ * used specifically for handling the extension metadata
+ * mapping for extensions.
+ * 
+ * It dries up some reusable code which is common for
+ * all extensions who mapps additional metadata through
+ * extended drivers
+ * 
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Mapping
+ * @subpackage MappedEventSubscriber
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+abstract class MappedEventSubscriber
+{
+    /**
+     * List of cached object configurations
+     *  
+     * @var array
+     */
+    protected $_configurations = array();
+    
+    /**
+     * ExtensionMetadataFactory used to read the extension
+     * metadata through the extension drivers
+     * 
+     * @var Gedmo\Mapping\ExtensionMetadataFactory
+     */
+    protected $_extensionMetadataFactory = null;
+    
+    /**
+     * Get the namespace of extension event subscriber.
+     * used for loading mapping drivers and cache of
+     * extensions
+     * 
+     * @return string
+     */
+    abstract protected function _getNamespace();
+    
+	/**
+     * Get the configuration for specific object class
+     * if cache driver is present it scans it also
+     * 
+     * @param EntityManager $em
+     * @param string $class
+     * @return array
+     */
+    public function getConfiguration(EntityManager $em, $class) {
+        $config = array();
+        if (isset($this->_configurations[$class])) {
+            $config = $this->_configurations[$class];
+        } else {
+            $cacheDriver = $em->getMetadataFactory()->getCacheDriver();
+            $cacheId = ExtensionMetadataFactory::getCacheId($class, $this->_getNamespace());
+            if (($cached = $cacheDriver->fetch($cacheId)) !== false) {
+                $this->_configurations[$class] = $cached;
+                $config = $cached;
+            }
+        }
+        return $config;
+    }
+    
+	/**
+     * Get extended metadata mapping reader
+     * 
+     * @param EntityManager $em
+     * @return Gedmo\Mapping\ExtensionMetadataFactory
+     */
+    public function getExtensionMetadataFactory(EntityManager $em)
+    {
+        if (null === $this->_extensionMetadataFactory) {
+            $this->_extensionMetadataFactory = new ExtensionMetadataFactory($em, $this->_getNamespace());
+        }
+        return $this->_extensionMetadataFactory;
+    }
+    
+	/**
+     * Scans the objects for extended annotations
+     * event subscribers must subscribe to loadClassMetadata event
+     * 
+     * @param LoadClassMetadataEventArgs $eventArgs
+     * @return void
+     */
+    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
+    {
+        $meta = $eventArgs->getClassMetadata();
+        $em = $eventArgs->getEntityManager();
+        $factory = $this->getExtensionMetadataFactory($em);
+        $config = $factory->getExtensionMetadata($meta);
+        if ($config) {
+            $this->_configurations[$meta->name] = $config;
+        }
+    }
+}

+ 9 - 70
lib/Gedmo/Sluggable/SluggableListener.php

@@ -6,12 +6,11 @@ use Doctrine\Common\EventSubscriber,
     Doctrine\ORM\Events,
     Doctrine\ORM\Event\LifecycleEventArgs,
     Doctrine\ORM\Event\OnFlushEventArgs,
-    Doctrine\ORM\Event\LoadClassMetadataEventArgs,
+    Gedmo\Mapping\MappedEventSubscriber,
     Doctrine\ORM\EntityManager,
     Doctrine\ORM\Query,
     Doctrine\ORM\Mapping\ClassMetadata,
-    Doctrine\ORM\Mapping\ClassMetadataInfo,
-    Gedmo\Mapping\ExtensionMetadataFactory;
+    Doctrine\ORM\Mapping\ClassMetadataInfo;
 
 /**
  * The SluggableListener handles the generation of slugs
@@ -26,23 +25,8 @@ use Doctrine\Common\EventSubscriber,
  * @link http://www.gediminasm.org
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
-class SluggableListener implements EventSubscriber
-{    
-    /**
-     * List of cached entity configurations
-     *  
-     * @var array
-     */
-    protected $_configurations = array();
-    
-    /**
-     * ExtensionMetadataFactory used to read the extension
-     * metadata
-     * 
-     * @var Gedmo\Mapping\ExtensionMetadataFactory
-     */
-    protected $_extensionMetadataFactory = null;
-    
+class SluggableListener extends MappedEventSubscriber implements EventSubscriber
+{
     /**
      * Specifies the list of events to listen
      * 
@@ -56,61 +40,16 @@ class SluggableListener implements EventSubscriber
             Events::loadClassMetadata
         );
     }
-
-    /**
-     * Get the configuration for specific entity class
-     * if cache driver is present it scans it also
-     * 
-     * @param EntityManager $em
-     * @param string $class
-     * @return array
-     */
-    public function getConfiguration(EntityManager $em, $class) {
-        $config = array();
-        if (isset($this->_configurations[$class])) {
-            $config = $this->_configurations[$class];
-        } else {
-            $cacheDriver = $em->getMetadataFactory()->getCacheDriver();
-            $cacheId = ExtensionMetadataFactory::getCacheId($class, __NAMESPACE__);
-            if (($cached = $cacheDriver->fetch($cacheId)) !== false) {
-                $this->_configurations[$class] = $cached;
-                $config = $cached;
-            }
-        }
-        return $config;
-    }
     
     /**
-     * Get metadata mapping reader
+     * Get the namespace of extension event subscriber.
+     * used to load mapping drivers and cache
      * 
-     * @param EntityManager $em
-     * @return Gedmo\Mapping\MetadataReader
+     * @return string
      */
-    public function getExtensionMetadataFactory(EntityManager $em)
+    protected function _getNamespace()
     {
-        if (null === $this->_extensionMetadataFactory) {
-            $this->_extensionMetadataFactory = new ExtensionMetadataFactory($em, __NAMESPACE__);
-        }
-        return $this->_extensionMetadataFactory;
-    }
-    
-    /**
-     * Scans the entities for Sluggable annotations
-     * 
-     * @param LoadClassMetadataEventArgs $eventArgs
-     * @throws Sluggable\Exception if any mapping data is invalid
-     * @throws RuntimeException if ORM version is old
-     * @return void
-     */
-    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
-    {
-        $meta = $eventArgs->getClassMetadata();
-        $em = $eventArgs->getEntityManager();
-        $factory = $this->getExtensionMetadataFactory($em);
-        $config = $factory->getExtensionMetadata($meta);
-        if ($config) {
-            $this->_configurations[$meta->name] = $config;
-        }
+        return __NAMESPACE__;
     }
     
     /**

+ 8 - 68
lib/Gedmo/Timestampable/TimestampableListener.php

@@ -6,7 +6,7 @@ use Doctrine\Common\EventSubscriber,
     Doctrine\ORM\Events,
     Doctrine\ORM\Event\LifecycleEventArgs,
     Doctrine\ORM\Event\OnFlushEventArgs,
-    Doctrine\ORM\Event\LoadClassMetadataEventArgs,
+    Gedmo\Mapping\MappedEventSubscriber,
     Doctrine\ORM\EntityManager,
     Gedmo\Mapping\ExtensionMetadataFactory;
 
@@ -20,24 +20,8 @@ use Doctrine\Common\EventSubscriber,
  * @link http://www.gediminasm.org
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
-class TimestampableListener implements EventSubscriber
+class TimestampableListener extends MappedEventSubscriber implements EventSubscriber
 {
-    /**
-     * List of metadata configurations for Timestampable
-     * classes, from annotations
-     * 
-     * @var array
-     */
-    protected $_configurations = array();
-    
-    /**
-     * ExtensionMetadataFactory used to read the extension
-     * metadata
-     * 
-     * @var Gedmo\Mapping\ExtensionMetadataFactory
-     */
-    protected $_extensionMetadataFactory = null;
-    
     /**
      * Specifies the list of events to listen
      * 
@@ -52,59 +36,15 @@ class TimestampableListener implements EventSubscriber
         );
     }
     
-    /**
-     * Get the configuration for specific entity class
-     * if cache driver is present it scans it also
-     * 
-     * @param EntityManager $em
-     * @param string $class
-     * @return array
-     */
-    public function getConfiguration(EntityManager $em, $class) {
-        $config = array();
-        if (isset($this->_configurations[$class])) {
-            $config = $this->_configurations[$class];
-        } else {
-            $cacheDriver = $em->getMetadataFactory()->getCacheDriver();
-            $cacheId = ExtensionMetadataFactory::getCacheId($class, __NAMESPACE__);
-            if (($cached = $cacheDriver->fetch($cacheId)) !== false) {
-                $this->_configurations[$class] = $cached;
-                $config = $cached;
-            }
-        }
-        return $config;
-    }
-    
-    /**
-     * Get metadata mapping reader
+	/**
+     * Get the namespace of extension event subscriber.
+     * used to load mapping drivers and cache
      * 
-     * @param EntityManager $em
-     * @return Gedmo\Mapping\MetadataReader
+     * @return string
      */
-    public function getExtensionMetadataFactory(EntityManager $em)
+    protected function _getNamespace()
     {
-        if (null === $this->_extensionMetadataFactory) {
-            $this->_extensionMetadataFactory = new ExtensionMetadataFactory($em, __NAMESPACE__);
-        }
-        return $this->_extensionMetadataFactory;
-    }
-    
-    /**
-     * Scans the entities for Timestampable annotations
-     * 
-     * @param LoadClassMetadataEventArgs $eventArgs
-     * @throws RuntimeException if ORM version is old
-     * @return void
-     */
-    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
-    {
-        $meta = $eventArgs->getClassMetadata();
-        $em = $eventArgs->getEntityManager();
-        $factory = $this->getExtensionMetadataFactory($em);
-        $config = $factory->getExtensionMetadata($meta);
-        if ($config) {
-            $this->_configurations[$meta->name] = $config;
-        }
+        return __NAMESPACE__;
     }
     
     /**

+ 9 - 69
lib/Gedmo/Translatable/TranslationListener.php

@@ -6,7 +6,7 @@ use Doctrine\Common\EventSubscriber,
     Doctrine\ORM\Events,
     Doctrine\ORM\Event\LifecycleEventArgs,
     Doctrine\ORM\Event\OnFlushEventArgs,
-    Doctrine\ORM\Event\LoadClassMetadataEventArgs,
+    Gedmo\Mapping\MappedEventSubscriber,
     Doctrine\ORM\EntityManager,
     Doctrine\ORM\Query,
     Doctrine\ORM\Mapping\ClassMetadata,
@@ -32,7 +32,7 @@ use Doctrine\Common\EventSubscriber,
  * @link http://www.gediminasm.org
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
-class TranslationListener implements EventSubscriber
+class TranslationListener extends MappedEventSubscriber implements EventSubscriber
 {    
     /**
      * The translation entity class used to store the translations
@@ -50,22 +50,6 @@ class TranslationListener implements EventSubscriber
      */
     protected $_locale = 'en_us';
     
-    /**
-     * ExtensionMetadataFactory used to read the extension
-     * metadata
-     * 
-     * @var Gedmo\Mapping\ExtensionMetadataFactory
-     */
-    protected $_extensionMetadataFactory = null;
-    
-    /**
-     * List of metadata configurations for Translatable
-     * classes, from annotations
-     * 
-     * @var array
-     */
-    protected $_configurations = array();
-    
     /**
      * Default locale, this changes behavior
      * to not update the original record field if locale
@@ -101,27 +85,15 @@ class TranslationListener implements EventSubscriber
         );
     }
     
-    /**
-     * Get the configuration for specific entity class
-     * if cache driver is present it scans it also
+	/**
+     * Get the namespace of extension event subscriber.
+     * used to load mapping drivers and cache
      * 
-     * @param EntityManager $em
-     * @param string $class
-     * @return array
+     * @return string
      */
-    public function getConfiguration(EntityManager $em, $class) {
-        $config = array();
-        if (isset($this->_configurations[$class])) {
-            $config = $this->_configurations[$class];
-        } else {
-            $cacheDriver = $em->getMetadataFactory()->getCacheDriver();
-            $cacheId = ExtensionMetadataFactory::getCacheId($class, __NAMESPACE__);
-            if (($cached = $cacheDriver->fetch($cacheId)) !== false) {
-                $this->_configurations[$class] = $cached;
-                $config = $cached;
-            }
-        }
-        return $config;
+    protected function _getNamespace()
+    {
+        return __NAMESPACE__;
     }
     
     /**
@@ -191,38 +163,6 @@ class TranslationListener implements EventSubscriber
         $this->_defaultLocale = $locale;
     }
     
-    /**
-     * Get metadata mapping reader
-     * 
-     * @param EntityManager $em
-     * @return Gedmo\Mapping\MetadataReader
-     */
-    public function getExtensionMetadataFactory(EntityManager $em)
-    {
-        if (null === $this->_extensionMetadataFactory) {
-            $this->_extensionMetadataFactory = new ExtensionMetadataFactory($em, __NAMESPACE__);
-        }
-        return $this->_extensionMetadataFactory;
-    }
-    
-    /**
-     * Scans the entities for Translatable extension metadata
-     * 
-     * @param LoadClassMetadataEventArgs $eventArgs
-     * @throws RuntimeException if metadata driver is invalid
-     * @return void
-     */
-    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
-    {   
-        $meta = $eventArgs->getClassMetadata();
-        $em = $eventArgs->getEntityManager();
-        $factory = $this->getExtensionMetadataFactory($em);
-        $config = $factory->getExtensionMetadata($meta);
-        if ($config) {
-            $this->_configurations[$meta->name] = $config;
-        }
-    }
-    
     /**
      * Looks for translatable entities being inserted or updated
      * for further processing

+ 9 - 69
lib/Gedmo/Tree/TreeListener.php

@@ -6,7 +6,7 @@ use Doctrine\Common\EventSubscriber,
     Doctrine\ORM\Events,
     Doctrine\ORM\Event\LifecycleEventArgs,
     Doctrine\ORM\Event\OnFlushEventArgs,
-    Doctrine\ORM\Event\LoadClassMetadataEventArgs,
+    Gedmo\Mapping\MappedEventSubscriber,
     Doctrine\ORM\EntityManager,
     Doctrine\ORM\Query,
     Doctrine\ORM\Mapping\ClassMetadata,
@@ -30,15 +30,8 @@ use Doctrine\Common\EventSubscriber,
  * @link http://www.gediminasm.org
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
-class TreeListener implements EventSubscriber
-{    
-    /**
-     * List of cached entity configurations
-     *  
-     * @var array
-     */
-    protected $_configurations = array();
-    
+class TreeListener extends MappedEventSubscriber implements EventSubscriber
+{
     /**
      * The max number of "right" field of the
      * tree in case few root nodes will be persisted
@@ -57,14 +50,6 @@ class TreeListener implements EventSubscriber
      */
     protected $_pendingChildNodeInserts = array();
     
-    /**
-     * ExtensionMetadataFactory used to read the extension
-     * metadata
-     * 
-     * @var Gedmo\Mapping\ExtensionMetadataFactory
-     */
-    protected $_extensionMetadataFactory = null;
-    
     /**
      * Specifies the list of events to listen
      * 
@@ -81,60 +66,15 @@ class TreeListener implements EventSubscriber
         );
     }
     
-    /**
-     * Get the configuration for specific entity class
-     * if cache driver is present it scans it also
+	/**
+     * Get the namespace of extension event subscriber.
+     * used to load mapping drivers and cache
      * 
-     * @param EntityManager $em
-     * @param string $class
-     * @return array
+     * @return string
      */
-    public function getConfiguration(EntityManager $em, $class) {
-        $config = array();
-        if (isset($this->_configurations[$class])) {
-            $config = $this->_configurations[$class];
-        } else {
-            $cacheDriver = $em->getMetadataFactory()->getCacheDriver();
-            $cacheId = ExtensionMetadataFactory::getCacheId($class, __NAMESPACE__);
-            if (($cached = $cacheDriver->fetch($cacheId)) !== false) {
-                $this->_configurations[$class] = $cached;
-                $config = $cached;
-            }
-        }
-        return $config;
-    }
-    
-    /**
-     * Get metadata mapping reader
-     * 
-     * @param EntityManager $em
-     * @return Gedmo\Mapping\MetadataReader
-     */
-    public function getExtensionMetadataFactory(EntityManager $em)
-    {
-        if (null === $this->_extensionMetadataFactory) {
-            $this->_extensionMetadataFactory = new ExtensionMetadataFactory($em, __NAMESPACE__);
-        }
-        return $this->_extensionMetadataFactory;
-    }
-    
-    /**
-     * Scans the entities for Tree annotations
-     * 
-     * @param LoadClassMetadataEventArgs $eventArgs
-     * @throws Tree\Exception if any mapping data is invalid
-     * @throws RuntimeException if ORM version is old
-     * @return void
-     */
-    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
+    protected function _getNamespace()
     {
-        $meta = $eventArgs->getClassMetadata();
-        $em = $eventArgs->getEntityManager();
-        $factory = $this->getExtensionMetadataFactory($em);
-        $config = $factory->getExtensionMetadata($meta);
-        if ($config) {
-            $this->_configurations[$meta->name] = $config;
-        }
+        return __NAMESPACE__;
     }
     
     /**