-
Notifications
You must be signed in to change notification settings - Fork 2
/
ComposerCreator.php
84 lines (70 loc) · 2.19 KB
/
ComposerCreator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace Dex\Composer\PlugAndPlay\Commands;
use Composer\Composer;
use Composer\Console\Application;
use Composer\Json\JsonValidationException;
use Dex\Composer\PlugAndPlay\Composer\Factory;
use RuntimeException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
trait ComposerCreator
{
/**
* @var Composer
*/
protected $composer;
/**
* @var bool
*/
protected $usePlugAndPlay = false;
/**
* @param bool $required
* @param bool|null $disablePlugins
*
* @throws JsonValidationException|RuntimeException
*
* @return Composer
*/
public function getComposer($required = true, $disablePlugins = null)
{
$composer = $this->composer;
if ($this->usePlugAndPlay && is_null($composer)) {
$application = $this->getApplication();
if ($application instanceof Application) {
$composer = Factory::create($application->getIO(), null, $disablePlugins);
} elseif ($required) {
throw new RuntimeException(
'Could not create a Composer\Composer instance, you must inject ' .
'one if this command is not used with a Composer\Console\Application instance'
);
}
}
if (is_null($composer)) {
$composer = parent::getComposer($required, $disablePlugins);
}
if ($required) {
$this->composer = $composer;
}
return $composer;
}
/**
* Check if plug and play plugin is running.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$isTheCommand = $input->getFirstArgument() === $this->getName();
if ($isTheCommand) {
$this->usePlugAndPlay = true;
$output->writeln('<info>You are using Composer Plug and Play Plugin.</info>');
}
if ($input->getOption('plug-and-play-pretend')) {
return 0;
}
return parent::execute($input, $output);
}
}