vendor/gedmo/doctrine-extensions/lib/Gedmo/SoftDeleteable/Filter/SoftDeleteableFilter.php line 51

Open in your IDE?
  1. <?php
  2. namespace Gedmo\SoftDeleteable\Filter;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\Mapping\ClassMetadata;
  5. use Doctrine\ORM\Query\Filter\SQLFilter;
  6. use Gedmo\SoftDeleteable\SoftDeleteableListener;
  7. /**
  8.  * The SoftDeleteableFilter adds the condition necessary to
  9.  * filter entities which were deleted "softly"
  10.  *
  11.  * @author Gustavo Falco <comfortablynumb84@gmail.com>
  12.  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13.  * @author Patrik Votoček <patrik@votocek.cz>
  14.  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15.  */
  16. class SoftDeleteableFilter extends SQLFilter
  17. {
  18.     /**
  19.      * @var SoftDeleteableListener
  20.      */
  21.     protected $listener;
  22.     /**
  23.      * @var EntityManagerInterface
  24.      */
  25.     protected $entityManager;
  26.     /**
  27.      * @var string[bool]
  28.      */
  29.     protected $disabled = array();
  30.     /**
  31.      * @param ClassMetadata $targetEntity
  32.      * @param string        $targetTableAlias
  33.      * @return string
  34.      */
  35.     public function addFilterConstraint(ClassMetadata $targetEntity$targetTableAlias)
  36.     {
  37.         $class $targetEntity->getName();
  38.         if (array_key_exists($class$this->disabled) && $this->disabled[$class] === true) {
  39.             return '';
  40.         } elseif (array_key_exists($targetEntity->rootEntityName$this->disabled) && $this->disabled[$targetEntity->rootEntityName] === true) {
  41.             return '';
  42.         }
  43.         $config $this->getListener()->getConfiguration($this->getEntityManager(), $targetEntity->name);
  44.         if (!isset($config['softDeleteable']) || !$config['softDeleteable']) {
  45.             return '';
  46.         }
  47.         $conn $this->getEntityManager()->getConnection();
  48.         $platform $conn->getDatabasePlatform();
  49.         $column $targetEntity->getQuotedColumnName($config['fieldName'], $platform);
  50.         $addCondSql $platform->getIsNullExpression($targetTableAlias.'.'.$column);
  51.         if (isset($config['timeAware']) && $config['timeAware']) {
  52.             $addCondSql "({$addCondSql} OR {$targetTableAlias}.{$column} > {$platform->getCurrentTimestampSQL()})";
  53.         }
  54.         return $addCondSql;
  55.     }
  56.     /**
  57.      * @param string $class
  58.      */
  59.     public function disableForEntity($class)
  60.     {
  61.         $this->disabled[$class] = true;
  62.         // Make sure the hash (@see SQLFilter::__toString()) for this filter will be changed to invalidate the query cache.
  63.         $this->setParameter(sprintf('disabled_%s'$class), true);
  64.     }
  65.     /**
  66.      * @param string $class
  67.      */
  68.     public function enableForEntity($class)
  69.     {
  70.         $this->disabled[$class] = false;
  71.         // Make sure the hash (@see SQLFilter::__toString()) for this filter will be changed to invalidate the query cache.
  72.         $this->setParameter(sprintf('disabled_%s'$class), false);
  73.     }
  74.     /**
  75.      * @return SoftDeleteableListener
  76.      * @throws \RuntimeException
  77.      */
  78.     protected function getListener()
  79.     {
  80.         if ($this->listener === null) {
  81.             $em $this->getEntityManager();
  82.             $evm $em->getEventManager();
  83.             foreach ($evm->getListeners() as $listeners) {
  84.                 foreach ($listeners as $listener) {
  85.                     if ($listener instanceof SoftDeleteableListener) {
  86.                         $this->listener $listener;
  87.                         break 2;
  88.                     }
  89.                 }
  90.             }
  91.             if ($this->listener === null) {
  92.                 throw new \RuntimeException('Listener "SoftDeleteableListener" was not added to the EventManager!');
  93.             }
  94.         }
  95.         return $this->listener;
  96.     }
  97.     /**
  98.      * @return EntityManagerInterface
  99.      */
  100.     protected function getEntityManager()
  101.     {
  102.         if ($this->entityManager === null) {
  103.             $refl = new \ReflectionProperty('Doctrine\ORM\Query\Filter\SQLFilter''em');
  104.             $refl->setAccessible(true);
  105.             $this->entityManager $refl->getValue($this);
  106.         }
  107.         return $this->entityManager;
  108.     }
  109. }