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\Adapter;
6
 
7
final class ImmutableWriter implements WriterInterface
8
{
9
    /**
10
     * The inner writer to use.
11
     *
12
     * @var \Dotenv\Repository\Adapter\WriterInterface
13
     */
14
    private $writer;
15
 
16
    /**
17
     * The inner reader to use.
18
     *
19
     * @var \Dotenv\Repository\Adapter\ReaderInterface
20
     */
21
    private $reader;
22
 
23
    /**
24
     * The record of loaded variables.
25
     *
26
     * @var array<string,string>
27
     */
28
    private $loaded;
29
 
30
    /**
31
     * Create a new immutable writer instance.
32
     *
33
     * @param \Dotenv\Repository\Adapter\WriterInterface $writer
34
     * @param \Dotenv\Repository\Adapter\ReaderInterface $reader
35
     *
36
     * @return void
37
     */
38
    public function __construct(WriterInterface $writer, ReaderInterface $reader)
39
    {
40
        $this->writer = $writer;
41
        $this->reader = $reader;
42
        $this->loaded = [];
43
    }
44
 
45
    /**
46
     * Write to an environment variable, if possible.
47
     *
48
     * @param non-empty-string $name
49
     * @param string           $value
50
     *
51
     * @return bool
52
     */
53
    public function write(string $name, string $value)
54
    {
55
        // Don't overwrite existing environment variables
56
        // Ruby's dotenv does this with `ENV[key] ||= value`
57
        if ($this->isExternallyDefined($name)) {
58
            return false;
59
        }
60
 
61
        // Set the value on the inner writer
62
        if (!$this->writer->write($name, $value)) {
63
            return false;
64
        }
65
 
66
        // Record that we have loaded the variable
67
        $this->loaded[$name] = '';
68
 
69
        return true;
70
    }
71
 
72
    /**
73
     * Delete an environment variable, if possible.
74
     *
75
     * @param non-empty-string $name
76
     *
77
     * @return bool
78
     */
79
    public function delete(string $name)
80
    {
81
        // Don't clear existing environment variables
82
        if ($this->isExternallyDefined($name)) {
83
            return false;
84
        }
85
 
86
        // Clear the value on the inner writer
87
        if (!$this->writer->delete($name)) {
88
            return false;
89
        }
90
 
91
        // Leave the variable as fair game
92
        unset($this->loaded[$name]);
93
 
94
        return true;
95
    }
96
 
97
    /**
98
     * Determine if the given variable is externally defined.
99
     *
100
     * That is, is it an "existing" variable.
101
     *
102
     * @param non-empty-string $name
103
     *
104
     * @return bool
105
     */
106
    private function isExternallyDefined(string $name)
107
    {
108
        return $this->reader->read($name)->isDefined() && !isset($this->loaded[$name]);
109
    }
110
}