Envparser::setParse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Codervio\Envmanager;
4
5
use Codervio\Envmanager\Loader\Loader;
6
use Codervio\Envmanager\Resolver\KeyResolver;
7
use Codervio\Envmanager\Resolver\ValueResolver;
8
use Codervio\Envmanager\Parser\ParserCollector;
9
use Codervio\Envmanager\Resolver\VariableResolver;
10
use Codervio\Envmanager\Parser\Parser;
11
use Codervio\Envmanager\Parser\SystemParser;
12
use Codervio\Envmanager\Validator\VariableValidator;
13
use Codervio\Envmanager\Abstractparser\Envabstract;
14
use Exception;
15
16
/**
17
 * Class EnvParser
18
 *
19
 */
20
class Envparser extends Envabstract
21
{
22
    /**
23
     * @var array
24
     */
25
    public $lines = array();
26
    protected $context;
27
    protected $keepcomments;
28
    protected $valueresolver;
29
    protected $parsercollector;
30
    protected $variable_collector;
31
    protected $loader;
32
    protected $strictbool = false;
33
    protected $processEnv = true;
34
    protected $override = false;
35
36
    protected $systemparser = array();
37
    protected $keyresolver;
38
    protected $parser;
39
40
    private $result;
41
42
    public function __construct($path = null, $keepComments = false, $extension = array('env', 'main.env'))
43
    {
44
        new Prerequisities();
45
46
        $this->systemparser = new SystemParser();
47
48
        $this->loader = new Loader($path, $extension);
49
50
        $this->parser = new Parser();
51
        $this->keepcomments = $keepComments;
52
53
        $this->keyresolver = new KeyResolver($this);
54
        $this->valueresolver = new ValueResolver($this);
55
56
        $this->parsercollector = new ParserCollector();
57
58
        $this->variable_collector = new VariableResolver();
59
    }
60
61
    public function setParse(array $var, $processEnvironment = true)
62
    {
63
        $this->processEnv = $processEnvironment;
64
65
        $this->context = $var;
66
    }
67
68
    public function load($processEnvironment = true, $override = false)
69
    {
70
        $this->processEnv = $processEnvironment;
71
72
        $this->override = $override;
73
74
        if ($this->loader->getLoadStatus()) {
75
            $this->context = $this->parseEncoding($this->loader->run());
76
            return true;
77
        }
78
79
        return false;
80
    }
81
82
    public function setEncoding($encoding = 'UTF-8')
83
    {
84
        $this->fileEncoding = $encoding;
85
    }
86
87
    public function getEncoding()
88
    {
89
        return $this->fileEncoding;
90
    }
91
92
    public function setStrictBool($state = false)
93
    {
94
        $this->strictbool = $state;
95
    }
96
97
    public function required($variable)
98
    {
99
        $variableValidator = new VariableValidator($variable);
100
101
        $variableValidator->setStrict($this->getStrictBool());
102
        $variableValidator->setResult($this->result);
103
104
        return $variableValidator;
105
    }
106
107
    public function checkSuperGlobalsSet()
108
    {
109
        if (ini_get('variables_order') === '' || strpos(ini_get('variables_order'), 'G')) {
110
            return true;
111
        } else {
112
            throw new \RuntimeException('Warning: Set and create globals for $_ENV is disabled. To enable globally, for console run: \'php -d variables_order=EGPCS php.php\' or set in php.ini directive: variables_order=EGPCS');
113
        }
114
    }
115
116
    public function getValue($value)
117
    {
118
        if (empty($value)) {
119
            throw new Exception('No value set');
120
        }
121
122
        if (function_exists('apache_getenv')) {
123
            if (apache_getenv($value, false)) {
124
                return apache_getenv($value, false);
125
            }
126
        }
127
128
        if (isset($_ENV[$value])) {
129
            return $_ENV[$value];
130
        }
131
132
        if (function_exists('getenv')) {
133
            if (getenv($value, false)) {
134
                return getenv($value);
135
            }
136
        }
137
138
        return null;
139
    }
140
141
    public function getAllValues()
142
    {
143
        return $this->parsercollector->getArrayCopy();
144
    }
145
146
    public function checkSystemVar($variable)
147
    {
148
        return $this->systemparser->checkValue($variable);
149
    }
150
151
    public function getSystemVars($variable = null)
152
    {
153
        $systemparse = $this->systemparser;
154
        return $systemparse::getValues($variable);
155
    }
156
157
    public function run()
158
    {
159
        if (!$this->loader->getLoadStatus()) {
160
            return $this->processEnvironment($this->context);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->processEnvironment($this->context) targeting Codervio\Envmanager\Abst...t::processEnvironment() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
161
        }
162
163
        $result = $this->getParsed();
164
165
        $resultParsed = $result->getArrayCopy();
166
167
        if ($this->processEnv) {
168
            $this->processEnvironment($resultParsed);
169
        }
170
171
        $this->result = $resultParsed;
172
173
        return $resultParsed;
174
    }
175
176
    public function getComment($key)
177
    {
178
        if (isset($this->commentResolver[$key])) {
179
            return $this->commentResolver[$key];
180
        }
181
    }
182
}