CacheWarmerAggregate.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. class CacheWarmerAggregate implements CacheWarmerInterface
  17. {
  18. protected $warmers = array();
  19. protected $optionalsEnabled = false;
  20. public function __construct(array $warmers = array())
  21. {
  22. foreach ($warmers as $warmer) {
  23. $this->add($warmer);
  24. }
  25. }
  26. public function enableOptionalWarmers()
  27. {
  28. $this->optionalsEnabled = true;
  29. }
  30. /**
  31. * Warms up the cache.
  32. *
  33. * @param string $cacheDir The cache directory
  34. */
  35. public function warmUp($cacheDir)
  36. {
  37. foreach ($this->warmers as $warmer) {
  38. if (!$this->optionalsEnabled && $warmer->isOptional()) {
  39. continue;
  40. }
  41. $warmer->warmUp($cacheDir);
  42. }
  43. }
  44. /**
  45. * Checks whether this warmer is optional or not.
  46. *
  47. * @return bool always false
  48. */
  49. public function isOptional()
  50. {
  51. return false;
  52. }
  53. public function setWarmers(array $warmers)
  54. {
  55. $this->warmers = array();
  56. foreach ($warmers as $warmer) {
  57. $this->add($warmer);
  58. }
  59. }
  60. public function add(CacheWarmerInterface $warmer)
  61. {
  62. $this->warmers[] = $warmer;
  63. }
  64. }