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

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. /**
  9.  * @package core
  10.  */
  11. class CacheIdLoader
  12. {
  13.     private Connection $connection;
  14.     private ?CacheItemPoolInterface $restartSignalCachePool;
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct(Connection $connection, ?CacheItemPoolInterface $restartSignalCachePool null)
  19.     {
  20.         $this->connection $connection;
  21.         $this->restartSignalCachePool $restartSignalCachePool;
  22.     }
  23.     public function load(): string
  24.     {
  25.         $cacheId EnvironmentHelper::getVariable('SHOPWARE_CACHE_ID');
  26.         if ($cacheId) {
  27.             return (string) $cacheId;
  28.         }
  29.         try {
  30.             $cacheId $this->connection->fetchOne(
  31.                 '# cache-id-loader
  32.                 SELECT `value` FROM app_config WHERE `key` = :key',
  33.                 ['key' => 'cache-id']
  34.             );
  35.         } catch (\Exception $e) {
  36.             $cacheId null;
  37.         }
  38.         if (\is_string($cacheId)) {
  39.             return $cacheId;
  40.         }
  41.         $cacheId Uuid::randomHex();
  42.         try {
  43.             $this->write($cacheId);
  44.             return $cacheId;
  45.         } catch (\Exception $e) {
  46.             return 'live';
  47.         }
  48.     }
  49.     public function write(string $cacheId): void
  50.     {
  51.         $this->connection->executeStatement(
  52.             'REPLACE INTO app_config (`key`, `value`) VALUES (:key, :cacheId)',
  53.             ['cacheId' => $cacheId'key' => 'cache-id']
  54.         );
  55.         if ($this->restartSignalCachePool) {
  56.             $cacheItem $this->restartSignalCachePool->getItem(StopWorkerOnRestartSignalListener::RESTART_REQUESTED_TIMESTAMP_KEY);
  57.             $cacheItem->set(microtime(true));
  58.             $this->restartSignalCachePool->save($cacheItem);
  59.         }
  60.     }
  61. }