Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EnvProcessor.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types = 1);
7 
9 
16 {
22  private $envFile = '';
23 
29  private $envExampleFile = '';
30 
36  private $env = [];
37 
43  private $envExists;
44 
49  public function __construct(
50  string $envFile = ''
51  ) {
52  $this->envFile = $envFile;
53  $this->envExists = file_exists($envFile);
54  $this->envExampleFile = realpath(FW_BP . "/etc/config/.env.example");
55  }
56 
62  private function parseEnvFile(): array
63  {
64  $envExampleFile = file(
65  $this->envExampleFile,
66  FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
67  );
68 
69  $envContents = [];
70  if ($this->envExists) {
71  $envFile = file(
72  $this->envFile,
73  FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
74  );
75 
76  $envContents = $this->parseEnvFileLines($envFile);
77  }
78 
79  return array_merge($this->parseEnvFileLines($envExampleFile), $envContents);
80  }
81 
87  private function parseEnvFileLines(array $file): array
88  {
89  $fileArray = [];
90  foreach ($file as $line) {
91  // do not use commented out lines
92  if (strpos($line, '#') !== 0) {
93  list($key, $value) = explode('=', $line);
94  $fileArray[$key] = $value;
95  }
96  }
97  return $fileArray;
98  }
99 
106  public function putEnvFile(array $config = [])
107  {
108  $envData = '';
109  foreach ($config as $key => $value) {
110  $envData .= $key . '=' . $value . PHP_EOL;
111  }
112 
113  file_put_contents($this->envFile, $envData);
114  }
115 
121  public function getEnv(): array
122  {
123  if (empty($this->env)) {
124  $this->env = $this->parseEnvFile();
125  }
126  return $this->env;
127  }
128 }
$config
Definition: fraud_order.php:17
$value
Definition: gender.phtml:16