Bundle.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Console\Application;
  14. use Symfony\Component\Finder\Finder;
  15. /**
  16. * An implementation of BundleInterface that adds a few conventions
  17. * for DependencyInjection extensions and Console commands.
  18. *
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. */
  21. abstract class Bundle extends ContainerAware implements BundleInterface
  22. {
  23. protected $name;
  24. /**
  25. * Boots the Bundle.
  26. */
  27. public function boot()
  28. {
  29. }
  30. /**
  31. * Shutdowns the Bundle.
  32. */
  33. public function shutdown()
  34. {
  35. }
  36. /**
  37. * Returns the bundle parent name.
  38. *
  39. * @return string The Bundle parent name it overrides or null if no parent
  40. */
  41. public function getParent()
  42. {
  43. return null;
  44. }
  45. /**
  46. * Returns the bundle name (the class short name).
  47. *
  48. * @return string The Bundle name
  49. */
  50. final public function getName()
  51. {
  52. if (null !== $this->name) {
  53. return $this->name;
  54. }
  55. $pos = strrpos(get_class($this), '\\');
  56. return $this->name = substr(get_class($this), $pos ? $pos + 1 : 0);
  57. }
  58. /**
  59. * Finds and registers Dependency Injection Container extensions.
  60. *
  61. * Override this method if your DIC extensions do not follow the conventions:
  62. *
  63. * * Extensions are in the 'DependencyInjection/' sub-directory
  64. * * Extension class names ends with 'Extension'
  65. *
  66. * @param ContainerBuilder $container A ContainerBuilder instance
  67. */
  68. public function registerExtensions(ContainerBuilder $container)
  69. {
  70. if (!$dir = realpath($this->getPath().'/DependencyInjection')) {
  71. return;
  72. }
  73. $finder = new Finder();
  74. $finder->files()->name('*Extension.php')->in($dir);
  75. $prefix = $this->getNamespace().'\\DependencyInjection';
  76. foreach ($finder as $file) {
  77. $class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php');
  78. $container->registerExtension(new $class());
  79. }
  80. }
  81. /**
  82. * Finds and registers Commands.
  83. *
  84. * Override this method if your bundle commands do not follow the conventions:
  85. *
  86. * * Commands are in the 'Command' sub-directory
  87. * * Commands extend Symfony\Component\Console\Command\Command
  88. *
  89. * @param Application $application An Application instance
  90. */
  91. public function registerCommands(Application $application)
  92. {
  93. if (!$dir = realpath($this->getPath().'/Command')) {
  94. return;
  95. }
  96. $finder = new Finder();
  97. $finder->files()->name('*Command.php')->in($dir);
  98. $prefix = $this->getNamespace().'\\Command';
  99. foreach ($finder as $file) {
  100. $r = new \ReflectionClass($prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.$file->getBasename('.php'));
  101. if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract()) {
  102. $application->add($r->newInstance());
  103. }
  104. }
  105. }
  106. }