ClosureLoader.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\Config\Loader\Loader;
  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. /**
  24. * Constructor.
  25. *
  26. * @param ContainerBuilder $container A ContainerBuilder instance
  27. */
  28. public function __construct(ContainerBuilder $container)
  29. {
  30. $this->container = $container;
  31. }
  32. /**
  33. * Loads a Closure.
  34. *
  35. * @param \Closure $closure The resource
  36. * @param string $type The resource type
  37. */
  38. public function load($closure, $type = null)
  39. {
  40. call_user_func($closure, $this->container);
  41. }
  42. /**
  43. * Returns true if this class supports the given resource.
  44. *
  45. * @param mixed $resource A resource
  46. * @param string $type The resource type
  47. *
  48. * @return Boolean true if this class supports the given resource, false otherwise
  49. */
  50. public function supports($resource, $type = null)
  51. {
  52. return $resource instanceof \Closure;
  53. }
  54. }