PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Bundle/Bundle.php

https://gitlab.com/ineszribi/SmartBookStoreWeb
PHP | 211 lines | 128 code | 19 blank | 64 comment | 12 complexity | 955b5995942fcb044d8dbcc4faa9da23 MD5 | raw file
Possible License(s): 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\DependencyInjection\ContainerAware;
  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. * @api
  24. */
  25. abstract class Bundle extends ContainerAware implements BundleInterface
  26. {
  27. protected $name;
  28. protected $extension;
  29. protected $path;
  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. * @api
  63. */
  64. public function getContainerExtension()
  65. {
  66. if (null === $this->extension) {
  67. $class = $this->getContainerExtensionClass();
  68. if (class_exists($class)) {
  69. $extension = new $class();
  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. * @api
  94. */
  95. public function getNamespace()
  96. {
  97. $class = get_class($this);
  98. return substr($class, 0, strrpos($class, '\\'));
  99. }
  100. /**
  101. * Gets the Bundle directory path.
  102. *
  103. * @return string The Bundle absolute path
  104. *
  105. * @api
  106. */
  107. public function getPath()
  108. {
  109. if (null === $this->path) {
  110. $reflected = new \ReflectionObject($this);
  111. $this->path = dirname($reflected->getFileName());
  112. }
  113. return $this->path;
  114. }
  115. /**
  116. * Returns the bundle parent name.
  117. *
  118. * @return string The Bundle parent name it overrides or null if no parent
  119. *
  120. * @api
  121. */
  122. public function getParent()
  123. {
  124. }
  125. /**
  126. * Returns the bundle name (the class short name).
  127. *
  128. * @return string The Bundle name
  129. *
  130. * @api
  131. */
  132. final public function getName()
  133. {
  134. if (null !== $this->name) {
  135. return $this->name;
  136. }
  137. $name = get_class($this);
  138. $pos = strrpos($name, '\\');
  139. return $this->name = false === $pos ? $name : substr($name, $pos + 1);
  140. }
  141. /**
  142. * Finds and registers Commands.
  143. *
  144. * Override this method if your bundle commands do not follow the conventions:
  145. *
  146. * * Commands are in the 'Command' sub-directory
  147. * * Commands extend Symfony\Component\Console\Command\Command
  148. *
  149. * @param Application $application An Application instance
  150. */
  151. public function registerCommands(Application $application)
  152. {
  153. if (!is_dir($dir = $this->getPath().'/Command')) {
  154. return;
  155. }
  156. $finder = new Finder();
  157. $finder->files()->name('*Command.php')->in($dir);
  158. $prefix = $this->getNamespace().'\\Command';
  159. foreach ($finder as $file) {
  160. $ns = $prefix;
  161. if ($relativePath = $file->getRelativePath()) {
  162. $ns .= '\\'.strtr($relativePath, '/', '\\');
  163. }
  164. $class = $ns.'\\'.$file->getBasename('.php');
  165. if ($this->container) {
  166. $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
  167. if ($this->container->has($alias)) {
  168. continue;
  169. }
  170. }
  171. $r = new \ReflectionClass($class);
  172. if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
  173. $application->add($r->newInstance());
  174. }
  175. }
  176. }
  177. /**
  178. * Returns the bundle's container extension class.
  179. *
  180. * @return string
  181. */
  182. protected function getContainerExtensionClass()
  183. {
  184. $basename = preg_replace('/Bundle$/', '', $this->getName());
  185. return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  186. }
  187. }