Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Dotenv\Repository;
6
 
7
use Dotenv\Repository\Adapter\AdapterInterface;
8
use Dotenv\Repository\Adapter\EnvConstAdapter;
9
use Dotenv\Repository\Adapter\GuardedWriter;
10
use Dotenv\Repository\Adapter\ImmutableWriter;
11
use Dotenv\Repository\Adapter\MultiReader;
12
use Dotenv\Repository\Adapter\MultiWriter;
13
use Dotenv\Repository\Adapter\ReaderInterface;
14
use Dotenv\Repository\Adapter\ServerConstAdapter;
15
use Dotenv\Repository\Adapter\WriterInterface;
16
use InvalidArgumentException;
17
use PhpOption\Some;
18
use ReflectionClass;
19
 
20
final class RepositoryBuilder
21
{
22
    /**
23
     * The set of default adapters.
24
     */
25
    private const DEFAULT_ADAPTERS = [
26
        ServerConstAdapter::class,
27
        EnvConstAdapter::class,
28
    ];
29
 
30
    /**
31
     * The set of readers to use.
32
     *
33
     * @var \Dotenv\Repository\Adapter\ReaderInterface[]
34
     */
35
    private $readers;
36
 
37
    /**
38
     * The set of writers to use.
39
     *
40
     * @var \Dotenv\Repository\Adapter\WriterInterface[]
41
     */
42
    private $writers;
43
 
44
    /**
45
     * Are we immutable?
46
     *
47
     * @var bool
48
     */
49
    private $immutable;
50
 
51
    /**
52
     * The variable name allow list.
53
     *
54
     * @var string[]|null
55
     */
56
    private $allowList;
57
 
58
    /**
59
     * Create a new repository builder instance.
60
     *
61
     * @param \Dotenv\Repository\Adapter\ReaderInterface[] $readers
62
     * @param \Dotenv\Repository\Adapter\WriterInterface[] $writers
63
     * @param bool                                         $immutable
64
     * @param string[]|null                                $allowList
65
     *
66
     * @return void
67
     */
68
    private function __construct(array $readers = [], array $writers = [], bool $immutable = false, array $allowList = null)
69
    {
70
        $this->readers = $readers;
71
        $this->writers = $writers;
72
        $this->immutable = $immutable;
73
        $this->allowList = $allowList;
74
    }
75
 
76
    /**
77
     * Create a new repository builder instance with no adapters added.
78
     *
79
     * @return \Dotenv\Repository\RepositoryBuilder
80
     */
81
    public static function createWithNoAdapters()
82
    {
83
        return new self();
84
    }
85
 
86
    /**
87
     * Create a new repository builder instance with the default adapters added.
88
     *
89
     * @return \Dotenv\Repository\RepositoryBuilder
90
     */
91
    public static function createWithDefaultAdapters()
92
    {
93
        $adapters = \iterator_to_array(self::defaultAdapters());
94
 
95
        return new self($adapters, $adapters);
96
    }
97
 
98
    /**
99
     * Return the array of default adapters.
100
     *
101
     * @return \Generator<\Dotenv\Repository\Adapter\AdapterInterface>
102
     */
103
    private static function defaultAdapters()
104
    {
105
        foreach (self::DEFAULT_ADAPTERS as $adapter) {
106
            $instance = $adapter::create();
107
            if ($instance->isDefined()) {
108
                yield $instance->get();
109
            }
110
        }
111
    }
112
 
113
    /**
114
     * Determine if the given name if of an adapterclass.
115
     *
116
     * @param string $name
117
     *
118
     * @return bool
119
     */
120
    private static function isAnAdapterClass(string $name)
121
    {
122
        if (!\class_exists($name)) {
123
            return false;
124
        }
125
 
126
        return (new ReflectionClass($name))->implementsInterface(AdapterInterface::class);
127
    }
128
 
129
    /**
130
     * Creates a repository builder with the given reader added.
131
     *
132
     * Accepts either a reader instance, or a class-string for an adapter. If
133
     * the adapter is not supported, then we silently skip adding it.
134
     *
135
     * @param \Dotenv\Repository\Adapter\ReaderInterface|string $reader
136
     *
137
     * @throws \InvalidArgumentException
138
     *
139
     * @return \Dotenv\Repository\RepositoryBuilder
140
     */
141
    public function addReader($reader)
142
    {
143
        if (!(\is_string($reader) && self::isAnAdapterClass($reader)) && !($reader instanceof ReaderInterface)) {
144
            throw new InvalidArgumentException(
145
                \sprintf(
146
                    'Expected either an instance of %s or a class-string implementing %s',
147
                    ReaderInterface::class,
148
                    AdapterInterface::class
149
                )
150
            );
151
        }
152
 
153
        $optional = Some::create($reader)->flatMap(static function ($reader) {
154
            return \is_string($reader) ? $reader::create() : Some::create($reader);
155
        });
156
 
157
        $readers = \array_merge($this->readers, \iterator_to_array($optional));
158
 
159
        return new self($readers, $this->writers, $this->immutable, $this->allowList);
160
    }
161
 
162
    /**
163
     * Creates a repository builder with the given writer added.
164
     *
165
     * Accepts either a writer instance, or a class-string for an adapter. If
166
     * the adapter is not supported, then we silently skip adding it.
167
     *
168
     * @param \Dotenv\Repository\Adapter\WriterInterface|string $writer
169
     *
170
     * @throws \InvalidArgumentException
171
     *
172
     * @return \Dotenv\Repository\RepositoryBuilder
173
     */
174
    public function addWriter($writer)
175
    {
176
        if (!(\is_string($writer) && self::isAnAdapterClass($writer)) && !($writer instanceof WriterInterface)) {
177
            throw new InvalidArgumentException(
178
                \sprintf(
179
                    'Expected either an instance of %s or a class-string implementing %s',
180
                    WriterInterface::class,
181
                    AdapterInterface::class
182
                )
183
            );
184
        }
185
 
186
        $optional = Some::create($writer)->flatMap(static function ($writer) {
187
            return \is_string($writer) ? $writer::create() : Some::create($writer);
188
        });
189
 
190
        $writers = \array_merge($this->writers, \iterator_to_array($optional));
191
 
192
        return new self($this->readers, $writers, $this->immutable, $this->allowList);
193
    }
194
 
195
    /**
196
     * Creates a repository builder with the given adapter added.
197
     *
198
     * Accepts either an adapter instance, or a class-string for an adapter. If
199
     * the adapter is not supported, then we silently skip adding it. We will
200
     * add the adapter as both a reader and a writer.
201
     *
202
     * @param \Dotenv\Repository\Adapter\WriterInterface|string $adapter
203
     *
204
     * @throws \InvalidArgumentException
205
     *
206
     * @return \Dotenv\Repository\RepositoryBuilder
207
     */
208
    public function addAdapter($adapter)
209
    {
210
        if (!(\is_string($adapter) && self::isAnAdapterClass($adapter)) && !($adapter instanceof AdapterInterface)) {
211
            throw new InvalidArgumentException(
212
                \sprintf(
213
                    'Expected either an instance of %s or a class-string implementing %s',
214
                    WriterInterface::class,
215
                    AdapterInterface::class
216
                )
217
            );
218
        }
219
 
220
        $optional = Some::create($adapter)->flatMap(static function ($adapter) {
221
            return \is_string($adapter) ? $adapter::create() : Some::create($adapter);
222
        });
223
 
224
        $readers = \array_merge($this->readers, \iterator_to_array($optional));
225
        $writers = \array_merge($this->writers, \iterator_to_array($optional));
226
 
227
        return new self($readers, $writers, $this->immutable, $this->allowList);
228
    }
229
 
230
    /**
231
     * Creates a repository builder with mutability enabled.
232
     *
233
     * @return \Dotenv\Repository\RepositoryBuilder
234
     */
235
    public function immutable()
236
    {
237
        return new self($this->readers, $this->writers, true, $this->allowList);
238
    }
239
 
240
    /**
241
     * Creates a repository builder with the given allow list.
242
     *
243
     * @param string[]|null $allowList
244
     *
245
     * @return \Dotenv\Repository\RepositoryBuilder
246
     */
247
    public function allowList(array $allowList = null)
248
    {
249
        return new self($this->readers, $this->writers, $this->immutable, $allowList);
250
    }
251
 
252
    /**
253
     * Creates a new repository instance.
254
     *
255
     * @return \Dotenv\Repository\RepositoryInterface
256
     */
257
    public function make()
258
    {
259
        $reader = new MultiReader($this->readers);
260
        $writer = new MultiWriter($this->writers);
261
 
262
        if ($this->immutable) {
263
            $writer = new ImmutableWriter($writer, $reader);
264
        }
265
 
266
        if ($this->allowList !== null) {
267
            $writer = new GuardedWriter($writer, $this->allowList);
268
        }
269
 
270
        return new AdapterRepository($reader, $writer);
271
    }
272
}