Completed
Pull Request — master (#5)
by David
02:09
created

DiscoveryPlugin::activate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TheCodingMachine\Discovery;
6
7
use Composer\Composer;
8
use Composer\EventDispatcher\EventSubscriberInterface;
9
use Composer\Factory;
10
use Composer\IO\IOInterface;
11
use Composer\Package\PackageInterface;
12
use Composer\Plugin\Capability\CommandProvider;
13
use Composer\Plugin\Capable;
14
use Composer\Plugin\PluginInterface;
15
use Composer\Script\Event;
16
use Composer\Script\ScriptEvents;
17
use Symfony\Component\Filesystem\Filesystem;
18
use TheCodingMachine\Discovery\Commands\CommandProvider as DiscoveryCommandProvider;
19
use TheCodingMachine\Discovery\Commands\DumpCommand;
20
21
class DiscoveryPlugin implements PluginInterface, EventSubscriberInterface, Capable
22
{
23
    /**
24
     * @var Composer
25
     */
26
    protected $composer;
27
    protected $io;
28
29
    /**
30
     * Apply plugin modifications to Composer.
31
     *
32
     * @param Composer    $composer
33
     * @param IOInterface $io
34
     */
35
    public function activate(Composer $composer, IOInterface $io)
36
    {
37
        $this->composer = $composer;
38
        $this->io = $io;
39
    }
40
41
    public static function getSubscribedEvents()
42
    {
43
        return [
44
            ScriptEvents::PRE_AUTOLOAD_DUMP => 'beforeDumpAutoload',
45
        ];
46
    }
47
48
    public function beforeDumpAutoload(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        // Plugin has been uninstalled
51
        if (!file_exists(__FILE__)) {
52
            return;
53
        }
54
55
        $dumper = new Dumper($this->composer, $this->io);
56
        $dumper->dumpDiscoveryFiles();
57
58
        $this->registerClassInAutoloader();
59
    }
60
61
    /**
62
     * This registers the generated TheCodingMachine\Discovery class in the autoloader.
63
     */
64
    private function registerClassInAutoloader()
65
    {
66
        // Let's dynamically add a Discovery file to the autoloader
67
        $discoveryFile = '.discovery/Discovery.php';
68
        $autoload = $this->composer->getPackage()->getAutoload();
69
        $autoload['classmap'][] = $discoveryFile;
70
        $this->composer->getPackage()->setAutoload($autoload);
71
    }
72
73
    /**
74
     * Method by which a Plugin announces its API implementations, through an array
75
     * with a special structure.
76
     *
77
     * The key must be a string, representing a fully qualified class/interface name
78
     * which Composer Plugin API exposes.
79
     * The value must be a string as well, representing the fully qualified class name
80
     * of the implementing class.
81
     *
82
     * @tutorial
83
     *
84
     * return array(
85
     *     'Composer\Plugin\Capability\CommandProvider' => 'My\CommandProvider',
86
     *     'Composer\Plugin\Capability\Validator'       => 'My\Validator',
87
     * );
88
     *
89
     * @return string[]
90
     */
91
    public function getCapabilities()
92
    {
93
        return [
94
            CommandProvider::class => DiscoveryCommandProvider::class,
95
        ];
96
    }
97
}
98