Psr6CacheClearer.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\CacheClearer;
  11. use Psr\Cache\CacheItemPoolInterface;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. */
  15. class Psr6CacheClearer implements CacheClearerInterface
  16. {
  17. private array $pools = [];
  18. /**
  19. * @param array<string, CacheItemPoolInterface> $pools
  20. */
  21. public function __construct(array $pools = [])
  22. {
  23. $this->pools = $pools;
  24. }
  25. public function hasPool(string $name): bool
  26. {
  27. return isset($this->pools[$name]);
  28. }
  29. /**
  30. * @throws \InvalidArgumentException If the cache pool with the given name does not exist
  31. */
  32. public function getPool(string $name): CacheItemPoolInterface
  33. {
  34. if (!$this->hasPool($name)) {
  35. throw new \InvalidArgumentException(\sprintf('Cache pool not found: "%s".', $name));
  36. }
  37. return $this->pools[$name];
  38. }
  39. /**
  40. * @throws \InvalidArgumentException If the cache pool with the given name does not exist
  41. */
  42. public function clearPool(string $name): bool
  43. {
  44. if (!isset($this->pools[$name])) {
  45. throw new \InvalidArgumentException(\sprintf('Cache pool not found: "%s".', $name));
  46. }
  47. return $this->pools[$name]->clear();
  48. }
  49. /**
  50. * @return void
  51. */
  52. public function clear(string $cacheDir)
  53. {
  54. foreach ($this->pools as $pool) {
  55. $pool->clear();
  56. }
  57. }
  58. }