-
Notifications
You must be signed in to change notification settings - Fork 2
/
Plugin.php
108 lines (94 loc) · 2.2 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Light\Composer;
use Composer\Composer;
use Composer\Config;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Package\Package;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Composer\Util\Filesystem;
use Symfony\Component\Finder\Finder;
/**
* The composer ignore plugin.
*
* @author lichunqaing<[email protected]>
* @version 1.0.0
* @license MIT
*/
class Plugin implements PluginInterface, EventSubscriberInterface
{
/**
* @var Composer
*/
protected $composer;
/**
* @var Config
*/
protected $config;
/**
* @var Filesystem
*/
protected $fileSystem;
/**
* @inheritdoc
*/
public function activate(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->config = $composer->getConfig();
$this->fileSystem = new Filesystem();
}
/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
ScriptEvents::POST_AUTOLOAD_DUMP => 'onPostAutoloadDump',
];
}
/**
* @param Event $event
*/
public function onPostAutoloadDump(Event $event)
{
/** @var Package $package */
$package = $this->composer->getPackage();
$extra = $package->getExtra();
$ignoreList = isset($extra['light-ignore-plugin']) ? $extra['light-ignore-plugin'] : null;
if (!$ignoreList) {
return;
}
$vendor_dir = $this->config->get('vendor-dir');
foreach ($ignoreList as $vendor => $files) {
$root = $this->fileSystem->normalizePath("{$vendor_dir}/{$vendor}");
$this->ignorePath($root, $files);
}
}
/**
* @param string $root
* @param array $files
*/
protected function ignorePath($root, array $files)
{
foreach ($files as $file) {
$_file = $this->fileSystem->normalizePath("{$root}/{$file}");
if (is_dir($_file)) {
$this->fileSystem->removeDirectory($_file);
} else {
$finder = Finder::create()->in($root)->ignoreVCS(false)->ignoreDotFiles(false)->name($file)->files();
foreach ($finder as $item) {
$this->fileSystem->remove($item->getRealPath());
}
}
}
}
public function deactivate(Composer $composer, IOInterface $io)
{
}
public function uninstall(Composer $composer, IOInterface $io)
{
}
}