• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yawik / composer-plugin / 71

pending completion
71

push

travis-ci

TiSiE
Merge pull request #3: Compatibility with composer 2

* pr-3:
  chore: Fix unit tests
  chore: Upgrade to PHPUnit 8.5
  chore: force ravis to use composer version  2
  chore: drop support for php < 7.4
  chore: update dependencies
  feat: Implement new Plugin method deactivate and uninstall
  chore: require composer-plugin-api ^2.0, use laminas/dependency-plugin ^2.0

2 of 2 new or added lines in 1 file covered. (100.0%)

261 of 263 relevant lines covered (99.24%)

2.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.36
/src/Plugin.php
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 <me@itstoni.com>
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
    protected function configureEvents()
77
    {
78
        $eventManager = $this->getEventManager();
1✔
79
        $assets       = new AssetsInstaller();
1✔
80
        $fixer        = new PermissionsFixer();
1✔
81

82
        // activate events
83
        $eventManager->attach(self::YAWIK_PRE_CONFIGURE_EVENT, [ $assets, 'onPreConfigureEvent']);
1✔
84
        $eventManager->attach(self::YAWIK_PRE_CONFIGURE_EVENT, [ $fixer, 'onPreConfigureEvent']);
1✔
85

86
        // configure events
87
        $eventManager->attach(self::YAWIK_CONFIGURE_EVENT, [$assets,'onConfigureEvent']);
1✔
88
        $eventManager->attach(self::YAWIK_CONFIGURE_EVENT, [$fixer,'onConfigureEvent']);
1✔
89
    }
1✔
90

91
    public function activate(Composer $composer, IOInterface $io)
92
    {
93
        $this->composer   = $composer;
2✔
94
        $this->output     = $io;
2✔
95
    }
2✔
96

97
    public function deactivate(Composer $composer, IOInterface $io)
98
    {
99
        /* noop */
100
    }
×
101

102
    public function uninstall(Composer $composer, IOInterface $io)
103
    {
104
        /* noop */
105
    }
×
106

107
    /**
108
     * Provide composer event listeners.
109
     *
110
     * @return array
111
     */
112
    public static function getSubscribedEvents()
113
    {
114
        return [
115
            'post-autoload-dump'     => 'onPostAutoloadDump',
1✔
116
            'post-package-install'   => 'onPostPackageInstall',
117
            'post-package-update'    => 'onPostPackageUpdate',
118
            'pre-package-uninstall'  => 'onPrePackageUninstall'
119
        ];
120
    }
121

122
    /**
123
     * @return EventManager
124
     */
125
    public function getEventManager()
126
    {
127
        if (!is_object($this->eventManager)) {
2✔
128
            $this->eventManager = new EventManager();
1✔
129
        }
130
        return $this->eventManager;
2✔
131
    }
132

133
    /**
134
     * Get Yawik Application to use
135
     * @return Application|\Laminas\Mvc\Application
136
     */
137
    public function getApplication()
138
    {
139
        if (!is_object($this->application)) {
2✔
140
            $this->application = Application::init();
1✔
141
        }
142
        return $this->application;
2✔
143
    }
144

145
    public function getInstalledModules()
146
    {
147
        return $this->installed;
2✔
148
    }
149

150
    public function getUninstalledModules()
151
    {
152
        return $this->uninstalled;
1✔
153
    }
154

155
    public function onPostAutoloadDump()
156
    {
157
        // @codeCoverageIgnoreStart
158
        if (is_file($file = __DIR__.'/../../../autoload.php')) {
159
            require $file;
160
        }
161
        // @codeCoverageIgnoreEnd
162

163
        $this->configureEvents();
1✔
164
        $event            = new PreConfigureEvent($this->output);
1✔
165
        $this->getEventManager()->triggerEvent($event);
1✔
166

167
        $app              = $this->getApplication();
1✔
168
        $modules          = $app->getServiceManager()->get('ModuleManager')->getLoadedModules();
1✔
169
        $installed        = $this->installed;
1✔
170
        $coreOptions      = $app->getServiceManager()->get('Core/Options');
1✔
171

172
        foreach ($installed as $moduleName) {
1✔
173
            $className = $moduleName . '\\Module';
1✔
174
            if (class_exists($className, true)) {
1✔
175
                $modules[] = new $className;
1✔
176
            }
177
        }
178

179
        $event = new ConfigureEvent($coreOptions, $modules);
1✔
180
        $this->getEventManager()->triggerEvent($event);
1✔
181
    }
1✔
182

183
    public function onPostPackageInstall(PackageEvent $event)
184
    {
185
        $package = $event->getOperation()->getPackage();
1✔
186
        $this->addModules($package, static::ADD_TYPE_INSTALL);
1✔
187
    }
1✔
188

189
    public function onPostPackageUpdate(PackageEvent $event)
190
    {
191
        $package = $event->getOperation()->getTargetPackage();
1✔
192
        $this->addModules($package, static::ADD_TYPE_INSTALL);
1✔
193
    }
1✔
194

195
    public function onPrePackageUninstall(PackageEvent $event)
196
    {
197
        $package = $event->getOperation()->getPackage();
1✔
198
        $this->addModules($package, static::ADD_TYPE_REMOVE);
1✔
199
    }
1✔
200

201
    public function addModules(PackageInterface $package, $scanType='install')
202
    {
203
        $type           = $package->getType();
2✔
204
        $extras         = $package->getExtra();
2✔
205

206
        if ($type === static::YAWIK_MODULE_TYPE) {
2✔
207
            // we skip undefined zf module definition
208
            if (isset($extras['zf']['module'])) {
2✔
209
                // we register module class name
210
                $moduleName     = $extras['zf']['module'];
2✔
211
                if (self::ADD_TYPE_REMOVE == $scanType) {
2✔
212
                    $this->uninstalled[] = $moduleName;
1✔
213
                } else {
214
                    $this->installed[]   = $moduleName;
2✔
215
                }
216
            } else {
217
                $this->output->write('[warning] No module definition for: ' . $package->getName());
1✔
218
            }
219
        }
220
    }
2✔
221
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc