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

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