Passed
Push — trunk ( 2eab1a...f65a85 )
by Christian
13:15 queued 15s
created

EnvConfigWriter::toEnv()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Installer\Configuration;
4
5
use Defuse\Crypto\Key;
6
use Shopware\Core\Installer\Controller\ShopConfigurationController;
7
use Shopware\Core\Installer\Finish\UniqueIdGenerator;
8
use Shopware\Core\Maintenance\System\Struct\DatabaseConnectionInformation;
9
10
/**
11
 * @internal
12
 *
13
 * @phpstan-import-type Shop from ShopConfigurationController
14
 */
15
class EnvConfigWriter
16
{
17
    private string $projectDir;
18
19
    private UniqueIdGenerator $idGenerator;
20
21
    public function __construct(string $projectDir, UniqueIdGenerator $idGenerator)
22
    {
23
        $this->projectDir = $projectDir;
24
        $this->idGenerator = $idGenerator;
25
    }
26
27
    /**
28
     * @param Shop $shop
29
     */
30
    public function writeConfig(DatabaseConnectionInformation $info, array $shop): void
31
    {
32
        $key = Key::createNewRandomKey();
33
        $secret = $key->saveToAsciiSafeString();
34
35
        $newEnv = [];
36
37
        $newEnv[] = '###> symfony/lock ###';
38
        $newEnv[] = '# Choose one of the stores below';
39
        $newEnv[] = '# postgresql+advisory://db_user:db_password@localhost/db_name';
40
        $newEnv[] = 'LOCK_DSN=flock';
41
        $newEnv[] = '###< symfony/lock ###';
42
        $newEnv[] = '';
43
44
        $newEnv[] = '###> symfony/messenger ###';
45
        $newEnv[] = '# Choose one of the transports below';
46
        $newEnv[] = '# MESSENGER_TRANSPORT_DSN=doctrine://default';
47
        $newEnv[] = '# MESSENGER_TRANSPORT_DSN=amqp://guest:guest@localhost:5672/%2f/messages';
48
        $newEnv[] = '# MESSENGER_TRANSPORT_DSN=redis://localhost:6379/messages';
49
        $newEnv[] = '###< symfony/messenger ###';
50
        $newEnv[] = '';
51
52
        $newEnv[] = '###> symfony/mailer ###';
53
        $newEnv[] = 'MAILER_DSN=null://null';
54
        $newEnv[] = '###< symfony/mailer ###';
55
        $newEnv[] = '';
56
57
        $newEnv[] = '###> shopware/core ###';
58
        $newEnv[] = 'APP_ENV=prod';
59
        $newEnv[] = 'APP_SECRET=' . $secret;
60
        $newEnv[] = 'APP_URL=' . $shop['schema'] . '://' . $shop['host'] . $shop['basePath'];
61
        $newEnv[] = 'DATABASE_URL=' . $info->asDsn();
62
        $newEnv[] = 'DATABASE_SSL_CA=' . $info->getSslCaPath();
63
        $newEnv[] = 'DATABASE_SSL_CERT=' . $info->getSslCertPath();
64
        $newEnv[] = 'DATABASE_SSL_KEY=' . $info->getSslCertKeyPath();
65
        $newEnv[] = 'DATABASE_SSL_DONT_VERIFY_SERVER_CERT=' . ($info->getSslDontVerifyServerCert() ? '1' : '');
66
        $newEnv[] = 'COMPOSER_HOME=' . $this->projectDir . '/var/cache/composer';
67
        $newEnv[] = 'INSTANCE_ID=' . $this->idGenerator->getUniqueId();
68
        $newEnv[] = 'BLUE_GREEN_DEPLOYMENT=' . (int) $shop['blueGreenDeployment'];
69
        $newEnv[] = '###< shopware/core ###';
70
        $newEnv[] = '';
71
72
        $newEnv[] = '###> shopware/elasticsearch ###';
73
74
        if (file_exists($this->projectDir . '/symfony.lock')) {
75
            $newEnv[] = 'OPENSEARCH_URL=http://localhost:9200';
76
        } else {
77
            $newEnv[] = 'SHOPWARE_ES_HOSTS=http://localhost:9200';
78
        }
79
80
        $newEnv[] = 'SHOPWARE_ES_ENABLED=0';
81
        $newEnv[] = 'SHOPWARE_ES_INDEXING_ENABLED=0';
82
        $newEnv[] = 'SHOPWARE_ES_INDEX_PREFIX=sw';
83
        $newEnv[] = 'SHOPWARE_ES_THROW_EXCEPTION=1';
84
        $newEnv[] = '###< shopware/elasticsearch ###';
85
        $newEnv[] = '';
86
87
        $newEnv[] = '###> shopware/storefront ###';
88
        $newEnv[] = 'SHOPWARE_HTTP_CACHE_ENABLED=1';
89
        $newEnv[] = 'SHOPWARE_HTTP_DEFAULT_TTL=7200';
90
        $newEnv[] = '###< shopware/storefront ###';
91
        $newEnv[] = '';
92
93
        file_put_contents($this->projectDir . '/.env', implode("\n", $newEnv));
94
95
        $htaccessPath = $this->projectDir . '/public/.htaccess';
96
97
        if (file_exists($htaccessPath . '.dist') && !file_exists($htaccessPath)) {
98
            $perms = fileperms($htaccessPath . '.dist');
99
            copy($htaccessPath . '.dist', $htaccessPath);
100
101
            if ($perms) {
102
                chmod($htaccessPath, $perms | 0644);
103
            }
104
        }
105
    }
106
}
107