RouterCacheWarmer.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\Bundle\FrameworkBundle\CacheWarmer;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  13. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  14. use Symfony\Component\Routing\RouterInterface;
  15. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  16. /**
  17. * Generates the router matcher and generator classes.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. *
  21. * @final
  22. */
  23. class RouterCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
  24. {
  25. private $container;
  26. public function __construct(ContainerInterface $container)
  27. {
  28. // As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
  29. $this->container = $container;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. *
  34. * @return string[]
  35. */
  36. public function warmUp(string $cacheDir)
  37. {
  38. $router = $this->container->get('router');
  39. if ($router instanceof WarmableInterface) {
  40. return (array) $router->warmUp($cacheDir);
  41. }
  42. throw new \LogicException(sprintf('The router "%s" cannot be warmed up because it does not implement "%s".', get_debug_type($router), WarmableInterface::class));
  43. }
  44. /**
  45. * Checks whether this warmer is optional or not.
  46. *
  47. * @return bool always true
  48. */
  49. public function isOptional(): bool
  50. {
  51. return true;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public static function getSubscribedServices(): array
  57. {
  58. return [
  59. 'router' => RouterInterface::class,
  60. ];
  61. }
  62. }