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\EventDispatcher\EventSubscriberInterface; |
15
|
|
|
use Composer\Installer\PackageEvent; |
16
|
|
|
use Composer\IO\IOInterface; |
17
|
|
|
use Composer\Package\PackageInterface; |
18
|
|
|
use Composer\Plugin\PluginInterface; |
19
|
|
|
use Core\Application; |
20
|
|
|
use Yawik\Composer\Event\ActivateEvent; |
21
|
|
|
use Yawik\Composer\Event\ConfigureEvent; |
22
|
|
|
use Zend\EventManager\EventManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Plugin |
26
|
|
|
* @package Yawik\Composer |
27
|
|
|
* @author Anthonius Munthi <[email protected]> |
28
|
|
|
* @since 0.32.0 |
29
|
|
|
* @TODO: Create more documentation for methods |
30
|
|
|
*/ |
31
|
|
|
class Plugin implements PluginInterface, EventSubscriberInterface |
32
|
|
|
{ |
33
|
|
|
const YAWIK_ACTIVATE_EVENT = 'yawik.activate'; |
34
|
|
|
|
35
|
|
|
const YAWIK_CONFIGURE_EVENT = 'yawik.configure'; |
36
|
|
|
|
37
|
|
|
const YAWIK_MODULE_TYPE = 'yawik-module'; |
38
|
|
|
|
39
|
|
|
const ADD_TYPE_INSTALL = 'install'; |
40
|
|
|
|
41
|
|
|
const ADD_TYPE_REMOVE = 'remove'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Application |
45
|
|
|
*/ |
46
|
|
|
protected $application; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Composer |
50
|
|
|
*/ |
51
|
|
|
protected $composer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var IOInterface |
55
|
|
|
*/ |
56
|
|
|
protected $output; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* An array list of available modules |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
protected $installed = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* An array list of uninstalled packages |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
protected $uninstalled = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var EventManager |
73
|
|
|
*/ |
74
|
|
|
protected $eventManager; |
75
|
|
|
|
76
|
1 |
|
protected function configureEvents() |
77
|
|
|
{ |
78
|
1 |
|
$eventManager = $this->getEventManager(); |
79
|
1 |
|
$assets = new AssetsInstaller(); |
80
|
1 |
|
$fixer = new PermissionsFixer(); |
81
|
|
|
|
82
|
|
|
// activate events |
83
|
1 |
|
$eventManager->attach(self::YAWIK_ACTIVATE_EVENT, [$assets,'onActivateEvent']); |
84
|
1 |
|
$eventManager->attach(self::YAWIK_ACTIVATE_EVENT, [$fixer,'onActivateEvent']); |
85
|
|
|
|
86
|
|
|
// configure events |
87
|
1 |
|
$eventManager->attach(self::YAWIK_CONFIGURE_EVENT, [$assets,'onConfigureEvent']); |
88
|
1 |
|
$eventManager->attach(self::YAWIK_CONFIGURE_EVENT, [$fixer,'onConfigureEvent']); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
1 |
|
public function activate(Composer $composer, IOInterface $io) |
92
|
|
|
{ |
93
|
|
|
// @codeCoverageIgnoreStart |
94
|
|
|
if (is_file($file = __DIR__.'/../../../autoload.php')) { |
95
|
|
|
include $file; |
96
|
|
|
} |
97
|
|
|
// @codeCoverageIgnoreEnd |
98
|
|
|
|
99
|
1 |
|
$this->configureEvents(); |
100
|
|
|
|
101
|
1 |
|
$this->composer = $composer; |
102
|
1 |
|
$this->output = $io; |
103
|
1 |
|
$event = new ActivateEvent($composer, $io); |
104
|
|
|
|
105
|
1 |
|
$this->getEventManager()->triggerEvent($event); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Provide composer event listeners. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
1 |
|
public static function getSubscribedEvents() |
114
|
|
|
{ |
115
|
|
|
return [ |
116
|
1 |
|
'post-autoload-dump' => 'onPostAutoloadDump', |
117
|
|
|
'post-package-install' => 'onPostPackageInstall', |
118
|
|
|
'post-package-update' => 'onPostPackageUpdate', |
119
|
|
|
'pre-package-uninstall' => 'onPrePackageUninstall' |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return EventManager |
125
|
|
|
*/ |
126
|
3 |
|
public function getEventManager() |
127
|
|
|
{ |
128
|
3 |
|
if (!is_object($this->eventManager)) { |
129
|
1 |
|
$this->eventManager = new EventManager(); |
130
|
|
|
} |
131
|
3 |
|
return $this->eventManager; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get Yawik Application to use |
136
|
|
|
* @return Application|\Zend\Mvc\Application |
137
|
|
|
*/ |
138
|
2 |
|
public function getApplication() |
139
|
|
|
{ |
140
|
2 |
|
if (!is_object($this->application)) { |
141
|
1 |
|
$this->application = Application::init(); |
142
|
|
|
} |
143
|
2 |
|
return $this->application; |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
public function getInstalledModules() |
147
|
|
|
{ |
148
|
2 |
|
return $this->installed; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
public function getUninstalledModules() |
152
|
|
|
{ |
153
|
1 |
|
return $this->uninstalled; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
public function onPostAutoloadDump() |
157
|
|
|
{ |
158
|
1 |
|
$app = $this->getApplication(); |
159
|
1 |
|
$modules = $app->getServiceManager()->get('ModuleManager')->getLoadedModules(); |
160
|
1 |
|
$installed = $this->installed; |
161
|
1 |
|
$coreOptions = $app->getServiceManager()->get('Core/Options'); |
162
|
|
|
|
163
|
1 |
|
foreach ($installed as $moduleName) { |
164
|
1 |
|
$className = $moduleName . '\\Module'; |
165
|
1 |
|
if (class_exists($className, true)) { |
166
|
1 |
|
$modules[] = new $className; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
$event = new ConfigureEvent($coreOptions, $modules); |
171
|
1 |
|
$this->getEventManager()->triggerEvent($event); |
172
|
1 |
|
} |
173
|
|
|
|
174
|
1 |
|
public function onPostPackageInstall(PackageEvent $event) |
175
|
|
|
{ |
176
|
1 |
|
$package = $event->getOperation()->getPackage(); |
|
|
|
|
177
|
1 |
|
$this->addModules($package, static::ADD_TYPE_INSTALL); |
178
|
1 |
|
} |
179
|
|
|
|
180
|
1 |
|
public function onPostPackageUpdate(PackageEvent $event) |
181
|
|
|
{ |
182
|
1 |
|
$package = $event->getOperation()->getTargetPackage(); |
|
|
|
|
183
|
1 |
|
$this->addModules($package, static::ADD_TYPE_INSTALL); |
184
|
1 |
|
} |
185
|
|
|
|
186
|
1 |
|
public function onPrePackageUninstall(PackageEvent $event) |
187
|
|
|
{ |
188
|
1 |
|
$package = $event->getOperation()->getPackage(); |
189
|
1 |
|
$this->addModules($package, static::ADD_TYPE_REMOVE); |
190
|
1 |
|
} |
191
|
|
|
|
192
|
2 |
|
public function addModules(PackageInterface $package, $scanType='install') |
193
|
|
|
{ |
194
|
2 |
|
$type = $package->getType(); |
195
|
2 |
|
$extras = $package->getExtra(); |
196
|
|
|
|
197
|
2 |
|
if ($type === static::YAWIK_MODULE_TYPE) { |
198
|
|
|
// we skip undefined zf module definition |
199
|
2 |
|
if (isset($extras['zf']['module'])) { |
200
|
|
|
// we register module class name |
201
|
2 |
|
$moduleName = $extras['zf']['module']; |
202
|
2 |
|
if (self::ADD_TYPE_REMOVE == $scanType) { |
203
|
1 |
|
$this->uninstalled[] = $moduleName; |
204
|
|
|
} else { |
205
|
2 |
|
$this->installed[] = $moduleName; |
206
|
|
|
} |
207
|
|
|
} else { |
208
|
1 |
|
$this->output->write('[warning] No module definition for: ' . $package->getName()); |
209
|
|
|
} |
210
|
|
|
} |
211
|
2 |
|
} |
212
|
|
|
} |
213
|
|
|
|