PageRenderTime 20ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Bundle/Bundle.php

https://gitlab.com/daniruizcamacho/pfcascensores
PHP | 203 lines | 127 code | 14 blank | 62 comment | 5 complexity | 034988ec8522419fef54715968ff5640 MD5 | raw file
  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. $basename = preg_replace('/Bundle$/', '', $this->getName());
  68. $class = $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
  69. if (class_exists($class)) {
  70. $extension = new $class();
  71. // check naming convention
  72. $expectedAlias = Container::underscore($basename);
  73. if ($expectedAlias != $extension->getAlias()) {
  74. throw new \LogicException(sprintf(
  75. 'The extension alias for the default extension of a '.
  76. 'bundle must be the underscored version of the '.
  77. 'bundle name ("%s" instead of "%s")',
  78. $expectedAlias, $extension->getAlias()
  79. ));
  80. }
  81. $this->extension = $extension;
  82. } else {
  83. $this->extension = false;
  84. }
  85. }
  86. if ($this->extension) {
  87. return $this->extension;
  88. }
  89. }
  90. /**
  91. * Gets the Bundle namespace.
  92. *
  93. * @return string The Bundle namespace
  94. *
  95. * @api
  96. */
  97. public function getNamespace()
  98. {
  99. $class = get_class($this);
  100. return substr($class, 0, strrpos($class, '\\'));
  101. }
  102. /**
  103. * Gets the Bundle directory path.
  104. *
  105. * @return string The Bundle absolute path
  106. *
  107. * @api
  108. */
  109. public function getPath()
  110. {
  111. if (null === $this->path) {
  112. $reflected = new \ReflectionObject($this);
  113. $this->path = dirname($reflected->getFileName());
  114. }
  115. return $this->path;
  116. }
  117. /**
  118. * Returns the bundle parent name.
  119. *
  120. * @return string The Bundle parent name it overrides or null if no parent
  121. *
  122. * @api
  123. */
  124. public function getParent()
  125. {
  126. return null;
  127. }
  128. /**
  129. * Returns the bundle name (the class short name).
  130. *
  131. * @return string The Bundle name
  132. *
  133. * @api
  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. $finder = new Finder();
  160. $finder->files()->name('*Command.php')->in($dir);
  161. $prefix = $this->getNamespace().'\\Command';
  162. foreach ($finder as $file) {
  163. $ns = $prefix;
  164. if ($relativePath = $file->getRelativePath()) {
  165. $ns .= '\\'.strtr($relativePath, '/', '\\');
  166. }
  167. $class = $ns.'\\'.$file->getBasename('.php');
  168. if ($this->container) {
  169. $alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
  170. if ($this->container->has($alias)) {
  171. continue;
  172. }
  173. }
  174. $r = new \ReflectionClass($class);
  175. if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
  176. $application->add($r->newInstance());
  177. }
  178. }
  179. }
  180. }