PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-kernel/Bundle/Bundle.php

https://bitbucket.org/ondemosite/sompo.motor
PHP | 216 lines | 128 code | 29 blank | 59 comment | 19 complexity | 8bd3fc1b1d16d5baad8883ed0630b9f1 MD5 | raw file
Possible License(s): LGPL-2.0, MIT, LGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, GPL-3.0, BSD-3-Clause
  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\Console\Application;
  12. use Symfony\Component\DependencyInjection\Container;
  13. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  16. use Symfony\Component\Finder\Finder;
  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. * {@inheritdoc}
  32. */
  33. public function boot()
  34. {
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function shutdown()
  40. {
  41. }
  42. /**
  43. * {@inheritdoc}
  44. *
  45. * This method can be overridden to register compilation passes,
  46. * other extensions, ...
  47. */
  48. public function build(ContainerBuilder $container)
  49. {
  50. }
  51. /**
  52. * Returns the bundle's container extension.
  53. *
  54. * @return ExtensionInterface|null The container extension
  55. *
  56. * @throws \LogicException
  57. */
  58. public function getContainerExtension()
  59. {
  60. if (null === $this->extension) {
  61. $extension = $this->createContainerExtension();
  62. if (null !== $extension) {
  63. if (!$extension instanceof ExtensionInterface) {
  64. throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', \get_class($extension)));
  65. }
  66. // check naming convention
  67. $basename = preg_replace('/Bundle$/', '', $this->getName());
  68. $expectedAlias = Container::underscore($basename);
  69. if ($expectedAlias != $extension->getAlias()) {
  70. throw new \LogicException(sprintf('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.', $expectedAlias, $extension->getAlias()));
  71. }
  72. $this->extension = $extension;
  73. } else {
  74. $this->extension = false;
  75. }
  76. }
  77. if ($this->extension) {
  78. return $this->extension;
  79. }
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getNamespace()
  85. {
  86. if (null === $this->namespace) {
  87. $this->parseClassName();
  88. }
  89. return $this->namespace;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getPath()
  95. {
  96. if (null === $this->path) {
  97. $reflected = new \ReflectionObject($this);
  98. $this->path = \dirname($reflected->getFileName());
  99. }
  100. return $this->path;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getParent()
  106. {
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. final public function getName()
  112. {
  113. if (null === $this->name) {
  114. $this->parseClassName();
  115. }
  116. return $this->name;
  117. }
  118. /**
  119. * Finds and registers Commands.
  120. *
  121. * Override this method if your bundle commands do not follow the conventions:
  122. *
  123. * * Commands are in the 'Command' sub-directory
  124. * * Commands extend Symfony\Component\Console\Command\Command
  125. */
  126. public function registerCommands(Application $application)
  127. {
  128. if (!is_dir($dir = $this->getPath().'/Command')) {
  129. return;
  130. }
  131. if (!class_exists('Symfony\Component\Finder\Finder')) {
  132. throw new \RuntimeException('You need the symfony/finder component to register bundle commands.');
  133. }
  134. $finder = new Finder();
  135. $finder->files()->name('*Command.php')->in($dir);
  136. $prefix = $this->getNamespace().'\\Command';
  137. foreach ($finder as $file) {
  138. $ns = $prefix;
  139. if ($relativePath = $file->getRelativePath()) {
  140. $ns .= '\\'.str_replace('/', '\\', $relativePath);
  141. }
  142. $class = $ns.'\\'.$file->getBasename('.php');
  143. if ($this->container) {
  144. $commandIds = $this->container->hasParameter('console.command.ids') ? $this->container->getParameter('console.command.ids') : array();
  145. $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
  146. if (isset($commandIds[$alias]) || $this->container->has($alias)) {
  147. continue;
  148. }
  149. }
  150. $r = new \ReflectionClass($class);
  151. if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
  152. @trigger_error(sprintf('Auto-registration of the command "%s" is deprecated since Symfony 3.4 and won\'t be supported in 4.0. Use PSR-4 based service discovery instead.', $class), E_USER_DEPRECATED);
  153. $application->add($r->newInstance());
  154. }
  155. }
  156. }
  157. /**
  158. * Returns the bundle's container extension class.
  159. *
  160. * @return string
  161. */
  162. protected function getContainerExtensionClass()
  163. {
  164. $basename = preg_replace('/Bundle$/', '', $this->getName());
  165. return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  166. }
  167. /**
  168. * Creates the bundle's container extension.
  169. *
  170. * @return ExtensionInterface|null
  171. */
  172. protected function createContainerExtension()
  173. {
  174. if (class_exists($class = $this->getContainerExtensionClass())) {
  175. return new $class();
  176. }
  177. }
  178. private function parseClassName()
  179. {
  180. $pos = strrpos(static::class, '\\');
  181. $this->namespace = false === $pos ? '' : substr(static::class, 0, $pos);
  182. if (null === $this->name) {
  183. $this->name = false === $pos ? static::class : substr(static::class, $pos + 1);
  184. }
  185. }
  186. }