aboutsummaryrefslogtreecommitdiff
path: root/src/Dotenv.php
blob: 8e7ac1002caef720c2414d97d576a5712630460f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
 * @author: Alexander Kiryukhin <alexander@kiryukhin.su>
 * @license: MIT
 */

namespace NeonXP\Dotenv;

use NeonXP\Dotenv\Compiler\CompilerInterface;
use NeonXP\Dotenv\Exception\RuntimeException;
use NeonXP\Dotenv\Loader\LoaderInterface;
use NeonXP\Dotenv\Parser\ParserInterface;
use NeonXP\Dotenv\Types\KeyValue;

class Dotenv implements \ArrayAccess, \IteratorAggregate
{
    /**
     * @var LoaderInterface
     */
    private $loader;
    /**
     * @var ParserInterface
     */
    private $parser;
    /**
     * @var CompilerInterface
     */
    private $compiler;

    /**
     * @var array
     */
    private $loadedValues;

    /**
     * Dotenv constructor.
     * @param LoaderInterface $loader
     * @param ParserInterface $parser
     * @param CompilerInterface $compiler
     */
    public function __construct(LoaderInterface $loader = null, ParserInterface $parser = null, CompilerInterface $compiler = null)
    {
        $this->loader = $loader;
        $this->parser = $parser;
        $this->compiler = $compiler;
    }

    /**
     * Load .env file using loader and parse it
     * @param string $filePath
     * @return Dotenv
     */
    public function load(string $filePath = '.env'): Dotenv
    {
        $lines = $this->loader->load($filePath);
        $rawData = array_map([$this->parser, 'parseLine'], $lines);
        $this->compiler->setRawCollection($rawData);
        $this->loadedValues = array_reduce(
            array_map([$this->compiler, 'compileKeyValue'], $rawData),
            function (array $acc, KeyValue $current) {
                $acc[$current->getKey()] = $current->getValue();
                return $acc;
            },
            []
        );

        return $this;
    }

    /**
     * Is key exists
     * @param string $key
     * @return bool
     * @throws RuntimeException
     */
    public function has(string $key): bool
    {
        $this->checkIsLoaded();
        return isset($this->loadedValues[$key]);
    }

    /**
     * @param string $key
     * @param mixed|null $default
     * @return mixed
     * @throws RuntimeException
     */
    public function get(string $key, mixed $default = null): mixed
    {
        if (!$this->has($key)) {
            return $default;
        }
        return $this->loadedValues[$key];
    }

    /**
     * @internal
     * @throws RuntimeException
     * @return void
     */
    protected function checkIsLoaded(): void
    {
        if (!$this->loadedValues) {
            throw new RuntimeException('Dotenv file not loaded');
        }
    }

    /**
     * @inheritdoc
     * @return \ArrayIterator|\Traversable
     * @throws RuntimeException
     */
    public function getIterator()
    {
        $this->checkIsLoaded();
        return new \ArrayIterator($this->loadedValues);
    }

    /**
     * @inheritdoc
     * @param string $offset
     * @return bool
     * @throws RuntimeException
     */
    public function offsetExists($offset)
    {
        return $this->has($offset);
    }

    /**
     * @inheritdoc
     * @param string $offset
     * @return mixed
     * @throws RuntimeException
     */
    public function offsetGet($offset)
    {
        return $this->get($offset);
    }

    /**
     * @inheritdoc
     * @param string $offset
     * @param mixed $value
     * @throws RuntimeException
     */
    public function offsetSet($offset, $value)
    {
        throw new RuntimeException('Collection is immutable');
    }

    /**
     * @inheritdoc
     * @param string $offset
     * @throws RuntimeException
     */
    public function offsetUnset($offset)
    {
        throw new RuntimeException('Collection is immutable');
    }


}