Passed
Push — 6.5.0.0 ( 85bd4e...fe6596 )
by Christian
24:46 queued 09:10
created

FlexMigrator::cleanup()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 20
rs 9.6111
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Services;
5
6
use Shopware\Core\Framework\Log\Package;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
/**
10
 * @internal
11
 */
12
#[Package('core')]
13
class FlexMigrator
14
{
15
    private const REMOVE_FILES = [
16
        '.dockerignore',
17
        'Dockerfile',
18
        'PLATFORM_COMMIT_SHA',
19
        'README.md',
20
        'config/services.xml',
21
        'config/services_test.xml',
22
        'easy-coding-standard.php',
23
        'license.txt',
24
        'phpstan.neon',
25
        'phpunit.xml.dist',
26
        'psalm.xml',
27
        'public/index.php',
28
        'src/TestBootstrap.php',
29
        'var/plugins.json',
30
    ];
31
32
    private const REMOVE_DIRECTORIES = [
33
        '.github',
34
        '.gitlab-ci',
35
        'gitlab-ci.yml',
36
        'bin',
37
        'config/etc',
38
        'config/services',
39
        'public/recovery',
40
        'files/update',
41
    ];
42
43
    private const ENV_DEFAULT = <<<EOT
44
###> symfony/lock ###
45
# Choose one of the stores below
46
# postgresql+advisory://db_user:db_password@localhost/db_name
47
LOCK_DSN=flock
48
###< symfony/lock ###
49
###> symfony/messenger ###
50
# Choose one of the transports below
51
# MESSENGER_TRANSPORT_DSN=doctrine://default
52
# MESSENGER_TRANSPORT_DSN=amqp://guest:guest@localhost:5672/%2f/messages
53
# MESSENGER_TRANSPORT_DSN=redis://localhost:6379/messages
54
###< symfony/messenger ###
55
###> symfony/mailer ###
56
# MAILER_DSN=null://null
57
###< symfony/mailer ###
58
###> shopware/core ###
59
APP_ENV=prod
60
APP_URL=http://127.0.0.1:8000
61
APP_SECRET=7628a40c75b25f8a1f14b3812c3b250b
62
INSTANCE_ID=41322ddd2b70bd29ef65d402f025c785
63
BLUE_GREEN_DEPLOYMENT=0
64
DATABASE_URL=mysql://root:root@localhost/shopware
65
# With Shopware 6.4.17.0 the MAILER_DSN variable will be used in this template instead of MAILER_URL
66
MAILER_URL=null://null
67
###< shopware/core ###
68
###> shopware/elasticsearch ###
69
OPENSEARCH_URL=http://localhost:9200
70
SHOPWARE_ES_ENABLED=0
71
SHOPWARE_ES_INDEXING_ENABLED=0
72
SHOPWARE_ES_INDEX_PREFIX=sw
73
SHOPWARE_ES_THROW_EXCEPTION=1
74
###< shopware/elasticsearch ###
75
###> shopware/storefront ###
76
STOREFRONT_PROXY_URL=http://localhost
77
SHOPWARE_HTTP_CACHE_ENABLED=1
78
SHOPWARE_HTTP_DEFAULT_TTL=7200
79
###< shopware/storefront ###
80
EOT;
81
82
    /**
83
     * Delete all files and directories that are not needed anymore
84
     */
85
    public function cleanup(string $projectDir): void
86
    {
87
        $fs = new Filesystem();
88
89
        foreach (self::REMOVE_FILES as $file) {
90
            $path = $projectDir . '/' . $file;
91
            if ($fs->exists($path)) {
92
                $fs->remove($path);
93
            }
94
        }
95
96
        foreach (self::REMOVE_DIRECTORIES as $directory) {
97
            $path = $projectDir . '/' . $directory;
98
99
            if ($fs->exists($path)) {
100
                $fs->remove($path);
101
            }
102
        }
103
104
        $fs->mkdir($projectDir . '/bin');
105
    }
106
107
    public function patchRootComposerJson(string $projectDir): void
108
    {
109
        $composerJsonPath = $projectDir . '/composer.json';
110
111
        /** @var array{require: array<string, string>, config?: array{platform?: string, "allow-plugins": array<string, bool>}} $composerJson */
112
        $composerJson = json_decode((string) file_get_contents($composerJsonPath), true, \JSON_THROW_ON_ERROR);
113
114
        $composerJson['require']['symfony/flex'] = '^2';
115
        $composerJson['require']['symfony/runtime'] = '^5.0|^6.0';
116
117
        // Remove old recovery
118
        unset($composerJson['require']['shopware/recovery']);
119
120
        if (!isset($composerJson['config'])) {
121
            $composerJson['config'] = [];
122
        }
123
124
        if (!isset($composerJson['config']['allow-plugins'])) {
125
            $composerJson['config']['allow-plugins'] = [];
126
        }
127
128
        $composerJson['config']['allow-plugins']['symfony/flex'] = true;
129
        $composerJson['config']['allow-plugins']['symfony/runtime'] = true;
130
131
        unset($composerJson['config']['platform']);
132
133
        $composerJson['scripts'] = [
134
            'auto-scripts' => [
135
                'assets:install' => 'symfony-cmd',
136
            ],
137
            'post-install-cmd' => [
138
                '@auto-scripts',
139
            ],
140
            'post-update-cmd' => [
141
                '@auto-scripts',
142
            ],
143
        ];
144
145
        $composerJson['extra']['symfony'] = [
146
            'allow-contrib' => true,
147
            'endpoint' => [
148
                'https://raw.githubusercontent.com/shopware/recipes/flex/main/index.json',
149
                'flex://defaults',
150
            ],
151
        ];
152
153
        $composerJson['require-dev'] = [
154
            'fakerphp/faker' => '^1.20',
155
            'maltyxx/images-generator' => '^1.0',
156
            'mbezhanov/faker-provider-collection' => '^2.0',
157
            'symfony/stopwatch' => '^5.0|^6.0',
158
            'symfony/web-profiler-bundle' => '^5.0|^6.0',
159
        ];
160
161
        file_put_contents($composerJsonPath, json_encode($composerJson, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
162
    }
163
164
    public function copyNewTemplateFiles(string $projectDir): void
165
    {
166
        $fs = new Filesystem();
167
168
        $fs->mirror(__DIR__ . '/../Resources/flex-config/', $projectDir);
169
    }
170
171
    public function migrateEnvFile(string $projectDir): void
172
    {
173
        $envPath = $projectDir . '/.env';
174
175
        if (!file_exists($envPath)) {
176
            file_put_contents($envPath, self::ENV_DEFAULT);
177
178
            return;
179
        }
180
181
        rename($envPath, $envPath . '.local');
182
        file_put_contents($envPath, self::ENV_DEFAULT);
183
    }
184
}
185