Bundle.php 4.7KB

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