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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* Application file system directories dictionary.
*
* Provides information about what directories are available in the application.
* Serves as a customization point to specify different directories or add your own.
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Filesystem;
/**
* A list of directories
*
* Each list item consists of:
* - a directory path in the filesystem
* - optionally, a URL path
*
* This object is intended to be immutable (a "value object").
* The defaults are pre-defined and can be modified only by inheritors of this class.
* Through the constructor, it is possible to inject custom paths or URL paths, but impossible to inject new types.
*/
class DirectoryList
{
/**#@+
* Keys of directory configuration
*/
const PATH = 'path';
const URL_PATH = 'uri';
/**#@- */
/**
* System base temporary directory
*/
const SYS_TMP = 'sys_tmp';
/**
* Root path
*
* @var string
*/
private $root;
/**
* Directories configurations
*
* @var array
*/
private $directories;
/**
* Predefined types/paths
*
* @return array
*/
public static function getDefaultConfig()
{
return [self::SYS_TMP => [self::PATH => '']];
}
/**
* Validates format and contents of given configuration
*
* @param array $config
* @return void
* @throws \InvalidArgumentException
*/
public static function validate($config)
{
if (!is_array($config)) {
throw new \InvalidArgumentException('Unexpected value type.');
}
$defaultConfig = static::getDefaultConfig();
foreach ($config as $type => $row) {
if (!is_array($row)) {
throw new \InvalidArgumentException('Unexpected value type.');
}
if (!isset($defaultConfig[$type])) {
throw new \InvalidArgumentException("Unknown type: {$type}");
}
if (!isset($row[self::PATH]) && !isset($row[self::URL_PATH])) {
throw new \InvalidArgumentException("Missing required keys at: {$type}");
}
}
}
/**
* Constructor
*
* @param string $root
* @param array $config
*/
public function __construct($root, array $config = [])
{
static::validate($config);
$this->root = $this->normalizePath($root);
$this->directories = static::getDefaultConfig();
$this->directories[self::SYS_TMP] = [self::PATH => realpath(sys_get_temp_dir())];
// inject custom values from constructor
foreach ($this->directories as $code => $dir) {
foreach ([self::PATH, self::URL_PATH] as $key) {
if (isset($config[$code][$key])) {
$this->directories[$code][$key] = $config[$code][$key];
}
}
}
// filter/validate values
foreach ($this->directories as $code => $dir) {
$path = $this->normalizePath($dir[self::PATH]);
if (!$this->isAbsolute($path)) {
$path = $this->prependRoot($path);
}
$this->directories[$code][self::PATH] = $path;
if (isset($dir[self::URL_PATH])) {
$this->assertUrlPath($dir[self::URL_PATH]);
}
}
}
/**
* Converts slashes in path to a conventional unix-style
*
* @param string $path
* @return string
*/
private function normalizePath($path)
{
return str_replace('\\', '/', $path);
}
/**
* Validates a URL path
*
* Path must be usable as a fragment of a URL path.
* For interoperability and security purposes, no uppercase or "upper directory" paths like "." or ".."
*
* @param string $urlPath
* @return void
* @throws \InvalidArgumentException
*/
private function assertUrlPath($urlPath)
{
if (!preg_match('/^([a-z0-9_]+[a-z0-9\._]*(\/[a-z0-9_]+[a-z0-9\._]*)*)?$/', $urlPath)) {
throw new \InvalidArgumentException(
"URL path must be relative directory path in lowercase with '/' directory separator: '{$urlPath}'"
);
}
}
/**
* Concatenates root directory path with a relative path
*
* @param string $path
* @return string
*/
protected function prependRoot($path)
{
$root = $this->getRoot();
return $root . ($root && $path ? '/' : '') . $path;
}
/**
* Determine if a path is absolute
*
* @param string $path
* @return bool
*/
protected function isAbsolute($path)
{
$path = strtr($path, '\\', '/');
if (strpos($path, '/') === 0) {
//is UnixRoot
return true;
} elseif (preg_match('#^\w{1}:/#', $path)) {
//is WindowsRoot
return true;
} elseif (parse_url($path, PHP_URL_SCHEME) !== null) {
//is WindowsLetter
return true;
}
return false;
}
/**
* Gets a filesystem path of the root directory
*
* @return string
*/
public function getRoot()
{
return $this->root;
}
/**
* Gets a filesystem path of a directory
*
* @param string $code
* @return string
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function getPath($code)
{
$this->assertCode($code);
return $this->directories[$code][self::PATH];
}
/**
* Gets URL path of a directory
*
* @param string $code
* @return string|bool
*/
public function getUrlPath($code)
{
$this->assertCode($code);
if (!isset($this->directories[$code][self::URL_PATH])) {
return false;
}
return $this->directories[$code][self::URL_PATH];
}
/**
* Asserts that specified directory code is in the registry
*
* @param string $code
* @throws \Magento\Framework\Exception\FileSystemException
* @return void
*/
private function assertCode($code)
{
if (!isset($this->directories[$code])) {
throw new \Magento\Framework\Exception\FileSystemException(
new \Magento\Framework\Phrase('Unknown directory type: \'%1\'', [$code])
);
}
}
}