CacheWarmerAggregate.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\CacheWarmer;
  11. /**
  12. * Aggregates several cache warmers into a single one.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @final
  17. */
  18. class CacheWarmerAggregate implements CacheWarmerInterface
  19. {
  20. private $warmers;
  21. private $optionalsEnabled = false;
  22. private $onlyOptionalsEnabled = false;
  23. public function __construct(iterable $warmers = array())
  24. {
  25. $this->warmers = $warmers;
  26. }
  27. public function enableOptionalWarmers()
  28. {
  29. $this->optionalsEnabled = true;
  30. }
  31. public function enableOnlyOptionalWarmers()
  32. {
  33. $this->onlyOptionalsEnabled = $this->optionalsEnabled = true;
  34. }
  35. /**
  36. * Warms up the cache.
  37. *
  38. * @param string $cacheDir The cache directory
  39. */
  40. public function warmUp($cacheDir)
  41. {
  42. foreach ($this->warmers as $warmer) {
  43. if (!$this->optionalsEnabled && $warmer->isOptional()) {
  44. continue;
  45. }
  46. if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) {
  47. continue;
  48. }
  49. $warmer->warmUp($cacheDir);
  50. }
  51. }
  52. /**
  53. * Checks whether this warmer is optional or not.
  54. *
  55. * @return bool always false
  56. */
  57. public function isOptional()
  58. {
  59. return false;
  60. }
  61. }