Command.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Command\Command as BaseCommand;
  17. /**
  18. * Base class for Composer commands
  19. *
  20. * @author Ryan Weaver <ryan@knplabs.com>
  21. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  22. */
  23. abstract class Command extends BaseCommand
  24. {
  25. /**
  26. * @var Composer
  27. */
  28. private $composer;
  29. /**
  30. * @var IOInterface
  31. */
  32. private $io;
  33. /**
  34. * @param bool $required
  35. * @param bool $disablePlugins
  36. * @throws \RuntimeException
  37. * @return Composer
  38. */
  39. public function getComposer($required = true, $disablePlugins = false)
  40. {
  41. if (null === $this->composer) {
  42. $application = $this->getApplication();
  43. if ($application instanceof Application) {
  44. /* @var $application Application */
  45. $this->composer = $application->getComposer($required, $disablePlugins);
  46. } elseif ($required) {
  47. throw new \RuntimeException(
  48. 'Could not create a Composer\Composer instance, you must inject '.
  49. 'one if this command is not used with a Composer\Console\Application instance'
  50. );
  51. }
  52. }
  53. return $this->composer;
  54. }
  55. /**
  56. * @param Composer $composer
  57. */
  58. public function setComposer(Composer $composer)
  59. {
  60. $this->composer = $composer;
  61. }
  62. /**
  63. * @return IOInterface
  64. */
  65. public function getIO()
  66. {
  67. if (null === $this->io) {
  68. $application = $this->getApplication();
  69. if ($application instanceof Application) {
  70. /* @var $application Application */
  71. $this->io = $application->getIO();
  72. } else {
  73. $this->io = new NullIO();
  74. }
  75. }
  76. return $this->io;
  77. }
  78. /**
  79. * @param IOInterface $io
  80. */
  81. public function setIO(IOInterface $io)
  82. {
  83. $this->io = $io;
  84. }
  85. }