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 GuardedWriter 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 variable name allow list.
18
     *
19
     * @var string[]
20
     */
21
    private $allowList;
22
 
23
    /**
24
     * Create a new guarded writer instance.
25
     *
26
     * @param \Dotenv\Repository\Adapter\WriterInterface $writer
27
     * @param string[]                                   $allowList
28
     *
29
     * @return void
30
     */
31
    public function __construct(WriterInterface $writer, array $allowList)
32
    {
33
        $this->writer = $writer;
34
        $this->allowList = $allowList;
35
    }
36
 
37
    /**
38
     * Write to an environment variable, if possible.
39
     *
40
     * @param non-empty-string $name
41
     * @param string           $value
42
     *
43
     * @return bool
44
     */
45
    public function write(string $name, string $value)
46
    {
47
        // Don't set non-allowed variables
48
        if (!$this->isAllowed($name)) {
49
            return false;
50
        }
51
 
52
        // Set the value on the inner writer
53
        return $this->writer->write($name, $value);
54
    }
55
 
56
    /**
57
     * Delete an environment variable, if possible.
58
     *
59
     * @param non-empty-string $name
60
     *
61
     * @return bool
62
     */
63
    public function delete(string $name)
64
    {
65
        // Don't clear non-allowed variables
66
        if (!$this->isAllowed($name)) {
67
            return false;
68
        }
69
 
70
        // Set the value on the inner writer
71
        return $this->writer->delete($name);
72
    }
73
 
74
    /**
75
     * Determine if the given variable is allowed.
76
     *
77
     * @param non-empty-string $name
78
     *
79
     * @return bool
80
     */
81
    private function isAllowed(string $name)
82
    {
83
        return \in_array($name, $this->allowList, true);
84
    }
85
}