Pārlūkot izejas kodu

[mapping] made configurations static, to access from other listeners

gedi 13 gadi atpakaļ
vecāks
revīzija
cb2cd245e0

+ 2 - 2
lib/Gedmo/Loggable/LoggableListener.php

@@ -98,8 +98,8 @@ class LoggableListener extends MappedEventSubscriber
      */
     protected function getLogEntryClass(LoggableAdapter $ea, $class)
     {
-        return isset($this->configurations[$class]['logEntryClass']) ?
-            $this->configurations[$class]['logEntryClass'] :
+        return isset(self::$configurations[$this->name][$class]['logEntryClass']) ?
+            self::$configurations[$this->name][$class]['logEntryClass'] :
             $ea->getDefaultLogEntryClass();
     }
 

+ 25 - 8
lib/Gedmo/Mapping/MappedEventSubscriber.php

@@ -29,11 +29,20 @@ use Doctrine\Common\EventArgs;
 abstract class MappedEventSubscriber implements EventSubscriber
 {
     /**
-     * List of cached object configurations
+     * Static List of cached object configurations
+     * leaving it static for reasons to look into
+     * other listener configuration
      *
      * @var array
      */
-    protected $configurations = array();
+    protected static $configurations = array();
+
+    /**
+     * Listener name, etc: sluggable
+     *
+     * @var string
+     */
+    protected $name;
 
     /**
      * ExtensionMetadataFactory used to read the extension
@@ -62,6 +71,14 @@ abstract class MappedEventSubscriber implements EventSubscriber
      */
     private static $defaultAnnotationReader;
 
+    /**
+     * Constructor
+     */
+    public function __construct()
+    {
+        $this->name = end(explode('\\', $this->getNamespace()));
+    }
+
     /**
      * Get an event adapter to handle event specific
      * methods
@@ -98,21 +115,21 @@ abstract class MappedEventSubscriber implements EventSubscriber
      */
     public function getConfiguration(ObjectManager $objectManager, $class) {
         $config = array();
-        if (isset($this->configurations[$class])) {
-            $config = $this->configurations[$class];
+        if (isset(self::$configurations[$this->name][$class])) {
+            $config = self::$configurations[$this->name][$class];
         } else {
             $factory = $objectManager->getMetadataFactory();
             $cacheDriver = $factory->getCacheDriver();
             if ($cacheDriver) {
                 $cacheId = ExtensionMetadataFactory::getCacheId($class, $this->getNamespace());
                 if (($cached = $cacheDriver->fetch($cacheId)) !== false) {
-                    $this->configurations[$class] = $cached;
+                    self::$configurations[$this->name][$class] = $cached;
                     $config = $cached;
                 } else {
                     // re-generate metadata on cache miss
                     $this->loadMetadataForObjectClass($objectManager, $factory->getMetadataFor($class));
-                    if (isset($this->configurations[$class])) {
-                        $config = $this->configurations[$class];
+                    if (isset(self::$configurations[$this->name][$class])) {
+                        $config = self::$configurations[$this->name][$class];
                     }
                 }
             }
@@ -172,7 +189,7 @@ abstract class MappedEventSubscriber implements EventSubscriber
         $factory = $this->getExtensionMetadataFactory($objectManager);
         $config = $factory->getExtensionMetadata($metadata);
         if ($config) {
-            $this->configurations[$metadata->name] = $config;
+            self::$configurations[$this->name][$metadata->name] = $config;
         }
     }
 

+ 6 - 6
lib/Gedmo/Translatable/TranslatableListener.php

@@ -158,8 +158,8 @@ class TranslatableListener extends MappedEventSubscriber
      */
     public function getTranslationClass(TranslatableAdapter $ea, $class)
     {
-        return isset($this->configurations[$class]['translationClass']) ?
-            $this->configurations[$class]['translationClass'] :
+        return isset(self::$configurations[$this->name][$class]['translationClass']) ?
+            self::$configurations[$this->name][$class]['translationClass'] :
             $ea->getDefaultTranslationClass()
         ;
     }
@@ -250,11 +250,11 @@ class TranslatableListener extends MappedEventSubscriber
     public function getTranslatableLocale($object, $meta)
     {
         $locale = $this->locale;
-        if (isset($this->configurations[$meta->name]['locale'])) {
+        if (isset(self::$configurations[$this->name][$meta->name]['locale'])) {
             $class = $meta->getReflectionClass();
-            $reflectionProperty = $class->getProperty($this->configurations[$meta->name]['locale']);
+            $reflectionProperty = $class->getProperty(self::$configurations[$this->name][$meta->name]['locale']);
             if (!$reflectionProperty) {
-                $column = $this->configurations[$meta->name]['locale'];
+                $column = self::$configurations[$this->name][$meta->name]['locale'];
                 throw new \Gedmo\Exception\RuntimeException("There is no locale or language property ({$column}) found on object: {$meta->name}");
             }
             $reflectionProperty->setAccessible(true);
@@ -322,7 +322,7 @@ class TranslatableListener extends MappedEventSubscriber
         $object = $ea->getObject();
         $meta = $om->getClassMetadata(get_class($object));
         // check if entity is tracked by translatable and without foreign key
-        if (array_key_exists($meta->name, $this->configurations) && count($this->pendingTranslationInserts)) {
+        if ($this->getConfiguration($om, $meta->name) && count($this->pendingTranslationInserts)) {
             $oid = spl_object_hash($object);
             if (array_key_exists($oid, $this->pendingTranslationInserts)) {
                 // load the pending translations without key

+ 1 - 1
lib/Gedmo/Tree/TreeListener.php

@@ -193,7 +193,7 @@ class TreeListener extends MappedEventSubscriber
         $om = $ea->getObjectManager();
         $meta = $eventArgs->getClassMetadata();
         $this->loadMetadataForObjectClass($om, $meta);
-        if (isset($this->configurations[$meta->name]) && $this->configurations[$meta->name]) {
+        if (isset(self::$configurations[$this->name][$meta->name]) && self::$configurations[$this->name][$meta->name]) {
             $this->getStrategy($om, $meta->name)->processMetadataLoad($om, $meta);
         }
     }