CacheWarmerAggregate.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 since version 3.4
  17. */
  18. class CacheWarmerAggregate implements CacheWarmerInterface
  19. {
  20. protected $warmers = [];
  21. protected $optionalsEnabled = false;
  22. private $triggerDeprecation = false;
  23. public function __construct($warmers = [])
  24. {
  25. foreach ($warmers as $warmer) {
  26. $this->add($warmer);
  27. }
  28. $this->triggerDeprecation = true;
  29. }
  30. public function enableOptionalWarmers()
  31. {
  32. $this->optionalsEnabled = true;
  33. }
  34. /**
  35. * Warms up the cache.
  36. *
  37. * @param string $cacheDir The cache directory
  38. */
  39. public function warmUp($cacheDir)
  40. {
  41. foreach ($this->warmers as $warmer) {
  42. if (!$this->optionalsEnabled && $warmer->isOptional()) {
  43. continue;
  44. }
  45. $warmer->warmUp($cacheDir);
  46. }
  47. }
  48. /**
  49. * Checks whether this warmer is optional or not.
  50. *
  51. * @return bool always false
  52. */
  53. public function isOptional()
  54. {
  55. return false;
  56. }
  57. /**
  58. * @deprecated since version 3.4, to be removed in 4.0, inject the list of clearers as a constructor argument instead.
  59. */
  60. public function setWarmers(array $warmers)
  61. {
  62. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.4 and will be removed in 4.0, inject the list of clearers as a constructor argument instead.', __METHOD__), E_USER_DEPRECATED);
  63. $this->warmers = [];
  64. foreach ($warmers as $warmer) {
  65. $this->add($warmer);
  66. }
  67. }
  68. /**
  69. * @deprecated since version 3.4, to be removed in 4.0, inject the list of clearers as a constructor argument instead.
  70. */
  71. public function add(CacheWarmerInterface $warmer)
  72. {
  73. if ($this->triggerDeprecation) {
  74. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 3.4 and will be removed in 4.0, inject the list of clearers as a constructor argument instead.', __METHOD__), E_USER_DEPRECATED);
  75. }
  76. $this->warmers[] = $warmer;
  77. }
  78. }