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

DiscoveryPlugin::getCapabilities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
20
class DiscoveryPlugin implements PluginInterface, EventSubscriberInterface, Capable
21
{
22
    /**
23
     * @var Composer
24
     */
25
    protected $composer;
26
    protected $io;
27
28
    /**
29
     * Apply plugin modifications to Composer.
30
     *
31
     * @param Composer    $composer
32
     * @param IOInterface $io
33
     */
34
    public function activate(Composer $composer, IOInterface $io)
35
    {
36
        $this->composer = $composer;
37
        $this->io = $io;
38
    }
39
40
    public static function getSubscribedEvents()
41
    {
42
        return [
43
            ScriptEvents::PRE_AUTOLOAD_DUMP => 'beforeDumpAutoload',
44
        ];
45
    }
46
47
    /**
48
     * @return AssetsBuilder
49
     */
50
    private function getAssetsBuilder() : AssetsBuilder
51
    {
52
        $installationManager = $this->composer->getInstallationManager();
53
        $rootDir = dirname(Factory::getComposerFile());
54
        return new AssetsBuilder($installationManager, $this->io, $rootDir);
55
    }
56
57
    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...
58
    {
59
        // Plugin has been uninstalled
60
        if (!file_exists(__FILE__)) {
61
            return;
62
        }
63
64
        $fileSystem = new Filesystem();
65
66
        $localRepos = $this->composer->getRepositoryManager()->getLocalRepository();
67
        $assetTypes = $this->getAssetsBuilder()->findAssetTypes($localRepos);
68
69
        // Let's get an array of values, indexed by asset type (to store in the discovery_values.php file)
70
        $values = array_map(function (AssetType $assetType) {
71
            return $assetType->getValues();
72
        }, $assetTypes);
73
74
        $fileSystem->dumpFile('.discovery/discovery_values.php', '<?php
75
return '.var_export($values, true).";\n");
76
77
        // Let's get an array of assetTypes, indexed by asset type (to store in the discovery_asset_types.php file)
78
        $assetTypes = array_map(function (AssetType $assetType) {
79
            return $assetType->jsonSerialize();
80
        }, $assetTypes);
81
82
        $fileSystem->dumpFile('.discovery/discovery_asset_types.php', '<?php
83
return '.var_export($assetTypes, true).";\n");
84
85
        // Let's copy the Discovery class in the .discovery directory. This is needed because otherwise, we have no way to "find" the .discovery directory easily.
86
        $fileSystem->dumpFile('.discovery/Discovery.php', file_get_contents(__DIR__.'/Discovery.php.tpl'));
87
88
        $this->registerClassInAutoloader();
89
    }
90
91
    /**
92
     * This registers the generated TheCodingMachine\Discovery class in the autoloader.
93
     */
94
    private function registerClassInAutoloader()
95
    {
96
        // Let's dynamically add a Discovery file to the autoloader
97
        $discoveryFile = '.discovery/Discovery.php';
98
        $autoload = $this->composer->getPackage()->getAutoload();
99
        $autoload['classmap'][] = $discoveryFile;
100
        $this->composer->getPackage()->setAutoload($autoload);
101
    }
102
103
    /**
104
     * Method by which a Plugin announces its API implementations, through an array
105
     * with a special structure.
106
     *
107
     * The key must be a string, representing a fully qualified class/interface name
108
     * which Composer Plugin API exposes.
109
     * The value must be a string as well, representing the fully qualified class name
110
     * of the implementing class.
111
     *
112
     * @tutorial
113
     *
114
     * return array(
115
     *     'Composer\Plugin\Capability\CommandProvider' => 'My\CommandProvider',
116
     *     'Composer\Plugin\Capability\Validator'       => 'My\Validator',
117
     * );
118
     *
119
     * @return string[]
120
     */
121
    public function getCapabilities()
122
    {
123
        return [
124
            CommandProvider::class => DiscoveryCommandProvider::class,
125
        ];
126
    }
127
}
128