Bundle.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\HttpKernel\Bundle;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\DependencyInjection\Container;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  16. /**
  17. * An implementation of BundleInterface that adds a few conventions for DependencyInjection extensions.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. abstract class Bundle implements BundleInterface
  22. {
  23. protected string $name;
  24. protected ExtensionInterface|false|null $extension = null;
  25. protected string $path;
  26. protected ?ContainerInterface $container;
  27. private string $namespace;
  28. public function boot(): void
  29. {
  30. }
  31. public function shutdown(): void
  32. {
  33. }
  34. /**
  35. * This method can be overridden to register compilation passes,
  36. * other extensions, ...
  37. */
  38. public function build(ContainerBuilder $container): void
  39. {
  40. }
  41. /**
  42. * Returns the bundle's container extension.
  43. *
  44. * @throws \LogicException
  45. */
  46. public function getContainerExtension(): ?ExtensionInterface
  47. {
  48. if (!isset($this->extension)) {
  49. $extension = $this->createContainerExtension();
  50. if (null !== $extension) {
  51. if (!$extension instanceof ExtensionInterface) {
  52. throw new \LogicException(\sprintf('Extension "%s" must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_debug_type($extension)));
  53. }
  54. // check naming convention
  55. $basename = preg_replace('/Bundle$/', '', $this->getName());
  56. $expectedAlias = Container::underscore($basename);
  57. if ($expectedAlias != $extension->getAlias()) {
  58. throw new \LogicException(\sprintf('Users will expect the alias of the default extension of a bundle to be the underscored version of the bundle name ("%s"). You can override "Bundle::getContainerExtension()" if you want to use "%s" or another alias.', $expectedAlias, $extension->getAlias()));
  59. }
  60. $this->extension = $extension;
  61. } else {
  62. $this->extension = false;
  63. }
  64. }
  65. return $this->extension ?: null;
  66. }
  67. public function getNamespace(): string
  68. {
  69. if (!isset($this->namespace)) {
  70. $this->parseClassName();
  71. }
  72. return $this->namespace;
  73. }
  74. public function getPath(): string
  75. {
  76. if (!isset($this->path)) {
  77. $reflected = new \ReflectionObject($this);
  78. $this->path = \dirname($reflected->getFileName());
  79. }
  80. return $this->path;
  81. }
  82. /**
  83. * Returns the bundle name (the class short name).
  84. */
  85. final public function getName(): string
  86. {
  87. if (!isset($this->name)) {
  88. $this->parseClassName();
  89. }
  90. return $this->name;
  91. }
  92. public function registerCommands(Application $application): void
  93. {
  94. }
  95. /**
  96. * Returns the bundle's container extension class.
  97. */
  98. protected function getContainerExtensionClass(): string
  99. {
  100. $basename = preg_replace('/Bundle$/', '', $this->getName());
  101. return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  102. }
  103. /**
  104. * Creates the bundle's container extension.
  105. */
  106. protected function createContainerExtension(): ?ExtensionInterface
  107. {
  108. return class_exists($class = $this->getContainerExtensionClass()) ? new $class() : null;
  109. }
  110. private function parseClassName(): void
  111. {
  112. $pos = strrpos(static::class, '\\');
  113. $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
  114. $this->name ??= false === $pos ? static::class : substr(static::class, $pos + 1);
  115. }
  116. public function setContainer(?ContainerInterface $container): void
  117. {
  118. $this->container = $container;
  119. }
  120. }