1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Yawik project. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Yawik\Composer; |
12
|
|
|
|
13
|
|
|
use Composer\Composer; |
14
|
|
|
use Composer\DependencyResolver\Operation\InstallOperation; |
15
|
|
|
use Composer\DependencyResolver\Operation\UpdateOperation; |
16
|
|
|
use Composer\DependencyResolver\Operation\UninstallOperation; |
17
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
18
|
|
|
use Composer\Installer\PackageEvent; |
19
|
|
|
use Composer\IO\IOInterface; |
20
|
|
|
use Composer\Package\PackageInterface; |
21
|
|
|
use Composer\Plugin\PluginInterface; |
22
|
|
|
use Composer\Script\Event as ScriptEvent; |
23
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
24
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Plugin |
30
|
|
|
* @package Yawik\Composer |
31
|
|
|
* @author Anthonius Munthi <[email protected]> |
32
|
|
|
* @since 0.32.0 |
33
|
|
|
* @TODO Create more documentation for methods |
34
|
|
|
*/ |
35
|
|
|
class Plugin implements PluginInterface, EventSubscriberInterface |
36
|
|
|
{ |
37
|
|
|
const YAWIK_MODULE_TYPE = 'yawik-module'; |
38
|
|
|
|
39
|
|
|
const SCAN_TYPE_INSTALL = 'install'; |
40
|
|
|
const SCAN_TYPE_REMOVE = 'remove'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Composer |
44
|
|
|
*/ |
45
|
|
|
private $composer; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var IOInterface |
49
|
|
|
*/ |
50
|
|
|
private $output; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* An array list of available modules |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $installed = []; |
58
|
|
|
|
59
|
|
|
private $uninstalled = []; |
60
|
|
|
|
61
|
|
|
private $projectPath; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var AssetsInstaller |
65
|
|
|
*/ |
66
|
|
|
private $assetsInstaller; |
67
|
5 |
|
|
68
|
|
|
/** |
69
|
5 |
|
* @var PermissionsFixer |
70
|
5 |
|
*/ |
71
|
|
|
private $permissionsFixer; |
72
|
4 |
|
|
73
|
|
|
public function __construct($projectPath=null) |
74
|
4 |
|
{ |
75
|
4 |
|
$this->projectPath = is_null($projectPath) ? getcwd():$projectPath; |
76
|
4 |
|
} |
77
|
|
|
|
78
|
|
|
public function activate(Composer $composer, IOInterface $io) |
79
|
|
|
{ |
80
|
|
|
$this->composer = $composer; |
81
|
|
|
$this->output = $io; |
82
|
|
|
} |
83
|
5 |
|
|
84
|
|
|
/** |
85
|
5 |
|
* Define AssetsInstaller to use |
86
|
5 |
|
* This very usefull during testing process |
87
|
|
|
* @param AssetsInstaller $installer |
88
|
|
|
*/ |
89
|
|
|
public function setAssetsInstaller(AssetsInstaller $installer) |
90
|
|
|
{ |
91
|
|
|
if (!is_null($this->output)) { |
92
|
|
|
$installer->setOutputFromComposerIO($this->output); |
93
|
1 |
|
} |
94
|
|
|
$this->assetsInstaller = $installer; |
95
|
|
|
} |
96
|
1 |
|
|
97
|
|
|
/** |
98
|
|
|
* @return AssetsInstaller |
99
|
|
|
*/ |
100
|
|
|
public function getAssetsInstaller() |
101
|
|
|
{ |
102
|
|
|
if (!is_object($this->assetsInstaller)) { |
103
|
4 |
|
$this->setAssetsInstaller(new AssetsInstaller()); |
104
|
|
|
} |
105
|
4 |
|
|
106
|
2 |
|
return $this->assetsInstaller; |
107
|
|
|
} |
108
|
4 |
|
|
109
|
1 |
|
/** |
110
|
|
|
* @return PermissionsFixer |
111
|
|
|
*/ |
112
|
4 |
|
public function getPermissionsFixer() |
113
|
4 |
|
{ |
114
|
|
|
if (is_null($this->permissionsFixer)) { |
115
|
2 |
|
$this->setPermissionsFixer(new PermissionsFixer()); |
116
|
|
|
} |
117
|
2 |
|
return $this->permissionsFixer; |
118
|
2 |
|
} |
119
|
2 |
|
|
120
|
|
|
/** |
121
|
1 |
|
* @param PermissionsFixer $permissionsFixer |
122
|
|
|
* @return Plugin |
123
|
1 |
|
*/ |
124
|
1 |
|
public function setPermissionsFixer($permissionsFixer) |
125
|
1 |
|
{ |
126
|
|
|
if (!is_null($this->output)) { |
127
|
1 |
|
$permissionsFixer->setOutputFromComposerIO($this->output); |
128
|
|
|
} |
129
|
1 |
|
$this->permissionsFixer = $permissionsFixer; |
130
|
1 |
|
return $this; |
131
|
1 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Provide composer event listeners. |
135
|
|
|
* |
136
|
5 |
|
* @return array |
137
|
|
|
*/ |
138
|
5 |
|
public static function getSubscribedEvents() |
139
|
1 |
|
{ |
140
|
1 |
|
return [ |
141
|
1 |
|
'post-autoload-dump' => 'onPostAutoloadDump', |
142
|
1 |
|
'post-package-install' => 'onPostPackageInstall', |
143
|
|
|
'post-package-update' => 'onPostPackageUpdate', |
144
|
1 |
|
'pre-package-uninstall' => 'onPrePackageUninstall' |
145
|
|
|
]; |
146
|
5 |
|
} |
147
|
|
|
|
148
|
|
|
public function onPostAutoloadDump() |
149
|
4 |
|
{ |
150
|
|
|
if (count($this->uninstalled) > 0) { |
151
|
4 |
|
$this->getAssetsInstaller()->uninstall($this->uninstalled); |
152
|
4 |
|
} else { |
153
|
4 |
|
$this->getAssetsInstaller()->install($this->installed); |
154
|
4 |
|
} |
155
|
4 |
|
$this->getPermissionsFixer()->fix(); |
156
|
|
|
} |
157
|
4 |
|
|
158
|
|
|
public function onPostPackageInstall(PackageEvent $event) |
159
|
3 |
|
{ |
160
|
|
|
$package = $event->getOperation()->getPackage(); |
|
|
|
|
161
|
3 |
|
$this->scanModules($package, static::SCAN_TYPE_INSTALL); |
162
|
3 |
|
} |
163
|
2 |
|
|
164
|
|
|
public function onPostPackageUpdate(PackageEvent $event) |
165
|
3 |
|
{ |
166
|
|
|
$package = $event->getOperation()->getTargetPackage(); |
|
|
|
|
167
|
|
|
$this->scanModules($package, static::SCAN_TYPE_INSTALL); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function onPrePackageUninstall(PackageEvent $event) |
171
|
4 |
|
{ |
172
|
|
|
$package = $event->getOperation()->getPackage(); |
173
|
|
|
$this->scanModules($package, static::SCAN_TYPE_REMOVE); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
private function scanModules(PackageInterface $package, $scanType='install') |
177
|
|
|
{ |
178
|
|
|
$installer = $this->composer->getInstallationManager(); |
179
|
|
|
$packagePath = $installer->getInstallPath($package); |
180
|
|
|
$type = $package->getType(); |
181
|
|
|
$publicDir = $packagePath.'/public'; |
182
|
|
|
$extras = $package->getExtra(); |
183
|
|
|
|
184
|
|
|
if (file_exists($publicDir) && $type === static::YAWIK_MODULE_TYPE) { |
185
|
|
|
// we skip undefined zf module definition |
186
|
|
|
if (isset($extras['zf']['module'])) { |
187
|
|
|
// we register module class name |
188
|
|
|
$moduleName = $extras['zf']['module']; |
189
|
|
|
if (self::SCAN_TYPE_INSTALL == $scanType) { |
190
|
|
|
$this->installed[$moduleName] = realpath($publicDir); |
191
|
|
|
} else { |
192
|
|
|
$this->uninstalled[] = $moduleName; |
193
|
|
|
} |
194
|
|
|
} else { |
195
|
|
|
$this->output->write('[warning] No module definition for: ' . $package->getName()); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|