CacheWarmerAggregate.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }