1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helick\Composer; |
4
|
|
|
|
5
|
|
|
use Composer\Composer; |
6
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
7
|
|
|
use Composer\IO\IOInterface; |
8
|
|
|
use Composer\Plugin\PluginInterface; |
9
|
|
|
|
10
|
|
|
final class Plugin implements PluginInterface, EventSubscriberInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @inheritDoc |
14
|
|
|
*/ |
15
|
|
|
public static function getSubscribedEvents() |
16
|
|
|
{ |
17
|
|
|
return [ |
18
|
|
|
'post-install-cmd' => ['install'], |
19
|
|
|
'post-update-cmd' => ['install'], |
20
|
|
|
'post-autoload-dump' => ['discover'], |
21
|
|
|
]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritDoc |
26
|
|
|
*/ |
27
|
|
|
public function activate(Composer $composer, IOInterface $io) |
28
|
|
|
{ |
29
|
|
|
// |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Kick-off the installation. |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function install(): void |
38
|
|
|
{ |
39
|
|
|
$sourceDir = dirname(__DIR__, 1) . '/resources/stubs'; |
40
|
|
|
$destDir = dirname(__DIR__, 4); |
41
|
|
|
|
42
|
|
|
$this->createDirectories($destDir); |
43
|
|
|
|
44
|
|
|
$this->copyFiles($sourceDir, $destDir); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Discover mu-plugins. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function discover(): void |
53
|
|
|
{ |
54
|
|
|
$rootDir = dirname(__DIR__, 4); |
55
|
|
|
|
56
|
|
|
require_once $rootDir . '/web/wordpress/wp-admin/includes/plugin.php'; |
57
|
|
|
|
58
|
|
|
$autoPlugins = get_plugins('/../mu-plugins'); |
|
|
|
|
59
|
|
|
$muPlugins = get_mu_plugins(); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$plugins = array_diff_key($autoPlugins, $muPlugins); |
62
|
|
|
$plugins = array_keys($plugins); |
63
|
|
|
|
64
|
|
|
$manifestPath = $rootDir . '/bootstrap/cache/mu-plugins.php'; |
65
|
|
|
$manifestData = '<?php return ' . var_export($plugins, true) . ';'; |
66
|
|
|
|
67
|
|
|
file_put_contents($manifestPath, $manifestData); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create necessary directories. |
72
|
|
|
* |
73
|
|
|
* @param string $destDir |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
private function createDirectories(string $destDir): void |
78
|
|
|
{ |
79
|
|
|
$directories = [ |
80
|
|
|
$destDir . '/bootstrap', |
81
|
|
|
$destDir . '/bootstrap/cache', |
82
|
|
|
$destDir . '/config', |
83
|
|
|
$destDir . '/config/environments', |
84
|
|
|
$destDir . '/web', |
85
|
|
|
$destDir . '/web/content', |
86
|
|
|
$destDir . '/web/content/mu-plugins', |
87
|
|
|
$destDir . '/web/content/plugins', |
88
|
|
|
$destDir . '/web/content/themes', |
89
|
|
|
$destDir . '/web/content/uploads', |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$directories = array_filter($directories, function ($directory) { |
93
|
|
|
return !is_dir($directory); |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
foreach ($directories as $directory) { |
97
|
|
|
mkdir($directory); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Copy necessary files. |
103
|
|
|
* |
104
|
|
|
* @param string $sourceDir |
105
|
|
|
* @param string $destDir |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
private function copyFiles(string $sourceDir, string $destDir): void |
110
|
|
|
{ |
111
|
|
|
$files = [ |
112
|
|
|
'/config/environments/development.php', |
113
|
|
|
'/config/environments/staging.php', |
114
|
|
|
'/config/application.php', |
115
|
|
|
'/web/index.php', |
116
|
|
|
'/web/wp-config.php', |
117
|
|
|
'/.env.example', |
118
|
|
|
'/wp-cli.yml', |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
$files = array_filter($files, function ($file) use ($destDir) { |
122
|
|
|
return !file_exists($destDir . $file); |
123
|
|
|
}); |
124
|
|
|
|
125
|
|
|
foreach ($files as $file) { |
126
|
|
|
copy($sourceDir . $file . '.stub', $destDir . $file); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|