Bundle.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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
  18. * for DependencyInjection extensions and Console commands.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. abstract class Bundle implements BundleInterface
  23. {
  24. use ContainerAwareTrait;
  25. protected $name;
  26. protected $extension;
  27. protected $path;
  28. private $namespace;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function boot()
  33. {
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function shutdown()
  39. {
  40. }
  41. /**
  42. * {@inheritdoc}
  43. *
  44. * This method can be overridden to register compilation passes,
  45. * other extensions, ...
  46. */
  47. public function build(ContainerBuilder $container)
  48. {
  49. }
  50. /**
  51. * Returns the bundle's container extension.
  52. *
  53. * @return ExtensionInterface|null The container extension
  54. *
  55. * @throws \LogicException
  56. */
  57. public function getContainerExtension()
  58. {
  59. if (null === $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_class($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. if ($this->extension) {
  77. return $this->extension;
  78. }
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getNamespace()
  84. {
  85. if (null === $this->namespace) {
  86. $this->parseClassName();
  87. }
  88. return $this->namespace;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getPath()
  94. {
  95. if (null === $this->path) {
  96. $reflected = new \ReflectionObject($this);
  97. $this->path = \dirname($reflected->getFileName());
  98. }
  99. return $this->path;
  100. }
  101. /**
  102. * Returns the bundle name (the class short name).
  103. *
  104. * @return string The Bundle name
  105. */
  106. final public function getName()
  107. {
  108. if (null === $this->name) {
  109. $this->parseClassName();
  110. }
  111. return $this->name;
  112. }
  113. public function registerCommands(Application $application)
  114. {
  115. }
  116. /**
  117. * Returns the bundle's container extension class.
  118. *
  119. * @return string
  120. */
  121. protected function getContainerExtensionClass()
  122. {
  123. $basename = preg_replace('/Bundle$/', '', $this->getName());
  124. return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  125. }
  126. /**
  127. * Creates the bundle's container extension.
  128. *
  129. * @return ExtensionInterface|null
  130. */
  131. protected function createContainerExtension()
  132. {
  133. if (class_exists($class = $this->getContainerExtensionClass())) {
  134. return new $class();
  135. }
  136. }
  137. private function parseClassName()
  138. {
  139. $pos = strrpos(static::class, '\\');
  140. $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
  141. if (null === $this->name) {
  142. $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);
  143. }
  144. }
  145. }