Bundle.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /**
  29. * @return void
  30. */
  31. public function boot()
  32. {
  33. }
  34. /**
  35. * @return void
  36. */
  37. public function shutdown()
  38. {
  39. }
  40. /**
  41. * This method can be overridden to register compilation passes,
  42. * other extensions, ...
  43. *
  44. * @return void
  45. */
  46. public function build(ContainerBuilder $container)
  47. {
  48. }
  49. /**
  50. * Returns the bundle's container extension.
  51. *
  52. * @throws \LogicException
  53. */
  54. public function getContainerExtension(): ?ExtensionInterface
  55. {
  56. if (!isset($this->extension)) {
  57. $extension = $this->createContainerExtension();
  58. if (null !== $extension) {
  59. if (!$extension instanceof ExtensionInterface) {
  60. throw new \LogicException(\sprintf('Extension "%s" must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_debug_type($extension)));
  61. }
  62. // check naming convention
  63. $basename = preg_replace('/Bundle$/', '', $this->getName());
  64. $expectedAlias = Container::underscore($basename);
  65. if ($expectedAlias != $extension->getAlias()) {
  66. 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()));
  67. }
  68. $this->extension = $extension;
  69. } else {
  70. $this->extension = false;
  71. }
  72. }
  73. return $this->extension ?: null;
  74. }
  75. public function getNamespace(): string
  76. {
  77. if (!isset($this->namespace)) {
  78. $this->parseClassName();
  79. }
  80. return $this->namespace;
  81. }
  82. public function getPath(): string
  83. {
  84. if (!isset($this->path)) {
  85. $reflected = new \ReflectionObject($this);
  86. $this->path = \dirname($reflected->getFileName());
  87. }
  88. return $this->path;
  89. }
  90. /**
  91. * Returns the bundle name (the class short name).
  92. */
  93. final public function getName(): string
  94. {
  95. if (!isset($this->name)) {
  96. $this->parseClassName();
  97. }
  98. return $this->name;
  99. }
  100. /**
  101. * @return void
  102. */
  103. public function registerCommands(Application $application)
  104. {
  105. }
  106. /**
  107. * Returns the bundle's container extension class.
  108. */
  109. protected function getContainerExtensionClass(): string
  110. {
  111. $basename = preg_replace('/Bundle$/', '', $this->getName());
  112. return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  113. }
  114. /**
  115. * Creates the bundle's container extension.
  116. */
  117. protected function createContainerExtension(): ?ExtensionInterface
  118. {
  119. return class_exists($class = $this->getContainerExtensionClass()) ? new $class() : null;
  120. }
  121. private function parseClassName(): void
  122. {
  123. $pos = strrpos(static::class, '\\');
  124. $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
  125. $this->name ??= false === $pos ? static::class : substr(static::class, $pos + 1);
  126. }
  127. public function setContainer(?ContainerInterface $container): void
  128. {
  129. $this->container = $container;
  130. }
  131. }