Command.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Composer
  27. */
  28. private $composer;
  29. /**
  30. * @var \Composer\IO\IOInterface
  31. */
  32. private $io;
  33. /**
  34. * @param bool $required
  35. * @return \Composer\Composer
  36. */
  37. public function getComposer($required = true)
  38. {
  39. if (null === $this->composer) {
  40. $application = $this->getApplication();
  41. if ($application instanceof Application) {
  42. /* @var $application Application */
  43. $this->composer = $application->getComposer($required);
  44. } elseif ($required) {
  45. throw new \RuntimeException(
  46. 'Could not create a Composer\Composer instance, you must inject '.
  47. 'one if this command is not used with a Composer\Console\Application instance'
  48. );
  49. }
  50. }
  51. return $this->composer;
  52. }
  53. /**
  54. * @param \Composer\Composer $composer
  55. */
  56. public function setComposer(Composer $composer)
  57. {
  58. $this->composer = $composer;
  59. }
  60. /**
  61. * @return \Composer\IO\IOInterface
  62. */
  63. public function getIO()
  64. {
  65. if (null === $this->io) {
  66. $application = $this->getApplication();
  67. if ($application instanceof Application) {
  68. /* @var $application Application */
  69. $this->io = $application->getIO();
  70. } else {
  71. $this->io = new NullIO();
  72. }
  73. }
  74. return $this->io;
  75. }
  76. /**
  77. * @param \Composer\IO\IOInterface $io
  78. */
  79. public function setIO(IOInterface $io)
  80. {
  81. $this->io = $io;
  82. }
  83. }