Bundle.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. use Symfony\Component\Finder\Finder;
  17. /**
  18. * An implementation of BundleInterface that adds a few conventions
  19. * for DependencyInjection extensions and Console commands.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. abstract class Bundle implements BundleInterface
  24. {
  25. /**
  26. * @var ContainerInterface
  27. */
  28. protected $container;
  29. protected $name;
  30. protected $extension;
  31. protected $path;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function boot()
  36. {
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function shutdown()
  42. {
  43. }
  44. /**
  45. * {@inheritdoc}
  46. *
  47. * This method can be overridden to register compilation passes,
  48. * other extensions, ...
  49. */
  50. public function build(ContainerBuilder $container)
  51. {
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function setContainer(ContainerInterface $container = null)
  57. {
  58. $this->container = $container;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. *
  63. * @throws \LogicException
  64. */
  65. public function getContainerExtension()
  66. {
  67. if (null === $this->extension) {
  68. $extension = $this->createContainerExtension();
  69. if (null !== $extension) {
  70. if (!$extension instanceof ExtensionInterface) {
  71. throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', \get_class($extension)));
  72. }
  73. // check naming convention
  74. $basename = preg_replace('/Bundle$/', '', $this->getName());
  75. $expectedAlias = Container::underscore($basename);
  76. if ($expectedAlias != $extension->getAlias()) {
  77. 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()));
  78. }
  79. $this->extension = $extension;
  80. } else {
  81. $this->extension = false;
  82. }
  83. }
  84. if ($this->extension) {
  85. return $this->extension;
  86. }
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getNamespace()
  92. {
  93. $class = \get_class($this);
  94. return substr($class, 0, strrpos($class, '\\'));
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getPath()
  100. {
  101. if (null === $this->path) {
  102. $reflected = new \ReflectionObject($this);
  103. $this->path = \dirname($reflected->getFileName());
  104. }
  105. return $this->path;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getParent()
  111. {
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. final public function getName()
  117. {
  118. if (null !== $this->name) {
  119. return $this->name;
  120. }
  121. $name = \get_class($this);
  122. $pos = strrpos($name, '\\');
  123. return $this->name = false === $pos ? $name : substr($name, $pos + 1);
  124. }
  125. /**
  126. * Finds and registers Commands.
  127. *
  128. * Override this method if your bundle commands do not follow the conventions:
  129. *
  130. * * Commands are in the 'Command' sub-directory
  131. * * Commands extend Symfony\Component\Console\Command\Command
  132. */
  133. public function registerCommands(Application $application)
  134. {
  135. if (!is_dir($dir = $this->getPath().'/Command')) {
  136. return;
  137. }
  138. if (!class_exists('Symfony\Component\Finder\Finder')) {
  139. throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
  140. }
  141. $finder = new Finder();
  142. $finder->files()->name('*Command.php')->in($dir);
  143. $prefix = $this->getNamespace().'\\Command';
  144. foreach ($finder as $file) {
  145. $ns = $prefix;
  146. if ($relativePath = $file->getRelativePath()) {
  147. $ns .= '\\'.str_replace('/', '\\', $relativePath);
  148. }
  149. $class = $ns.'\\'.$file->getBasename('.php');
  150. if ($this->container) {
  151. $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
  152. if ($this->container->has($alias)) {
  153. continue;
  154. }
  155. }
  156. $r = new \ReflectionClass($class);
  157. if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
  158. $application->add($r->newInstance());
  159. }
  160. }
  161. }
  162. /**
  163. * Returns the bundle's container extension class.
  164. *
  165. * @return string
  166. */
  167. protected function getContainerExtensionClass()
  168. {
  169. $basename = preg_replace('/Bundle$/', '', $this->getName());
  170. return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  171. }
  172. /**
  173. * Creates the bundle's container extension.
  174. *
  175. * @return ExtensionInterface|null
  176. */
  177. protected function createContainerExtension()
  178. {
  179. if (class_exists($class = $this->getContainerExtensionClass())) {
  180. return new $class();
  181. }
  182. }
  183. }