ClosureLoader.php 1.0 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\Component\DependencyInjection\Loader;
  11. use Symfony\Component\Config\Loader\Loader;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. /**
  14. * ClosureLoader loads service definitions from a PHP closure.
  15. *
  16. * The Closure has access to the container as its first argument.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class ClosureLoader extends Loader
  21. {
  22. private $container;
  23. public function __construct(ContainerBuilder $container)
  24. {
  25. $this->container = $container;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function load($resource, string $type = null)
  31. {
  32. $resource($this->container);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function supports($resource, string $type = null)
  38. {
  39. return $resource instanceof \Closure;
  40. }
  41. }