PhpFileLoader.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Loader\Configurator\ContainerConfigurator;
  12. /**
  13. * PhpFileLoader loads service definitions from a PHP file.
  14. *
  15. * The PHP file is required and the $container variable can be
  16. * used within the file to change the container.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class PhpFileLoader extends FileLoader
  21. {
  22. protected $autoRegisterAliasesForSinglyImplementedInterfaces = false;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function load($resource, $type = null)
  27. {
  28. // the container and loader variables are exposed to the included file below
  29. $container = $this->container;
  30. $loader = $this;
  31. $path = $this->locator->locate($resource);
  32. $this->setCurrentDir(\dirname($path));
  33. $this->container->fileExists($path);
  34. // the closure forbids access to the private scope in the included file
  35. $load = \Closure::bind(function ($path) use ($container, $loader, $resource, $type) {
  36. return include $path;
  37. }, $this, ProtectedPhpFileLoader::class);
  38. try {
  39. $callback = $load($path);
  40. if (\is_object($callback) && \is_callable($callback)) {
  41. $callback(new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource), $this->container, $this);
  42. }
  43. } finally {
  44. $this->instanceof = [];
  45. $this->registerAliasesForSinglyImplementedInterfaces();
  46. }
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function supports($resource, string $type = null)
  52. {
  53. if (!\is_string($resource)) {
  54. return false;
  55. }
  56. if (null === $type && 'php' === pathinfo($resource, \PATHINFO_EXTENSION)) {
  57. return true;
  58. }
  59. return 'php' === $type;
  60. }
  61. }
  62. /**
  63. * @internal
  64. */
  65. final class ProtectedPhpFileLoader extends PhpFileLoader
  66. {
  67. }