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\PreConfigureEvent; |
||||
21 | use Yawik\Composer\Event\ConfigureEvent; |
||||
22 | use Laminas\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_PRE_CONFIGURE_EVENT = 'yawik.configure.pre'; |
||||
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_PRE_CONFIGURE_EVENT, [ $assets, 'onPreConfigureEvent']); |
|||
84 | 1 | $eventManager->attach(self::YAWIK_PRE_CONFIGURE_EVENT, [ $fixer, 'onPreConfigureEvent']); |
|||
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 | 2 | public function activate(Composer $composer, IOInterface $io) |
|||
92 | { |
||||
93 | 2 | $this->composer = $composer; |
|||
94 | 2 | $this->output = $io; |
|||
95 | 2 | } |
|||
96 | |||||
97 | public function deactivate(Composer $composer, IOInterface $io) |
||||
98 | { |
||||
99 | /* noop */ |
||||
100 | } |
||||
101 | |||||
102 | 1 | public function uninstall(Composer $composer, IOInterface $io) |
|||
103 | { |
||||
104 | /* noop */ |
||||
105 | 1 | } |
|||
106 | |||||
107 | /** |
||||
108 | * Provide composer event listeners. |
||||
109 | * |
||||
110 | * @return array |
||||
111 | */ |
||||
112 | public static function getSubscribedEvents() |
||||
113 | { |
||||
114 | return [ |
||||
115 | 2 | 'post-autoload-dump' => 'onPostAutoloadDump', |
|||
116 | 'post-package-install' => 'onPostPackageInstall', |
||||
117 | 2 | 'post-package-update' => 'onPostPackageUpdate', |
|||
118 | 1 | 'pre-package-uninstall' => 'onPrePackageUninstall' |
|||
119 | ]; |
||||
120 | 2 | } |
|||
121 | |||||
122 | /** |
||||
123 | * @return EventManager |
||||
124 | */ |
||||
125 | public function getEventManager() |
||||
126 | { |
||||
127 | 2 | if (!is_object($this->eventManager)) { |
|||
128 | $this->eventManager = new EventManager(); |
||||
129 | 2 | } |
|||
130 | 1 | return $this->eventManager; |
|||
131 | } |
||||
132 | 2 | ||||
133 | /** |
||||
134 | * Get Yawik Application to use |
||||
135 | 2 | * @return Application|\Laminas\Mvc\Application |
|||
136 | */ |
||||
137 | 2 | public function getApplication() |
|||
138 | { |
||||
139 | if (!is_object($this->application)) { |
||||
140 | 1 | $this->application = Application::init(); |
|||
141 | } |
||||
142 | 1 | return $this->application; |
|||
143 | } |
||||
144 | |||||
145 | 1 | public function getInstalledModules() |
|||
146 | { |
||||
147 | return $this->installed; |
||||
148 | } |
||||
149 | |||||
150 | public function getUninstalledModules() |
||||
151 | { |
||||
152 | return $this->uninstalled; |
||||
153 | 1 | } |
|||
154 | 1 | ||||
155 | 1 | public function onPostAutoloadDump() |
|||
156 | { |
||||
157 | 1 | // @codeCoverageIgnoreStart |
|||
158 | 1 | if (is_file($file = __DIR__.'/../../../autoload.php')) { |
|||
159 | 1 | require $file; |
|||
160 | 1 | } |
|||
161 | // @codeCoverageIgnoreEnd |
||||
162 | 1 | ||||
163 | 1 | $this->configureEvents(); |
|||
164 | 1 | $event = new PreConfigureEvent($this->output); |
|||
165 | 1 | $this->getEventManager()->triggerEvent($event); |
|||
166 | |||||
167 | $app = $this->getApplication(); |
||||
168 | $modules = $app->getServiceManager()->get('ModuleManager')->getLoadedModules(); |
||||
169 | 1 | $installed = $this->installed; |
|||
170 | 1 | $coreOptions = $app->getServiceManager()->get('Core/Options'); |
|||
171 | 1 | ||||
172 | foreach ($installed as $moduleName) { |
||||
173 | 1 | $className = $moduleName . '\\Module'; |
|||
174 | if (class_exists($className, true)) { |
||||
175 | 1 | $modules[] = new $className; |
|||
176 | 1 | } |
|||
177 | 1 | } |
|||
178 | |||||
179 | 1 | $event = new ConfigureEvent($coreOptions, $modules); |
|||
180 | $this->getEventManager()->triggerEvent($event); |
||||
181 | 1 | } |
|||
182 | 1 | ||||
183 | 1 | public function onPostPackageInstall(PackageEvent $event) |
|||
184 | { |
||||
185 | 1 | $package = $event->getOperation()->getPackage(); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
186 | $this->addModules($package, static::ADD_TYPE_INSTALL); |
||||
187 | 1 | } |
|||
188 | 1 | ||||
189 | 1 | public function onPostPackageUpdate(PackageEvent $event) |
|||
190 | { |
||||
191 | 2 | $package = $event->getOperation()->getTargetPackage(); |
|||
0 ignored issues
–
show
The method
getTargetPackage() does not exist on Composer\DependencyResol...tion\OperationInterface . It seems like you code against a sub-type of Composer\DependencyResol...tion\OperationInterface such as Composer\DependencyResol...eration\UpdateOperation or Composer\DependencyResol...eration\UpdateOperation .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
192 | $this->addModules($package, static::ADD_TYPE_INSTALL); |
||||
193 | 2 | } |
|||
194 | 2 | ||||
195 | public function onPrePackageUninstall(PackageEvent $event) |
||||
196 | 2 | { |
|||
197 | $package = $event->getOperation()->getPackage(); |
||||
198 | 2 | $this->addModules($package, static::ADD_TYPE_REMOVE); |
|||
199 | } |
||||
200 | 2 | ||||
201 | 2 | public function addModules(PackageInterface $package, $scanType='install') |
|||
202 | 1 | { |
|||
203 | $type = $package->getType(); |
||||
204 | 2 | $extras = $package->getExtra(); |
|||
205 | |||||
206 | if ($type === static::YAWIK_MODULE_TYPE) { |
||||
207 | 1 | // we skip undefined zf module definition |
|||
208 | if (isset($extras['zf']['module'])) { |
||||
209 | // we register module class name |
||||
210 | 2 | $moduleName = $extras['zf']['module']; |
|||
211 | if (self::ADD_TYPE_REMOVE == $scanType) { |
||||
212 | $this->uninstalled[] = $moduleName; |
||||
213 | } else { |
||||
214 | $this->installed[] = $moduleName; |
||||
215 | } |
||||
216 | } else { |
||||
217 | $this->output->write('[warning] No module definition for: ' . $package->getName()); |
||||
218 | } |
||||
219 | } |
||||
220 | } |
||||
221 | } |
||||
222 |