Bundle.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /**
  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. * @param ContainerBuilder $container A ContainerBuilder instance
  50. */
  51. public function build(ContainerBuilder $container)
  52. {
  53. }
  54. /**
  55. * Returns the bundle's container extension.
  56. *
  57. * @return ExtensionInterface|null The container extension
  58. *
  59. * @throws \LogicException
  60. */
  61. public function getContainerExtension()
  62. {
  63. if (null === $this->extension) {
  64. $extension = $this->createContainerExtension();
  65. if (null !== $extension) {
  66. if (!$extension instanceof ExtensionInterface) {
  67. throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
  68. }
  69. // check naming convention
  70. $basename = preg_replace('/Bundle$/', '', $this->getName());
  71. $expectedAlias = Container::underscore($basename);
  72. if ($expectedAlias != $extension->getAlias()) {
  73. throw new \LogicException(sprintf(
  74. '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.',
  75. $expectedAlias, $extension->getAlias()
  76. ));
  77. }
  78. $this->extension = $extension;
  79. } else {
  80. $this->extension = false;
  81. }
  82. }
  83. if ($this->extension) {
  84. return $this->extension;
  85. }
  86. }
  87. /**
  88. * Gets the Bundle namespace.
  89. *
  90. * @return string The Bundle namespace
  91. */
  92. public function getNamespace()
  93. {
  94. $class = get_class($this);
  95. return substr($class, 0, strrpos($class, '\\'));
  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 parent name.
  112. *
  113. * @return string The Bundle parent name it overrides or null if no parent
  114. */
  115. public function getParent()
  116. {
  117. }
  118. /**
  119. * Returns the bundle name (the class short name).
  120. *
  121. * @return string The Bundle name
  122. */
  123. final public function getName()
  124. {
  125. if (null !== $this->name) {
  126. return $this->name;
  127. }
  128. $name = get_class($this);
  129. $pos = strrpos($name, '\\');
  130. return $this->name = false === $pos ? $name : substr($name, $pos + 1);
  131. }
  132. /**
  133. * Finds and registers Commands.
  134. *
  135. * Override this method if your bundle commands do not follow the conventions:
  136. *
  137. * * Commands are in the 'Command' sub-directory
  138. * * Commands extend Symfony\Component\Console\Command\Command
  139. *
  140. * @param Application $application An Application instance
  141. */
  142. public function registerCommands(Application $application)
  143. {
  144. if (!is_dir($dir = $this->getPath().'/Command')) {
  145. return;
  146. }
  147. if (!class_exists('Symfony\Component\Finder\Finder')) {
  148. throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
  149. }
  150. $finder = new Finder();
  151. $finder->files()->name('*Command.php')->in($dir);
  152. $prefix = $this->getNamespace().'\\Command';
  153. foreach ($finder as $file) {
  154. $ns = $prefix;
  155. if ($relativePath = $file->getRelativePath()) {
  156. $ns .= '\\'.str_replace('/', '\\', $relativePath);
  157. }
  158. $class = $ns.'\\'.$file->getBasename('.php');
  159. if ($this->container) {
  160. $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
  161. if ($this->container->has($alias)) {
  162. continue;
  163. }
  164. }
  165. $r = new \ReflectionClass($class);
  166. if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
  167. $application->add($r->newInstance());
  168. }
  169. }
  170. }
  171. /**
  172. * Returns the bundle's container extension class.
  173. *
  174. * @return string
  175. */
  176. protected function getContainerExtensionClass()
  177. {
  178. $basename = preg_replace('/Bundle$/', '', $this->getName());
  179. return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  180. }
  181. /**
  182. * Creates the bundle's container extension.
  183. *
  184. * @return ExtensionInterface|null
  185. */
  186. protected function createContainerExtension()
  187. {
  188. if (class_exists($class = $this->getContainerExtensionClass())) {
  189. return new $class();
  190. }
  191. }
  192. }