vendor/blackbit/data-director/EventListener/ClassChangedListener.php line 60

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright Blackbit digital Commerce GmbH <info@blackbit.de>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  8.  *
  9.  * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  10.  */
  11. namespace Blackbit\DataDirectorBundle\EventListener;
  12. use Blackbit\DataDirectorBundle\lib\Pim\Item\ItemMoldBuilder;
  13. use Blackbit\DataDirectorBundle\model\Dataport;
  14. use Blackbit\DataDirectorBundle\model\PimcoreDbRepository;
  15. use Blackbit\DataDirectorBundle\Tools\Installer;
  16. use Pimcore\Event\Model\DataObject\ClassDefinitionEvent;
  17. use Pimcore\Model\DataObject\ClassDefinition\Data\Localizedfields;
  18. use Pimcore\Tool;
  19. class ClassChangedListener
  20. {
  21.     /** @var ItemMoldBuilder */
  22.     private $itemMoldBuilder;
  23.     public function __construct(ItemMoldBuilder $itemMoldBuilder)
  24.     {
  25.         $this->itemMoldBuilder $itemMoldBuilder;
  26.     }
  27.     public function removeCompiledDataQuerySelectors(ClassDefinitionEvent $e) {
  28.         try {
  29.             $dummyObject $this->itemMoldBuilder->getItemMoldByClassname($e->getClassDefinition()->getName());
  30.             $classFqn = \get_class($dummyObject);
  31.         } catch (\Exception $classNotFoundException) {
  32.             $classFqn 'Pimcore\\Model\\DataObject\\'.$e->getClassDefinition()->getName();
  33.         }
  34.         $directoryIterator = new \DirectoryIterator(Installer::getCachePath());
  35.         $fileNamePrefix preg_replace('/\W+/''_'$classFqn.' ');
  36.         $filterIterator = new \CallbackFilterIterator($directoryIterator, static function (\SplFileInfo $fileInfo) use ($fileNamePrefix) {
  37.             return strpos($fileInfo->getFilename(), $fileNamePrefix) === || strpos($fileInfo->getFilename(), 'Dao_'.$fileNamePrefix) === || strpos($fileInfo->getFilename(), 'Listing_'.$fileNamePrefix) === 0;
  38.         });
  39.         /** @var \SplFileInfo $compiledFileInfo */
  40.         foreach ($filterIterator as $compiledFile) {
  41.             unlink($compiledFile->getPathname());
  42.         }
  43.     }
  44.     public function removeAllCompiledDataQuerySelectors() {
  45.         /** @var \SplFileInfo $compiledFileInfo */
  46.         foreach(new \DirectoryIterator(Installer::getCachePath()) as $compiledFileInfo) {
  47.             if (!$compiledFileInfo->isDot()) {
  48.                 @unlink($compiledFileInfo->getPathname());
  49.             }
  50.         }
  51.     }
  52.     public function createStoreView(ClassDefinitionEvent $e)
  53.     {
  54.         if ($e->getClassDefinition()->getAllowInherit()) {
  55.             foreach (Tool::getValidLanguages() as $language) {
  56.                 $hasLocalizedFields false;
  57.                 foreach($e->getClassDefinition()->getFieldDefinitions() as $fieldDefinition) {
  58.                     if($fieldDefinition instanceof Localizedfields) {
  59.                         $hasLocalizedFields true;
  60.                         break;
  61.                     }
  62.                 }
  63.                 $query 'CREATE OR REPLACE VIEW `object_localized_store_'.$e->getClassDefinition()->getId().'_'.$language.'` AS SELECT * FROM `object_store_'.$e->getClassDefinition()->getId().'` JOIN `objects` ON `objects`.`o_id` = `object_store_'.$e->getClassDefinition()->getId().'`.`oo_id`';
  64.                 $parameters = [];
  65.                 if($hasLocalizedFields) {
  66.                     $query .= ' JOIN `object_localized_data_'.$e->getClassDefinition()->getId().'` ON `object_store_'.$e->getClassDefinition()->getId().'`.oo_id=`object_localized_data_'.$e->getClassDefinition()->getId().'`.ooo_id AND language=?';
  67.                     $parameters[] = $language;
  68.                 }
  69.                 PimcoreDbRepository::getInstance()->execute($query$parameters);
  70.             }
  71.         } else {
  72.             $this->removeStoreView($e);
  73.         }
  74.     }
  75.     public function removeStoreView(ClassDefinitionEvent $e)
  76.     {
  77.         foreach (Tool::getValidLanguages() as $language) {
  78.             PimcoreDbRepository::getInstance()->execute('DROP VIEW IF EXISTS `object_localized_store_'.$e->getClassDefinition()->getId().'_'.$language.'`');
  79.         }
  80.     }
  81. }