Keine Beschreibung

Bundle.php 4.3KB

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