vendor/shopware/core/Framework/Api/Controller/CacheController.php line 55

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\Controller;
  3. use Shopware\Core\Framework\Adapter\Cache\CacheClearer;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexerRegistry;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Shopware\Core\Framework\Routing\Annotation\Acl;
  7. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  8. use Shopware\Core\Framework\Routing\Annotation\Since;
  9. use Shopware\Core\Framework\Util\Random;
  10. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  11. use Shopware\Storefront\Framework\Cache\CacheWarmer\CacheWarmer;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\Cache\Adapter\AdapterInterface;
  14. use Symfony\Component\Cache\Adapter\TagAwareAdapter;
  15. use Symfony\Component\Cache\Adapter\TraceableAdapter;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @Route(defaults={"_routeScope"={"api"}})
  21.  */
  22. #[Package('system-settings')]
  23. class CacheController extends AbstractController
  24. {
  25.     private CacheClearer $cacheClearer;
  26.     private AdapterInterface $adapter;
  27.     private ?CacheWarmer $cacheWarmer;
  28.     private EntityIndexerRegistry $indexerRegistry;
  29.     /**
  30.      * @internal
  31.      */
  32.     public function __construct(
  33.         CacheClearer $cacheClearer,
  34.         AdapterInterface $adapter,
  35.         ?CacheWarmer $cacheWarmer,
  36.         EntityIndexerRegistry $indexerRegistry
  37.     ) {
  38.         $this->cacheClearer $cacheClearer;
  39.         $this->adapter $adapter;
  40.         $this->cacheWarmer $cacheWarmer;
  41.         $this->indexerRegistry $indexerRegistry;
  42.     }
  43.     /**
  44.      * @Since("6.2.0.0")
  45.      * @Route("/api/_action/cache_info", name="api.action.cache.info", methods={"GET"}, defaults={"_acl"={"system:cache:info"}})
  46.      */
  47.     public function info(): JsonResponse
  48.     {
  49.         return new JsonResponse([
  50.             'environment' => $this->getParameter('kernel.environment'),
  51.             'httpCache' => $this->container->get('parameter_bag')->has('shopware.http.cache.enabled') && $this->getParameter('shopware.http.cache.enabled'),
  52.             'cacheAdapter' => $this->getUsedCache($this->adapter),
  53.         ]);
  54.     }
  55.     /**
  56.      * @Since("6.2.0.0")
  57.      * @Route("/api/_action/index", name="api.action.cache.index", methods={"POST"}, defaults={"_acl"={"api_action_cache_index"}})
  58.      */
  59.     public function index(RequestDataBag $dataBag): Response
  60.     {
  61.         $data $dataBag->all();
  62.         $skip = !empty($data['skip']) && \is_array($data['skip']) ? $data['skip'] : [];
  63.         $this->indexerRegistry->sendIndexingMessage([], $skip);
  64.         return new Response(''Response::HTTP_NO_CONTENT);
  65.     }
  66.     /**
  67.      * @Since("6.2.0.0")
  68.      * @Route("/api/_action/cache_warmup", name="api.action.cache.delete_and_warmup", methods={"DELETE"}, defaults={"_acl"={"system:clear:cache"}})
  69.      */
  70.     public function clearCacheAndScheduleWarmUp(): Response
  71.     {
  72.         if ($this->cacheWarmer === null) {
  73.             throw new \RuntimeException('Storefront is not installed');
  74.         }
  75.         $this->cacheWarmer->warmUp(Random::getAlphanumericString(32));
  76.         return new Response(''Response::HTTP_NO_CONTENT);
  77.     }
  78.     /**
  79.      * @Since("6.0.0.0")
  80.      * @Route("/api/_action/cache", name="api.action.cache.delete", methods={"DELETE"}, defaults={"_acl"={"system:clear:cache"}})
  81.      */
  82.     public function clearCache(): Response
  83.     {
  84.         $this->cacheClearer->clear();
  85.         return new Response(''Response::HTTP_NO_CONTENT);
  86.     }
  87.     /**
  88.      * @Since("6.2.0.0")
  89.      * @Route("/api/_action/cleanup", name="api.action.cache.cleanup", methods={"DELETE"}, defaults={"_acl"={"system:clear:cache"}})
  90.      */
  91.     public function clearOldCacheFolders(): Response
  92.     {
  93.         $this->cacheClearer->scheduleCacheFolderCleanup();
  94.         return new Response(''Response::HTTP_NO_CONTENT);
  95.     }
  96.     /**
  97.      * @Since("6.2.0.0")
  98.      * @Route("/api/_action/container_cache", name="api.action.container-cache.delete", methods={"DELETE"}, defaults={"_acl"={"system:clear:cache"}})
  99.      */
  100.     public function clearContainerCache(): Response
  101.     {
  102.         $this->cacheClearer->clearContainerCache();
  103.         return new Response(''Response::HTTP_NO_CONTENT);
  104.     }
  105.     private function getUsedCache(AdapterInterface $adapter): string
  106.     {
  107.         if ($adapter instanceof TagAwareAdapter || $adapter instanceof TraceableAdapter) {
  108.             // Do not declare function as static
  109.             $func = \Closure::bind(function () use ($adapter) {
  110.                 return $adapter->pool;
  111.             }, $adapter, \get_class($adapter));
  112.             $adapter $func();
  113.         }
  114.         if ($adapter instanceof TraceableAdapter) {
  115.             return $this->getUsedCache($adapter);
  116.         }
  117.         $name = \get_class($adapter);
  118.         \assert(\is_string($name));
  119.         $parts explode('\\'$name);
  120.         $name str_replace('Adapter'''end($parts));
  121.         return $name;
  122.     }
  123. }