vendor/shopware/core/Framework/Adapter/Cache/CacheIdLoader.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Cache;
  3. use Doctrine\DBAL\Connection;
  4. use Psr\Cache\CacheItemPoolInterface;
  5. use Shopware\Core\DevOps\Environment\EnvironmentHelper;
  6. use Shopware\Core\Framework\Uuid\Uuid;
  7. use Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener;
  8. class CacheIdLoader
  9. {
  10.     /**
  11.      * @var Connection
  12.      */
  13.     private $connection;
  14.     /**
  15.      * @var CacheItemPoolInterface|null
  16.      */
  17.     private $restartSignalCachePool;
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(Connection $connection, ?CacheItemPoolInterface $restartSignalCachePool null)
  22.     {
  23.         $this->connection $connection;
  24.         $this->restartSignalCachePool $restartSignalCachePool;
  25.     }
  26.     public function load(): string
  27.     {
  28.         $cacheId EnvironmentHelper::getVariable('SHOPWARE_CACHE_ID');
  29.         if ($cacheId) {
  30.             return (string) $cacheId;
  31.         }
  32.         try {
  33.             $cacheId $this->connection->fetchColumn(
  34.                 '# cache-id-loader
  35.                 SELECT `value` FROM app_config WHERE `key` = :key',
  36.                 ['key' => 'cache-id']
  37.             );
  38.         } catch (\Exception $e) {
  39.             $cacheId null;
  40.         }
  41.         if (\is_string($cacheId)) {
  42.             return $cacheId;
  43.         }
  44.         $cacheId Uuid::randomHex();
  45.         try {
  46.             $this->write($cacheId);
  47.             return $cacheId;
  48.         } catch (\Exception $e) {
  49.             return 'live';
  50.         }
  51.     }
  52.     public function write(string $cacheId): void
  53.     {
  54.         $this->connection->executeUpdate(
  55.             'REPLACE INTO app_config (`key`, `value`) VALUES (:key, :cacheId)',
  56.             ['cacheId' => $cacheId'key' => 'cache-id']
  57.         );
  58.         if ($this->restartSignalCachePool) {
  59.             $cacheItem $this->restartSignalCachePool->getItem(StopWorkerOnRestartSignalListener::RESTART_REQUESTED_TIMESTAMP_KEY);
  60.             $cacheItem->set(microtime(true));
  61.             $this->restartSignalCachePool->save($cacheItem);
  62.         }
  63.     }
  64. }