Bundle.php 4.3 KB

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