Completed
Branch master (7a7471)
by ANTHONIUS
05:27
created

Plugin::scanModules()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 4
nop 2
dl 0
loc 20
rs 9.4888
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 30
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\EventDispatcher;
18
use Composer\EventDispatcher\EventSubscriberInterface;
19
use Composer\Installer\PackageEvent;
20
use Composer\IO\IOInterface;
21
use Composer\Package\PackageInterface;
22
use Composer\Plugin\PluginInterface;
23
use Composer\Script\Event as ScriptEvent;
24
use Core\Application;
25
use Symfony\Component\Console\Input\ArgvInput;
26
use Symfony\Component\Console\Output\ConsoleOutput;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Console\Style\SymfonyStyle;
29
use Yawik\Composer\Event\ActivateEvent;
30
use Yawik\Composer\Event\ConfigureEvent;
31
32
/**
33
 * Class Plugin
34
 * @package Yawik\Composer
35
 * @author  Anthonius Munthi <[email protected]>
36
 * @since   0.32.0
37
 * @TODO    Create more documentation for methods
38
 */
39
class Plugin implements PluginInterface, EventSubscriberInterface
40
{
41
    const YAWIK_ACTIVATE_EVENT  = 'yawik.activate';
42
43
    const YAWIK_CONFIGURE_EVENT = 'yawik.configure';
44
45
    const YAWIK_MODULE_TYPE     = 'yawik-module';
46
47
    const ADD_TYPE_INSTALL      = 'install';
48
49
    const ADD_TYPE_REMOVE       = 'remove';
50
51
    /**
52
     * @var EventDispatcher
53
     */
54
    protected $dispatcher;
55
56
    /**
57
     * @var Application
58
     */
59
    protected $application;
60
61
    /**
62
     * @var Composer
63
     */
64
    protected $composer;
65
66
    /**
67 5
     * @var IOInterface
68
     */
69 5
    protected $output;
70 5
71
    /**
72 4
     * An array list of available modules
73
     *
74 4
     * @var array
75 4
     */
76 4
    protected $installed = [];
77
78
    protected $uninstalled = [];
79
80
    /**
81
     * A lists of available installed yawik modules type
82
     * @var array
83 5
     */
84
    protected $yawikModules;
85 5
86 5
    public function activate(Composer $composer, IOInterface $io)
87
    {
88
        $dispatcher       = $composer->getEventDispatcher();
89
        $assets           = new AssetsInstaller();
90
        $fixer            = new PermissionsFixer();
91
92
93 1
        $event = new ActivateEvent($composer, $io);
94
        $dispatcher->addSubscriber($assets);
95
        $dispatcher->addSubscriber($fixer);
96 1
97
        $dispatcher->dispatch(static::YAWIK_ACTIVATE_EVENT, $event);
98
        $this->composer   = $composer;
99
        $this->output     = $io;
100
    }
101
102
    /**
103 4
     * Provide composer event listeners.
104
     *
105 4
     * @return array
106 2
     */
107
    public static function getSubscribedEvents()
108 4
    {
109 1
        return [
110
            'post-autoload-dump'     => 'onPostAutoloadDump',
111
            'post-package-install'   => 'onPostPackageInstall',
112 4
            'post-package-update'    => 'onPostPackageUpdate',
113 4
            'pre-package-uninstall'  => 'onPrePackageUninstall'
114
        ];
115 2
    }
116
117 2
    /**
118 2
     * Get Yawik Application to use
119 2
     * @return Application|\Zend\Mvc\Application
120
     */
121 1
    public function getApplication()
122
    {
123 1
        // @codeCoverageIgnoreStart
124 1
        if (is_file(__DIR__.'/../../vendor/autoload')) {
125 1
            include __DIR__.'/../../vendor/autoload';
126
        }
127 1
        // @codeCoverageIgnoreEnd
128
129 1
        if (!$this->application instanceof Application) {
0 ignored issues
show
introduced by
$this->application is always a sub-type of Core\Application.
Loading history...
130 1
            $this->application = Application::init();
131 1
        }
132
        return $this->application;
133
    }
134
135
    public function getInstalledModules()
136 5
    {
137
        return $this->installed;
138 5
    }
139 1
140 1
    public function getUninstalledModules()
141 1
    {
142 1
        return $this->uninstalled;
143
    }
144 1
145
    public function onPostAutoloadDump()
146 5
    {
147
        $app              = $this->getApplication();
148
        $modules          = $app->getServiceManager()->get('ModuleManager')->getLoadedModules();
149 4
        $installed        = $this->installed;
150
        $coreOptions      = $app->getServiceManager()->get('Core/Options');
151 4
152 4
        foreach ($installed as $moduleName) {
153 4
            $className = $moduleName . '\\Module';
154 4
            if (class_exists($className, true)) {
155 4
                $modules[] = new $className;
156
            }
157 4
        }
158
159 3
        $event = new ConfigureEvent($coreOptions, $modules);
160
        $dispatcher = $this->composer->getEventDispatcher();
161 3
        $dispatcher->dispatch(self::YAWIK_CONFIGURE_EVENT, $event);
162 3
    }
163 2
164
    public function onPostPackageInstall(PackageEvent $event)
165 3
    {
166
        $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

166
        $package = $event->getOperation()->/** @scrutinizer ignore-call */ getPackage();
Loading history...
167
        $this->addModules($package, static::ADD_TYPE_INSTALL);
168
    }
169
170
    public function onPostPackageUpdate(PackageEvent $event)
171 4
    {
172
        $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

172
        $package = $event->getOperation()->/** @scrutinizer ignore-call */ getTargetPackage();
Loading history...
173
        $this->addModules($package, static::ADD_TYPE_INSTALL);
174
    }
175
176
    public function onPrePackageUninstall(PackageEvent $event)
177
    {
178
        $package = $event->getOperation()->getPackage();
179
        $this->addModules($package, static::ADD_TYPE_REMOVE);
180
    }
181
182
    public function addModules(PackageInterface $package, $scanType='install')
183
    {
184
        $type           = $package->getType();
185
        $extras         = $package->getExtra();
186
187
        if ($type === static::YAWIK_MODULE_TYPE) {
188
            // we skip undefined zf module definition
189
            if (isset($extras['zf']['module'])) {
190
                // we register module class name
191
                $moduleName     = $extras['zf']['module'];
192
                if (self::ADD_TYPE_REMOVE == $scanType) {
193
                    $this->uninstalled[] = $moduleName;
194
                } else {
195
                    $this->installed[]   = $moduleName;
196
                }
197
            } else {
198
                $this->output->write('[warning] No module definition for: ' . $package->getName());
199
            }
200
        }
201
    }
202
}
203