Passed
Push — master ( 084553...482ec7 )
by Evgenii
01:21
created

Plugin::discover()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
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');
0 ignored issues
show
Bug introduced by
The function get_plugins was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $autoPlugins = /** @scrutinizer ignore-call */ get_plugins('/../mu-plugins');
Loading history...
59
        $muPlugins   = get_mu_plugins();
0 ignored issues
show
Bug introduced by
The function get_mu_plugins was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        $muPlugins   = /** @scrutinizer ignore-call */ get_mu_plugins();
Loading history...
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