菜谱项目

Bundle.php 6.5KB

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