Completed
Push — master ( 9d78f0...b09679 )
by Florin
14:38 queued 44s
created

Environment::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Makehappen\AutoMinifier;
4
5
/**
6
 * Class Environment
7
 * @package Makehappen\AutoMinifier
8
 */
9
class Environment
10
{
11
    /**
12
     * Environment file
13
     */
14
    const ENV_FILE = 'env.json';
15
16
    /**
17
     * Configuration file
18
     */
19
    const CONFIG_FILE = 'config.json';
20
21
    /**
22
     * Signature file for cache bust
23
     */
24
    const SIGNATURE_FILE = 'signature.txt';
25
26
    /**
27
     * Is Development
28
     * @var
29
     */
30
    protected $blnIsDev;
31
32
    /**
33
     * Environment Settings
34
     * @var
35
     */
36
    protected $objEnvironmentSettings;
37
38
    /**
39
     * Environment constructor.
40
     */
41
    public function __construct()
42
    {
43
    }
44
45
    /**
46
     * Set env to Dev
47
     *
48
     * @param bool $bln
49
     * @return $this
50
     */
51
    public function setDev($bln = true)
52
    {
53
        // set to provided value
54
        $this->blnIsDev = $bln;
55
        return $this;
56
    }
57
58
    /**
59
     * Determine if we are in development environment
60
     *
61
     * @return bool
62
     */
63
    public function isDevEnv()
64
    {
65
        // if set to dev stop here
66
        if ($this->blnIsDev) {
67
            return true;
68
        }
69
70
        if ($this->hasDevEnv()) {
71
            return true;
72
        }
73
74
        return false;
75
    }
76
77
    /**
78
     * Determine if it has develompent env set
79
     *
80
     * @return bool
81
     */
82
    public function hasDevEnv()
83
    {
84
        // if not set abort
85
        if (empty($this->objEnvironmentSettings->environment)) {
86
            return false;
87
        }
88
89
        // determine if it's development
90
        return 'development' == $this->objEnvironmentSettings->environment;
91
    }
92
93
    /**
94
     * Create a settings file
95
     *
96
     * @param $strFilePath
97
     * @param $arrSettings
98
     * @return object
99
     */
100
    public function createSettingsFile($strFilePath, $arrSettings)
101
    {
102
        // create file
103
        file_put_contents($strFilePath, json_encode($arrSettings, JSON_PRETTY_PRINT));
104
105
        return (object) $arrSettings;
106
    }
107
108
    /**
109
     * Get settings
110
     *
111
     * @param null $strFile
112
     * @param array $arrSettings
113
     * @return mixed|object
114
     */
115
    public function getSettings($strFile = null, $arrSettings = [])
116
    {
117
        // crete and retur if does not exists
118
        if (!file_exists($strFile)) {
119
            return $this->createSettingsFile($strFile, $arrSettings);
120
        }
121
122
        // return contents
123
        return json_decode(file_get_contents($strFile));
124
    }
125
126
    /**
127
     * Get Config
128
     *
129
     * @param $strFilesFolder
130
     * @return mixed|object
131
     */
132
    public function getConfig($strFilesFolder)
133
    {
134
        // default config
135
        $arrDefaultConfig = [
136
            'files' => []
137
        ];
138
139
        // return config
140
        return $this->getSettings($strFilesFolder . '/' . Environment::CONFIG_FILE, $arrDefaultConfig);
141
    }
142
143
    /**
144
     * Set environment
145
     *
146
     * @param null $strFilesFolder
147
     */
148
    public function setEnvironment($strFilesFolder = null)
149
    {
150
        // default environment
151
        $arrDefaultEnv = [
152
            'environment' => 'production'
153
        ];
154
155
        // get env file settings
156
        $this->objEnvironmentSettings = $this->getSettings($strFilesFolder  . '/' . self::ENV_FILE, $arrDefaultEnv);
157
    }
158
}
159