Codervio /
Envmanager
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Codervio\Envmanager\Abstractparser; |
||
| 4 | |||
| 5 | use Codervio\Envmanager\Parser\ParserCollector; |
||
| 6 | |||
| 7 | abstract class Envabstract |
||
| 8 | { |
||
| 9 | protected $fileEncoding; |
||
| 10 | protected $parsedsystemvars = array(); |
||
| 11 | protected $commentResolver = array(); |
||
| 12 | |||
| 13 | protected function parseEncoding($data) |
||
| 14 | { |
||
| 15 | if (empty($this->fileEncoding)) { |
||
| 16 | $this->fileEncoding = mb_detect_encoding($data, "auto"); |
||
| 17 | } |
||
| 18 | |||
| 19 | if (!in_array($this->fileEncoding, mb_list_encodings())) { |
||
| 20 | throw new \Exception(sprintf('Encoding "%s" not found. Check supported encoding using mb_list_encoding() function or use mostly UTF-8 encoding type', $this->fileEncoding)); |
||
| 21 | } |
||
| 22 | |||
| 23 | return mb_convert_encoding($data, $this->fileEncoding, 'auto'); |
||
| 24 | } |
||
| 25 | |||
| 26 | protected function explodeKeyVal($value) |
||
| 27 | { |
||
| 28 | $keyPair = strtok($value, '='); |
||
| 29 | |||
| 30 | preg_match("/^([^=]*)=(.*)/i", $value, $matches, 0); |
||
| 31 | |||
| 32 | if (!isset($matches[2])) { |
||
| 33 | return array($keyPair, null); |
||
| 34 | } |
||
| 35 | |||
| 36 | return array($keyPair, $matches[2]); |
||
| 37 | } |
||
| 38 | |||
| 39 | protected function processEnvironment($values) |
||
| 40 | { |
||
| 41 | if (!is_array($values)) { |
||
| 42 | return null; |
||
| 43 | } |
||
| 44 | |||
| 45 | foreach ($values as $envkey => $envvalue) { |
||
| 46 | // HTTP_ .... |
||
| 47 | |||
| 48 | if (!$this->override) { |
||
| 49 | |||
| 50 | if (function_exists('getenv')) { |
||
| 51 | if (!getenv($envkey, false)) { |
||
| 52 | putenv("$envkey=$envvalue"); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | if (!isset($_ENV[$envkey])) { |
||
| 57 | $_ENV[$envkey] = $envvalue; |
||
| 58 | } |
||
| 59 | |||
| 60 | if (function_exists('apache_getenv')) { |
||
| 61 | if (!apache_getenv($envkey, false)) { |
||
| 62 | /** @scrutinizer ignore-unhandled */ @apache_setenv($envkey, $envvalue, false); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | if (!isset($_SERVER[$envkey])) { |
||
| 67 | $_SERVER[$envkey] = $envvalue; |
||
| 68 | } |
||
| 69 | |||
| 70 | } else { |
||
| 71 | |||
| 72 | if (function_exists('putenv')) { |
||
| 73 | putenv("$envkey=$envvalue"); |
||
| 74 | } |
||
| 75 | |||
| 76 | $_ENV[$envkey] = $envvalue; |
||
| 77 | |||
| 78 | $_SERVER[$envkey] = $envvalue; |
||
| 79 | if (function_exists('apache_setenv')) { |
||
| 80 | /** @scrutinizer ignore-unhandled */ @apache_setenv($envkey, $envvalue, false); |
||
| 81 | } |
||
| 82 | |||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | protected function parseLines() : ParserCollector |
||
| 88 | { |
||
| 89 | foreach (getenv() as $key => $value) |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 90 | { |
||
| 91 | $this->parsedsystemvars = $this->parsercollector->offsetSet($key, $value); |
||
| 92 | } |
||
| 93 | |||
| 94 | if (empty($this->parser->arr->getArrayCopy())) { |
||
| 95 | return $this->parsercollector; |
||
| 96 | } |
||
| 97 | |||
| 98 | foreach ($this->parser->arr->getArrayCopy() as $key => $line) { |
||
| 99 | |||
| 100 | if ($this->variable_collector->isComment($line) && !$this->keepcomments) { |
||
| 101 | |||
| 102 | $this->parser->arr->offsetUnset($key); |
||
| 103 | |||
| 104 | } else { |
||
| 105 | |||
| 106 | list($k, $v) = $this->explodeKeyVal($line); |
||
| 107 | |||
| 108 | $key = $this->keyresolver->execute($k); |
||
| 109 | $value = $this->valueresolver->execute($v, $this->getStrictBool()); |
||
| 110 | |||
| 111 | $this->commentResolver[$key] = $this->valueresolver->resolveComment($v); |
||
| 112 | |||
| 113 | $value = $this->variable_collector->parseSystemEnvironmentVariables($value); |
||
| 114 | $value = $this->variable_collector->parseVariable($value, $this->parsercollector); |
||
| 115 | |||
| 116 | $this->parsercollector->offsetSet($key, $value); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | unset($this->parser->arr); |
||
| 121 | |||
| 122 | return $this->parsercollector; |
||
| 123 | } |
||
| 124 | |||
| 125 | protected function getStrictBool() |
||
| 126 | { |
||
| 127 | return $this->strictbool; |
||
| 128 | } |
||
| 129 | |||
| 130 | protected function getParsed() : ParserCollector |
||
| 131 | { |
||
| 132 | $parsedText = $this->parser->prepareLines($this->context); |
||
| 133 | |||
| 134 | $this->parser->explodeLines($parsedText); |
||
| 135 | |||
| 136 | return $this->parseLines(); |
||
| 137 | } |
||
| 138 | } |