CacheWarmerAggregate.php 1.6 KB

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