Drupal investigation

Bundle.php 6.5KB

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