Нет описания

Bundle.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 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. $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
  162. if ($this->container->has($alias)) {
  163. continue;
  164. }
  165. }
  166. $r = new \ReflectionClass($class);
  167. if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
  168. $application->add($r->newInstance());
  169. }
  170. }
  171. }
  172. /**
  173. * Returns the bundle's container extension class.
  174. *
  175. * @return string
  176. */
  177. protected function getContainerExtensionClass()
  178. {
  179. $basename = preg_replace('/Bundle$/', '', $this->getName());
  180. return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  181. }
  182. /**
  183. * Creates the bundle's container extension.
  184. *
  185. * @return ExtensionInterface|null
  186. */
  187. protected function createContainerExtension()
  188. {
  189. if (class_exists($class = $this->getContainerExtensionClass())) {
  190. return new $class();
  191. }
  192. }
  193. private function parseClassName()
  194. {
  195. $pos = strrpos(static::class, '\\');
  196. $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
  197. if (null === $this->name) {
  198. $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);
  199. }
  200. }
  201. }