Bundle.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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\Finder\Finder;
  16. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  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. use ContainerAwareTrait;
  26. protected $name;
  27. protected $extension;
  28. protected $path;
  29. private $namespace;
  30. /**
  31. * Boots the Bundle.
  32. */
  33. public function boot()
  34. {
  35. }
  36. /**
  37. * Shutdowns the Bundle.
  38. */
  39. public function shutdown()
  40. {
  41. }
  42. /**
  43. * Builds the bundle.
  44. *
  45. * It is only ever called once when the cache is empty.
  46. *
  47. * This method can be overridden to register compilation passes,
  48. * other extensions, ...
  49. *
  50. * @param ContainerBuilder $container A ContainerBuilder instance
  51. */
  52. public function build(ContainerBuilder $container)
  53. {
  54. }
  55. /**
  56. * Returns the bundle's container extension.
  57. *
  58. * @return ExtensionInterface|null The container extension
  59. *
  60. * @throws \LogicException
  61. */
  62. public function getContainerExtension()
  63. {
  64. if (null === $this->extension) {
  65. $extension = $this->createContainerExtension();
  66. if (null !== $extension) {
  67. if (!$extension instanceof ExtensionInterface) {
  68. throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
  69. }
  70. // check naming convention
  71. $basename = preg_replace('/Bundle$/', '', $this->getName());
  72. $expectedAlias = Container::underscore($basename);
  73. if ($expectedAlias != $extension->getAlias()) {
  74. throw new \LogicException(sprintf(
  75. '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.',
  76. $expectedAlias, $extension->getAlias()
  77. ));
  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. * Gets the Bundle namespace.
  90. *
  91. * @return string The Bundle namespace
  92. */
  93. public function getNamespace()
  94. {
  95. if (null === $this->namespace) {
  96. $this->parseClassName();
  97. }
  98. return $this->namespace;
  99. }
  100. /**
  101. * Gets the Bundle directory path.
  102. *
  103. * @return string The Bundle absolute path
  104. */
  105. public function getPath()
  106. {
  107. if (null === $this->path) {
  108. $reflected = new \ReflectionObject($this);
  109. $this->path = dirname($reflected->getFileName());
  110. }
  111. return $this->path;
  112. }
  113. /**
  114. * Returns the bundle parent name.
  115. *
  116. * @return string|null The Bundle parent name it overrides or null if no parent
  117. */
  118. public function getParent()
  119. {
  120. }
  121. /**
  122. * Returns the bundle name (the class short name).
  123. *
  124. * @return string The Bundle name
  125. */
  126. final public function getName()
  127. {
  128. if (null === $this->name) {
  129. $this->parseClassName();
  130. }
  131. return $this->name;
  132. }
  133. /**
  134. * Finds and registers Commands.
  135. *
  136. * Override this method if your bundle commands do not follow the conventions:
  137. *
  138. * * Commands are in the 'Command' sub-directory
  139. * * Commands extend Symfony\Component\Console\Command\Command
  140. *
  141. * @param Application $application An Application instance
  142. */
  143. public function registerCommands(Application $application)
  144. {
  145. if (!is_dir($dir = $this->getPath().'/Command')) {
  146. return;
  147. }
  148. if (!class_exists('Symfony\Component\Finder\Finder')) {
  149. throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
  150. }
  151. $finder = new Finder();
  152. $finder->files()->name('*Command.php')->in($dir);
  153. $prefix = $this->getNamespace().'\\Command';
  154. foreach ($finder as $file) {
  155. $ns = $prefix;
  156. if ($relativePath = $file->getRelativePath()) {
  157. $ns .= '\\'.str_replace('/', '\\', $relativePath);
  158. }
  159. $class = $ns.'\\'.$file->getBasename('.php');
  160. if ($this->container) {
  161. $commandIds = $this->container->hasParameter('console.command.ids') ? $this->container->getParameter('console.command.ids') : array();
  162. $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
  163. if (isset($commandIds[$alias]) || $this->container->has($alias)) {
  164. continue;
  165. }
  166. }
  167. $r = new \ReflectionClass($class);
  168. if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
  169. $application->add($r->newInstance());
  170. }
  171. }
  172. }
  173. /**
  174. * Returns the bundle's container extension class.
  175. *
  176. * @return string
  177. */
  178. protected function getContainerExtensionClass()
  179. {
  180. $basename = preg_replace('/Bundle$/', '', $this->getName());
  181. return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  182. }
  183. /**
  184. * Creates the bundle's container extension.
  185. *
  186. * @return ExtensionInterface|null
  187. */
  188. protected function createContainerExtension()
  189. {
  190. if (class_exists($class = $this->getContainerExtensionClass())) {
  191. return new $class();
  192. }
  193. }
  194. private function parseClassName()
  195. {
  196. $pos = strrpos(static::class, '\\');
  197. $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
  198. if (null === $this->name) {
  199. $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);
  200. }
  201. }
  202. }