Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MagentoComposerApplication.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Composer;
8 
9 use Composer\Console\Application;
10 use Composer\IO\BufferIO;
11 use Composer\Factory as ComposerFactory;
12 use Symfony\Component\Console\Output\BufferedOutput;
13 
21 {
22 
23  const COMPOSER_WORKING_DIR = '--working-dir';
24 
30  private $composerHome;
31 
37  private $composerJson;
38 
44  private $consoleOutput;
45 
49  private $consoleArrayInputFactory;
50 
54  private $consoleApplication;
55 
65  public function __construct(
66  $pathToComposerHome,
67  $pathToComposerJson,
68  Application $consoleApplication = null,
69  ConsoleArrayInputFactory $consoleArrayInputFactory = null,
70  BufferedOutput $consoleOutput = null
71  ) {
72  $this->consoleApplication = $consoleApplication ? $consoleApplication : new Application();
73  $this->consoleArrayInputFactory = $consoleArrayInputFactory ? $consoleArrayInputFactory
75  $this->consoleOutput = $consoleOutput ? $consoleOutput : new BufferedOutput();
76 
77  $this->composerJson = $pathToComposerJson;
78  $this->composerHome = $pathToComposerHome;
79 
80  putenv('COMPOSER_HOME=' . $pathToComposerHome);
81 
82  $this->consoleApplication->setAutoExit(false);
83  }
84 
91  public function createComposer()
92  {
93  return ComposerFactory::create(new BufferIO(), $this->composerJson);
94  }
95 
104  public function runComposerCommand(array $commandParams, $workingDir = null)
105  {
106  $this->consoleApplication->resetComposer();
107 
108  if ($workingDir) {
109  $commandParams[self::COMPOSER_WORKING_DIR] = $workingDir;
110  } else {
111  $commandParams[self::COMPOSER_WORKING_DIR] = dirname($this->composerJson);
112  }
113 
114  $input = $this->consoleArrayInputFactory->create($commandParams);
115 
116  $exitCode = $this->consoleApplication->run($input, $this->consoleOutput);
117 
118  if ($exitCode) {
119  throw new \RuntimeException(
120  sprintf('Command "%s" failed: %s', $commandParams['command'], $this->consoleOutput->fetch())
121  );
122  }
123 
124  return $this->consoleOutput->fetch();
125  }
126 }
__construct( $pathToComposerHome, $pathToComposerJson, Application $consoleApplication=null, ConsoleArrayInputFactory $consoleArrayInputFactory=null, BufferedOutput $consoleOutput=null)
runComposerCommand(array $commandParams, $workingDir=null)