Issues (11)

src/Config/Traits/HasEnviromentsTrait.php (1 issue)

1
<?php
2
3
namespace ByTIC\Migrations\Config\Traits;
4
5
/**
6
 * Trait HasEnviromentsTrait
7
 * @package ByTIC\Migrations\Config\Traits
8
 */
9
trait HasEnviromentsTrait
10
{
11
12
    /**
13
     * @param $name
14
     * @param $config
15
     */
16 3
    public function addEnviromentFromEnv($name = 'local', $config = [])
17
    {
18
        $env = [
19 3
            'adapter' => 'DB_ADAPTER',
20
            'host'    => 'DB_HOST',
21
            'name'    => 'DB_DATABASE',
22
            'user'    => 'DB_USERNAME',
23
            'pass'    => 'DB_PASSWORD',
24
        ];
25 3
        foreach ($env as $param => $envName) {
26 3
            if (!empty($config[$param])) {
27
                continue;
28
            }
29 3
            if (empty($_ENV[$envName])) {
30 3
                continue;
31
            }
32 3
            $config[$param] = $_ENV[$envName];
33
        }
34 3
        $this->addEnviroment($name, $config);
35 3
    }
36
37
    /**
38
     * @param $name
39
     * @param $config
40
     */
41 3
    public function addEnviroment($name, $config)
42
    {
43
        $default = [
44 3
            'adapter'   => 'mysql',
45
            'host'      => 'localhost',
46
            'name'      => '',
47
            'user'      => '',
48
            'pass'      => '',
49
            'port'      => '3306',
50
            'charset'   => 'utf8',
51
            'collation' => 'utf8_unicode_ci',
52
        ];
53 3
        foreach ($default as $param => $value) {
54 3
            if (isset($config[$param])) {
55 3
                continue;
56
            }
57 3
            $config[$param] = $value;
58
        }
59 3
        $this->params['environments'][$name] = $config;
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
    }
61
}