BaseCommand.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Composer\Composer;
  13. use Composer\Console\Application;
  14. use Composer\IO\IOInterface;
  15. use Composer\IO\NullIO;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Command\Command;
  19. /**
  20. * Base class for Composer commands
  21. *
  22. * @author Ryan Weaver <ryan@knplabs.com>
  23. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  24. */
  25. abstract class BaseCommand extends Command
  26. {
  27. /**
  28. * @var Composer
  29. */
  30. private $composer;
  31. /**
  32. * @var IOInterface
  33. */
  34. private $io;
  35. /**
  36. * @param bool $required
  37. * @param bool $disablePlugins
  38. * @throws \RuntimeException
  39. * @return Composer
  40. */
  41. public function getComposer($required = true, $disablePlugins = false)
  42. {
  43. if (null === $this->composer) {
  44. $application = $this->getApplication();
  45. if ($application instanceof Application) {
  46. /* @var $application Application */
  47. $this->composer = $application->getComposer($required, $disablePlugins);
  48. } elseif ($required) {
  49. throw new \RuntimeException(
  50. 'Could not create a Composer\Composer instance, you must inject '.
  51. 'one if this command is not used with a Composer\Console\Application instance'
  52. );
  53. }
  54. }
  55. return $this->composer;
  56. }
  57. /**
  58. * @param Composer $composer
  59. */
  60. public function setComposer(Composer $composer)
  61. {
  62. $this->composer = $composer;
  63. }
  64. /**
  65. * Removes the cached composer instance
  66. */
  67. public function resetComposer()
  68. {
  69. $this->composer = null;
  70. $this->getApplication()->resetComposer();
  71. }
  72. /**
  73. * @return IOInterface
  74. */
  75. public function getIO()
  76. {
  77. if (null === $this->io) {
  78. $application = $this->getApplication();
  79. if ($application instanceof Application) {
  80. /* @var $application Application */
  81. $this->io = $application->getIO();
  82. } else {
  83. $this->io = new NullIO();
  84. }
  85. }
  86. return $this->io;
  87. }
  88. /**
  89. * @param IOInterface $io
  90. */
  91. public function setIO(IOInterface $io)
  92. {
  93. $this->io = $io;
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. protected function initialize(InputInterface $input, OutputInterface $output)
  99. {
  100. if (true === $input->hasParameterOption(array('--no-ansi')) && $input->hasOption('no-progress')) {
  101. $input->setOption('no-progress', true);
  102. }
  103. parent::initialize($input, $output);
  104. }
  105. }