Completed
Branch master (50a96e)
by ANTHONIUS
05:47
created

Plugin::configureEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
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();
0 ignored issues
show
Bug introduced by
The method getPackage() does not exist on Composer\DependencyResol...tion\OperationInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Composer\DependencyResol...eration\SolverOperation or Composer\DependencyResol...eration\UpdateOperation. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
        $package = $event->getOperation()->/** @scrutinizer ignore-call */ getPackage();
Loading history...
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();
0 ignored issues
show
Bug introduced by
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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
        $package = $event->getOperation()->/** @scrutinizer ignore-call */ getTargetPackage();
Loading history...
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