classes/XLite/Core/Database.php line 111

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XLite\Core;
  7. use XCart\Bundle\DoctrineBridgeBundle\QueryBuilder\Factory\QueryBuilderFactory;
  8. use XCart\Bundle\DoctrineBridgeBundle\Repository\ReadRepositoryInterface;
  9. use XCart\Doctrine\FixtureLoader;
  10. use XLite\InjectLoggerTrait;
  11. /**
  12.  * Database
  13.  */
  14. class Database extends \XLite\Base\Singleton
  15. {
  16.     use InjectLoggerTrait;
  17.     /**
  18.      * Doctrine entity manager
  19.      *
  20.      * @var \XLite\Core\Doctrine\ORM\EntityManager
  21.      */
  22.     protected static $em;
  23.     /**
  24.      * connected
  25.      *
  26.      * @var boolean
  27.      */
  28.     protected $connected;
  29.     /**
  30.      * @return FixtureLoader
  31.      */
  32.     protected function getFixtureLoader()
  33.     {
  34.         return \XCart\Container::getContainer()->get(FixtureLoader::class);
  35.     }
  36.     /**
  37.      * Get entity manager
  38.      *
  39.      * @return \XLite\Core\Doctrine\ORM\EntityManager
  40.      */
  41.     public static function getEM()
  42.     {
  43.         if (static::$em === null) {
  44.             \XLite\Core\Database::getInstance();
  45.         }
  46.         return static::$em;
  47.     }
  48.     /**
  49.      * Get repository (short method)
  50.      *
  51.      * @param string $entity Entity class name
  52.      *
  53.      * @return \XLite\Model\Repo\ARepo
  54.      */
  55.     public static function getRepo($entity)
  56.     {
  57.         $entity = static::getEntityClass($entity);
  58.         $repository class_exists($entity)
  59.             ? static::getEM()->getRepository($entity)
  60.             : null;
  61.         if ($repository instanceof ReadRepositoryInterface) {
  62.             $className str_contains(get_class($repository), '\Repository\\')
  63.                 ? preg_replace('/\\\\Repository\\\\/S''\QueryBuilder\\'get_class($repository))
  64.                 : preg_replace('/\\\\Repo\\\\/S''\QueryBuilder\\'get_class($repository));
  65.             if (!class_exists($className)) {
  66.                 $className \XCart\Bundle\DoctrineBridgeBundle\QueryBuilder\CommonQueryBuilder::class;
  67.             }
  68.             $repository->setQueryBuilderFactory(
  69.                 new QueryBuilderFactory(static::getEM(), $className),
  70.             );
  71.         }
  72.         return $repository;
  73.     }
  74.     /**
  75.      * Calculate the class name for entity
  76.      *
  77.      * @param string $entity Entity
  78.      *
  79.      * @return string
  80.      */
  81.     public static function getEntityClass($entity)
  82.     {
  83.         return ltrim($entity'\\');
  84.     }
  85.     /**
  86.      * Get cache driver
  87.      *
  88.      * @return \Doctrine\Common\Cache\CacheProvider
  89.      *
  90.      * @deprecated since CDev-Core 5.5.0, use \XLite\Core\Cache::getDriver() instead
  91.      */
  92.     public static function getCacheDriver()
  93.     {
  94.         trigger_deprecation('CDev-Core''5.5.0''"%s" is deprecated, use \XLite\Core\Cache::getDriver() instead.'__METHOD__);
  95.         return \XLite\Core\Cache::getInstance()->getDriver();
  96.     }
  97.     /**
  98.      * Get cache driver
  99.      *
  100.      * @return \Doctrine\Common\Cache\CacheProvider
  101.      *
  102.      * @deprecated since CDev-Core 5.5.0, use \XLite\Core\Cache::getDriver() instead
  103.      */
  104.     public static function getFreshCacheDriver()
  105.     {
  106.         trigger_deprecation('CDev-Core''5.5.0''"%s" is deprecated, use \XLite\Core\Cache::getDriver() instead.'__METHOD__);
  107.         return \XLite\Core\Cache::getInstance()->getDriver();
  108.     }
  109.     /**
  110.      * Prepare array for IN () DQL function
  111.      *
  112.      * @param array  $data   Hash array
  113.      * @param string $prefix Placeholder prefix OPTIONAL
  114.      *
  115.      * @return array (keys for IN () function & parameters hash array)
  116.      */
  117.     public static function prepareArray(array $data$prefix 'arr')
  118.     {
  119.         $keys       = [];
  120.         $parameters = [];
  121.         foreach ($data as $k => $v) {
  122.             $k              $prefix $k;
  123.             $keys[]         = ':' $k;
  124.             $parameters[$k] = $v;
  125.         }
  126.         return [$keys$parameters];
  127.     }
  128.     /**
  129.      * Constructor
  130.      */
  131.     public function __construct()
  132.     {
  133.         if (!$this->connected) {
  134.             $this->startEntityManager();
  135.         }
  136.     }
  137.     /**
  138.      * Start Doctrine entity manager
  139.      *
  140.      * @return void
  141.      */
  142.     public function startEntityManager()
  143.     {
  144.         static::$em \XCart\Container::getContainer()->get('doctrine.orm.entity_manager');
  145.     }
  146.     /**
  147.      * Load fixtures from YAML file
  148.      *
  149.      * @param string $path    YAML file path
  150.      * @param array  $options Options         OPTIONAL
  151.      *
  152.      * @return void
  153.      * @deprecated since CDev-Core 5.5.0, \XCart\Doctrine\FixtureLoader::loadYaml instead
  154.      */
  155.     public function loadFixturesFromYaml($path$options null)
  156.     {
  157.         trigger_deprecation('CDev-Core''5.5.0''"%s" is deprecated, use \XCart\Doctrine\FixtureLoader::loadYaml() instead.'__METHOD__);
  158.         $allowedModels  $options['allowedModels'] ?? [];
  159.         $excludedModels $options['excludedModels'] ?? [];
  160.         $this->getFixtureLoader()->loadYaml($path$allowedModels$excludedModels);
  161.     }
  162.     /**
  163.      * postPersist event handler
  164.      *
  165.      * @param \Doctrine\ORM\Event\LifecycleEventArgs $arg Event argument
  166.      *
  167.      * @return void
  168.      */
  169.     public function postPersist(\Doctrine\ORM\Event\LifecycleEventArgs $arg)
  170.     {
  171.         $entity $arg->getEntity();
  172.         if ($entity instanceof \XLite\Model\AEntity) {
  173.             $entity->checkCache();
  174.         }
  175.     }
  176.     /**
  177.      * postUpdate event handler
  178.      *
  179.      * @param \Doctrine\ORM\Event\LifecycleEventArgs $arg Event argument
  180.      *
  181.      * @return void
  182.      */
  183.     public function postUpdate(\Doctrine\ORM\Event\LifecycleEventArgs $arg)
  184.     {
  185.         $entity $arg->getEntity();
  186.         if ($entity instanceof \XLite\Model\AEntity) {
  187.             $entity->checkCache();
  188.         }
  189.     }
  190.     /**
  191.      * postRemove event handler
  192.      *
  193.      * @param \Doctrine\ORM\Event\LifecycleEventArgs $arg Event argument
  194.      *
  195.      * @return void
  196.      */
  197.     public function postRemove(\Doctrine\ORM\Event\LifecycleEventArgs $arg)
  198.     {
  199.         $entity $arg->getEntity();
  200.         if ($entity instanceof \XLite\Model\AEntity) {
  201.             $entity->checkCache();
  202.         }
  203.     }
  204. }