1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the composer-changelogs project. |
5
|
|
|
* |
6
|
|
|
* (c) Loïck Piera <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Pyrech\ComposerChangelogs; |
13
|
|
|
|
14
|
|
|
use Composer\Composer; |
15
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
use Composer\Installer\PackageEvent; |
17
|
|
|
use Composer\Installer\PackageEvents; |
18
|
|
|
use Composer\IO\IOInterface; |
19
|
|
|
use Composer\Plugin\PluginInterface; |
20
|
|
|
use Composer\Script\Event; |
21
|
|
|
use Composer\Script\ScriptEvents; |
22
|
|
|
use Pyrech\ComposerChangelogs\Config\Config; |
23
|
|
|
use Pyrech\ComposerChangelogs\Config\ConfigBuilder; |
24
|
|
|
use Pyrech\ComposerChangelogs\Config\ConfigLocator; |
25
|
|
|
|
26
|
|
|
class ChangelogsPlugin implements PluginInterface, EventSubscriberInterface |
27
|
|
|
{ |
28
|
|
|
const EXTRA_KEY = 'composer-changelogs'; |
29
|
|
|
|
30
|
|
|
/** @var Composer */ |
31
|
|
|
private $composer; |
32
|
|
|
|
33
|
|
|
/** @var IOInterface */ |
34
|
|
|
private $io; |
35
|
|
|
|
36
|
|
|
/** @var Outputter */ |
37
|
|
|
private $outputter; |
38
|
|
|
|
39
|
|
|
/** @var ConfigLocator */ |
40
|
|
|
private $configLocator; |
41
|
|
|
|
42
|
|
|
/** @var Config */ |
43
|
|
|
private $config; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
6 |
|
public function activate(Composer $composer, IOInterface $io) |
49
|
|
|
{ |
50
|
6 |
|
$this->composer = $composer; |
51
|
6 |
|
$this->io = $io; |
52
|
6 |
|
$this->outputter = Factory::createOutputter(); |
53
|
6 |
|
$this->configLocator = new ConfigLocator($composer); |
54
|
|
|
|
55
|
6 |
|
$this->setupConfig(); |
56
|
6 |
|
$this->autoloadNeededClasses(); |
57
|
6 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
3 |
|
public static function getSubscribedEvents() |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
2 |
|
PackageEvents::POST_PACKAGE_UPDATE => [ |
66
|
2 |
|
['postPackageOperation'], |
67
|
2 |
|
], |
68
|
2 |
|
PackageEvents::POST_PACKAGE_INSTALL => [ |
69
|
2 |
|
['postPackageOperation'], |
70
|
2 |
|
], |
71
|
2 |
|
PackageEvents::POST_PACKAGE_UNINSTALL => [ |
72
|
2 |
|
['postPackageOperation'], |
73
|
2 |
|
], |
74
|
2 |
|
ScriptEvents::POST_UPDATE_CMD => [ |
75
|
2 |
|
['postUpdate'], |
76
|
2 |
|
], |
77
|
2 |
|
]; |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param PackageEvent $event |
82
|
|
|
*/ |
83
|
5 |
|
public function postPackageOperation(PackageEvent $event) |
84
|
|
|
{ |
85
|
5 |
|
$operation = $event->getOperation(); |
86
|
|
|
|
87
|
5 |
|
$this->outputter->addOperation($operation); |
88
|
5 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Event $event |
92
|
|
|
*/ |
93
|
5 |
|
public function postUpdate(Event $event) |
|
|
|
|
94
|
|
|
{ |
95
|
5 |
|
$this->io->write($this->outputter->getOutput()); |
96
|
|
|
|
97
|
5 |
|
$this->handleCommit(); |
98
|
5 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* This method ensures all the classes required to make the plugin working |
102
|
|
|
* are loaded. |
103
|
|
|
* |
104
|
|
|
* It's required to avoid composer looking for classes no longer existing |
105
|
|
|
* (after the plugin is updated or removed for example). |
106
|
|
|
* |
107
|
|
|
* Lot of classes (like operation handlers, url generators, Outputter, etc) |
108
|
|
|
* do not need this because they are already autoloaded at the activation |
109
|
|
|
* of the plugin. |
110
|
|
|
*/ |
111
|
6 |
|
private function autoloadNeededClasses() |
112
|
|
|
{ |
113
|
|
|
$classes = [ |
114
|
6 |
|
'Pyrech\ComposerChangelogs\Version', |
115
|
6 |
|
]; |
116
|
|
|
|
117
|
6 |
|
foreach ($classes as $class) { |
118
|
|
|
// Force the class to be autoloaded |
119
|
6 |
|
class_exists($class, true); |
120
|
6 |
|
} |
121
|
6 |
|
} |
122
|
|
|
|
123
|
6 |
|
private function setupConfig() |
124
|
|
|
{ |
125
|
6 |
|
$builder = new ConfigBuilder(); |
126
|
|
|
|
127
|
6 |
|
$this->config = $builder->build( |
128
|
6 |
|
$this->configLocator->getConfig(self::EXTRA_KEY), |
129
|
6 |
|
$this->configLocator->getPath(self::EXTRA_KEY) |
130
|
6 |
|
); |
131
|
|
|
|
132
|
6 |
|
if (count($builder->getWarnings()) > 0) { |
133
|
|
|
$this->io->writeError('<error>Invalid config for composer-changelogs plugin:</error>'); |
134
|
|
|
foreach ($builder->getWarnings() as $warning) { |
135
|
|
|
$this->io->write(' ' . $warning); |
136
|
|
|
} |
137
|
|
|
} |
138
|
6 |
|
} |
139
|
|
|
|
140
|
5 |
|
private function handleCommit() |
141
|
|
|
{ |
142
|
5 |
|
if ($this->outputter->isEmpty()) { |
143
|
|
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
5 |
|
switch ($this->config->getCommitAuto()) { |
147
|
5 |
|
case 'never': |
148
|
3 |
|
return; |
149
|
2 |
|
case 'ask': |
150
|
|
|
if ($this->io->askConfirmation('<info>Would you like to commit the update? </info>[<comment>no</comment>]: ', false)) { |
151
|
|
|
$this->doCommit(); |
152
|
|
|
} |
153
|
|
|
break; |
154
|
2 |
|
case 'always': |
155
|
2 |
|
$this->doCommit(); |
156
|
2 |
|
} |
157
|
2 |
|
} |
158
|
|
|
|
159
|
2 |
|
private function doCommit() |
160
|
|
|
{ |
161
|
2 |
|
if (!$this->config->getCommitBinFile()) { |
|
|
|
|
162
|
|
|
$this->io->writeError('<error>No "commit-bin-file" for composer-changelogs plugin. Commit not done.</error>'); |
163
|
|
|
|
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
2 |
|
$workingDirectory = getcwd(); |
168
|
2 |
|
$filename = tempnam(sys_get_temp_dir(), 'composer-changelogs-'); |
169
|
2 |
|
$message = 'Update dependencies' . PHP_EOL . PHP_EOL . strip_tags($this->outputter->getOutput()); |
170
|
|
|
|
171
|
2 |
|
file_put_contents($filename, $message); |
172
|
|
|
|
173
|
2 |
|
$command = $this->config->getCommitBinFile() . ' ' . escapeshellarg($workingDirectory) . ' ' . escapeshellarg($filename); |
174
|
|
|
|
175
|
2 |
|
$this->io->write(sprintf('Executing following command: %s', $command)); |
176
|
2 |
|
exec($command); |
177
|
2 |
|
} |
178
|
|
|
} |
179
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.