Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Spatie\Ignition\Config;
4
 
5
use Spatie\Ignition\Contracts\ConfigManager;
6
use Throwable;
7
 
8
class FileConfigManager implements ConfigManager
9
{
10
    private const SETTINGS_FILE_NAME = '.ignition.json';
11
 
12
    private string $path;
13
 
14
    private string $file;
15
 
16
    public function __construct(string $path = '')
17
    {
18
        $this->path = $this->initPath($path);
19
        $this->file = $this->initFile();
20
    }
21
 
22
    protected function initPath(string $path): string
23
    {
24
        $path = $this->retrievePath($path);
25
 
26
        if (! $this->isValidWritablePath($path)) {
27
            return '';
28
        }
29
 
30
        return $this->preparePath($path);
31
    }
32
 
33
    protected function retrievePath(string $path): string
34
    {
35
        if ($path !== '') {
36
            return $path;
37
        }
38
 
39
        return $this->initPathFromEnvironment();
40
    }
41
 
42
    protected function isValidWritablePath(string $path): bool
43
    {
44
        return @file_exists($path) && @is_writable($path);
45
    }
46
 
47
    protected function preparePath(string $path): string
48
    {
49
        return rtrim($path, DIRECTORY_SEPARATOR);
50
    }
51
 
52
    protected function initPathFromEnvironment(): string
53
    {
54
        if (! empty($_SERVER['HOMEDRIVE']) && ! empty($_SERVER['HOMEPATH'])) {
55
            return $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
56
        }
57
 
58
        if (! empty(getenv('HOME'))) {
59
            return getenv('HOME');
60
        }
61
 
62
        return '';
63
    }
64
 
65
    protected function initFile(): string
66
    {
67
        return $this->path . DIRECTORY_SEPARATOR . self::SETTINGS_FILE_NAME;
68
    }
69
 
70
    /** {@inheritDoc} */
71
    public function load(): array
72
    {
73
        return $this->readFromFile();
74
    }
75
 
76
    /** @return array<string, mixed> */
77
    protected function readFromFile(): array
78
    {
79
        if (! $this->isValidFile()) {
80
            return [];
81
        }
82
 
83
        $content = (string)file_get_contents($this->file);
84
        $settings = json_decode($content, true) ?? [];
85
 
86
        return $settings;
87
    }
88
 
89
    protected function isValidFile(): bool
90
    {
91
        return $this->isValidPath() &&
92
            @file_exists($this->file) &&
93
            @is_writable($this->file);
94
    }
95
 
96
    protected function isValidPath(): bool
97
    {
98
        return trim($this->path) !== '';
99
    }
100
 
101
    /** {@inheritDoc} */
102
    public function save(array $options): bool
103
    {
104
        if (! $this->createFile()) {
105
            return false;
106
        }
107
 
108
        return $this->saveToFile($options);
109
    }
110
 
111
    protected function createFile(): bool
112
    {
113
        if (! $this->isValidPath()) {
114
            return false;
115
        }
116
 
117
        if (@file_exists($this->file)) {
118
            return true;
119
        }
120
 
121
        return (file_put_contents($this->file, '') !== false);
122
    }
123
 
124
    /**
125
     * @param array<string, mixed> $options
126
     *
127
     * @return bool
128
     */
129
    protected function saveToFile(array $options): bool
130
    {
131
        try {
132
            $content = json_encode($options, JSON_THROW_ON_ERROR);
133
        } catch (Throwable) {
134
            return false;
135
        }
136
 
137
        return $this->writeToFile($content);
138
    }
139
 
140
    protected function writeToFile(string $content): bool
141
    {
142
        if (! $this->isValidFile()) {
143
            return false;
144
        }
145
 
146
        return (file_put_contents($this->file, $content) !== false);
147
    }
148
 
149
    /** {@inheritDoc} */
150
    public function getPersistentInfo(): array
151
    {
152
        return [
153
            'name' => self::SETTINGS_FILE_NAME,
154
            'path' => $this->path,
155
            'file' => $this->file,
156
        ];
157
    }
158
}