BaseCommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Config;
  14. use Composer\Console\Application;
  15. use Composer\Factory;
  16. use Composer\IO\IOInterface;
  17. use Composer\IO\NullIO;
  18. use Composer\Plugin\PreCommandRunEvent;
  19. use Composer\Plugin\PluginEvents;
  20. use Symfony\Component\Console\Input\InputInterface;
  21. use Symfony\Component\Console\Output\OutputInterface;
  22. use Symfony\Component\Console\Command\Command;
  23. /**
  24. * Base class for Composer commands
  25. *
  26. * @author Ryan Weaver <ryan@knplabs.com>
  27. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  28. */
  29. abstract class BaseCommand extends Command
  30. {
  31. /**
  32. * @var Composer
  33. */
  34. private $composer;
  35. /**
  36. * @var IOInterface
  37. */
  38. private $io;
  39. /**
  40. * @param bool $required
  41. * @param bool|null $disablePlugins
  42. * @throws \RuntimeException
  43. * @return Composer
  44. */
  45. public function getComposer($required = true, $disablePlugins = null)
  46. {
  47. if (null === $this->composer) {
  48. $application = $this->getApplication();
  49. if ($application instanceof Application) {
  50. /* @var $application Application */
  51. $this->composer = $application->getComposer($required, $disablePlugins);
  52. } elseif ($required) {
  53. throw new \RuntimeException(
  54. 'Could not create a Composer\Composer instance, you must inject '.
  55. 'one if this command is not used with a Composer\Console\Application instance'
  56. );
  57. }
  58. }
  59. return $this->composer;
  60. }
  61. /**
  62. * @param Composer $composer
  63. */
  64. public function setComposer(Composer $composer)
  65. {
  66. $this->composer = $composer;
  67. }
  68. /**
  69. * Removes the cached composer instance
  70. */
  71. public function resetComposer()
  72. {
  73. $this->composer = null;
  74. $this->getApplication()->resetComposer();
  75. }
  76. /**
  77. * Whether or not this command is meant to call another command.
  78. *
  79. * This is mainly needed to avoid duplicated warnings messages.
  80. *
  81. * @return bool
  82. */
  83. public function isProxyCommand()
  84. {
  85. return false;
  86. }
  87. /**
  88. * @return IOInterface
  89. */
  90. public function getIO()
  91. {
  92. if (null === $this->io) {
  93. $application = $this->getApplication();
  94. if ($application instanceof Application) {
  95. /* @var $application Application */
  96. $this->io = $application->getIO();
  97. } else {
  98. $this->io = new NullIO();
  99. }
  100. }
  101. return $this->io;
  102. }
  103. /**
  104. * @param IOInterface $io
  105. */
  106. public function setIO(IOInterface $io)
  107. {
  108. $this->io = $io;
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. protected function initialize(InputInterface $input, OutputInterface $output)
  114. {
  115. // initialize a plugin-enabled Composer instance, either local or global
  116. $disablePlugins = $input->hasParameterOption('--no-plugins');
  117. $composer = $this->getComposer(false, $disablePlugins);
  118. if (null === $composer) {
  119. $composer = Factory::createGlobal($this->getIO(), false);
  120. }
  121. if ($composer) {
  122. $preCommandRunEvent = new PreCommandRunEvent(PluginEvents::PRE_COMMAND_RUN, $input, $this->getName());
  123. $composer->getEventDispatcher()->dispatch($preCommandRunEvent->getName(), $preCommandRunEvent);
  124. }
  125. if (true === $input->hasParameterOption(array('--no-ansi')) && $input->hasOption('no-progress')) {
  126. $input->setOption('no-progress', true);
  127. }
  128. parent::initialize($input, $output);
  129. }
  130. /**
  131. * Returns preferSource and preferDist values based on the configuration.
  132. *
  133. * @param Config $config
  134. * @param InputInterface $input
  135. * @param bool $keepVcsRequiresPreferSource
  136. *
  137. * @return bool[] An array composed of the preferSource and preferDist values
  138. */
  139. protected function getPreferredInstallOptions(Config $config, InputInterface $input, $keepVcsRequiresPreferSource = false)
  140. {
  141. $preferSource = false;
  142. $preferDist = false;
  143. switch ($config->get('preferred-install')) {
  144. case 'source':
  145. $preferSource = true;
  146. break;
  147. case 'dist':
  148. $preferDist = true;
  149. break;
  150. case 'auto':
  151. default:
  152. // noop
  153. break;
  154. }
  155. if ($input->getOption('prefer-source') || $input->getOption('prefer-dist') || ($keepVcsRequiresPreferSource && $input->hasOption('keep-vcs') && $input->getOption('keep-vcs'))) {
  156. $preferSource = $input->getOption('prefer-source') || ($keepVcsRequiresPreferSource && $input->hasOption('keep-vcs') && $input->getOption('keep-vcs'));
  157. $preferDist = $input->getOption('prefer-dist');
  158. }
  159. return array($preferSource, $preferDist);
  160. }
  161. }