-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMagentoDotenv.php
83 lines (68 loc) · 2.14 KB
/
MagentoDotenv.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
<?php
declare(strict_types=1);
namespace Zepgram\Component\MagentoDotenv;
use Symfony\Component\Dotenv\Dotenv as SymfonyDotenv;
use function copy;
use function is_file;
use function touch;
class MagentoDotenv
{
/** @var string */
public const MAGENTO_BOOTSTRAP_FILE = 'app/bootstrap.php';
/** @var string */
public const SRC_APP_ETC_DOTENV_FILE = 'vendor/zepgram/magento-dotenv/dist/app/dotenv.php';
/** @var string */
public const DEST_APP_ETC_DOTENV_FILE = 'app/dotenv.php';
/** @var string */
public const ENV_FILE = '.env';
/** @var string */
public const ENV_KEY = 'APP_ENV';
/** @var string */
public const DEBUG_KEY = 'APP_DEBUG';
/**
* Deploy dotenv files and load dotenv
*/
public static function init(): void
{
$dotenvDest = ROOT_DIRECTORY . self::DEST_APP_ETC_DOTENV_FILE;
if (is_file($dotenvDest)) {
require_once $dotenvDest;
return;
} else {
$dotenvSrc = ROOT_DIRECTORY . self::SRC_APP_ETC_DOTENV_FILE;
if (is_file($dotenvSrc)) {
copy($dotenvSrc, $dotenvDest);
}
}
$magentoBootstrapFile = ROOT_DIRECTORY . self::MAGENTO_BOOTSTRAP_FILE;
if (!is_file($magentoBootstrapFile)) {
return;
}
require_once $dotenvDest;
}
/**
* @param bool $usePutEnv
* @param string $envKey
* @param string $debugKey
* @param string $envFilePath
* @return SymfonyDotenv
*/
public static function get(
bool $usePutEnv = false,
string $envKey = self::ENV_KEY,
string $debugKey = self::DEBUG_KEY,
string $envFilePath = ''
): SymfonyDotenv {
if ($envFilePath !== '') {
$envFilePath = $envFilePath . DIRECTORY_SEPARATOR;
}
$envFile = ROOT_DIRECTORY . $envFilePath . self::ENV_FILE;
if (!is_file($envFile)) {
touch($envFile);
}
$dotenv = new SymfonyDotenv($envKey, $debugKey);
$dotenv->usePutEnv($usePutEnv);
$dotenv->loadEnv(ROOT_DIRECTORY . $envFilePath . self::ENV_FILE);
return $dotenv;
}
}