Sfoglia il codice sorgente

[SoftDeleteable] Change SoftDeleteableFilter class

GennadyMiheev 13 anni fa
parent
commit
77b99fc177
1 ha cambiato i file con 23 aggiunte e 18 eliminazioni
  1. 23 18
      lib/Gedmo/SoftDeleteable/Filter/SoftDeleteableFilter.php

+ 23 - 18
lib/Gedmo/SoftDeleteable/Filter/SoftDeleteableFilter.php

@@ -20,47 +20,52 @@ use Doctrine\ORM\Mapping\ClassMetaData,
 
 class SoftDeleteableFilter extends SQLFilter
 {
-    protected $configuration;
-
+    protected $listener;
+    protected $entityManager;
 
     public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
     {
-        $config = $this->getConfiguration($targetEntity);
+        $config = $this->getListener()->getConfiguration($this->getEntityManager(), $targetEntity->name);
 
-        if (empty($config['softDeleteable']) || $config['useObjectClass'] !== $targetEntity->name) {
+        if (!isset($config['softDeleteable']) || !$config['softDeleteable']) {
             return '';
         }
 
-        return $targetTableAlias.'.'.$config['fieldName'].' IS NULL';        
+        return $targetTableAlias.'.'.$config['fieldName'].' IS NULL';
     }
 
-    protected function getConfiguration(ClassMetadata $meta)
+    protected function getListener()
     {
-        if (empty($this->configuration)) {
-            $refl = new \ReflectionProperty('Doctrine\ORM\Query\Filter\SQLFilter', 'em');
-            $refl->setAccessible(true);
-            $em = $refl->getValue($this);
+        if ($this->listener === null) {
+            $em = $this->getEntityManager();
             $evm = $em->getEventManager();
 
             foreach ($evm->getListeners() as $listeners) {
                 foreach ($listeners as $listener) {
                     if ($listener instanceof SoftDeleteableListener) {
-                        $this->configuration = $listener->getConfiguration($em, $meta->name);
+                        $this->listener = $listener;
 
-                        break;
+                        break 2;
                     }
                 }
-
-                if ($this->configuration === null) {
-                    break;
-                }
             }
 
-            if ($this->configuration === null) {
+            if (!$this->listener === null) {
                 throw new \RuntimeException('Listener "SoftDeleteableListener" was not added to the EventManager!');
             }
         }
 
-        return $this->configuration;
+        return $this->listener;
+    }
+
+    protected function getEntityManager()
+    {
+        if ($this->entityManager === null) {
+            $refl = new \ReflectionProperty('Doctrine\ORM\Query\Filter\SQLFilter', 'em');
+            $refl->setAccessible(true);
+            $this->entityManager = $refl->getValue($this);
+        }
+
+        return $this->entityManager;
     }
 }