ContainerAwareRuntimeLoader.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\TwigBundle;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. /**
  14. * Loads Twig extension runtimes via the service container.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class ContainerAwareRuntimeLoader implements \Twig_RuntimeLoaderInterface
  19. {
  20. private $container;
  21. private $mapping;
  22. private $logger;
  23. public function __construct(ContainerInterface $container, array $mapping, LoggerInterface $logger = null)
  24. {
  25. $this->container = $container;
  26. $this->mapping = $mapping;
  27. $this->logger = $logger;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function load($class)
  33. {
  34. if (isset($this->mapping[$class])) {
  35. return $this->container->get($this->mapping[$class]);
  36. }
  37. if (null !== $this->logger) {
  38. $this->logger->warning(sprintf('Class "%s" is not configured as a Twig runtime. Add the "twig.runtime" tag to the related service in the container.', $class));
  39. }
  40. }
  41. }