Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DirectoryList.php
Go to the documentation of this file.
1 <?php
12 
25 {
29  const PATH = 'path';
30  const URL_PATH = 'uri';
36  const SYS_TMP = 'sys_tmp';
37 
43  private $root;
44 
50  private $directories;
51 
57  public static function getDefaultConfig()
58  {
59  return [self::SYS_TMP => [self::PATH => '']];
60  }
61 
69  public static function validate($config)
70  {
71  if (!is_array($config)) {
72  throw new \InvalidArgumentException('Unexpected value type.');
73  }
74  $defaultConfig = static::getDefaultConfig();
75  foreach ($config as $type => $row) {
76  if (!is_array($row)) {
77  throw new \InvalidArgumentException('Unexpected value type.');
78  }
79  if (!isset($defaultConfig[$type])) {
80  throw new \InvalidArgumentException("Unknown type: {$type}");
81  }
82  if (!isset($row[self::PATH]) && !isset($row[self::URL_PATH])) {
83  throw new \InvalidArgumentException("Missing required keys at: {$type}");
84  }
85  }
86  }
87 
94  public function __construct($root, array $config = [])
95  {
96  static::validate($config);
97  $this->root = $this->normalizePath($root);
98  $this->directories = static::getDefaultConfig();
99  $this->directories[self::SYS_TMP] = [self::PATH => realpath(sys_get_temp_dir())];
100 
101  // inject custom values from constructor
102  foreach ($this->directories as $code => $dir) {
103  foreach ([self::PATH, self::URL_PATH] as $key) {
104  if (isset($config[$code][$key])) {
105  $this->directories[$code][$key] = $config[$code][$key];
106  }
107  }
108  }
109 
110  // filter/validate values
111  foreach ($this->directories as $code => $dir) {
112  $path = $this->normalizePath($dir[self::PATH]);
113  if (!$this->isAbsolute($path)) {
114  $path = $this->prependRoot($path);
115  }
116  $this->directories[$code][self::PATH] = $path;
117 
118  if (isset($dir[self::URL_PATH])) {
119  $this->assertUrlPath($dir[self::URL_PATH]);
120  }
121  }
122  }
123 
130  private function normalizePath($path)
131  {
132  return str_replace('\\', '/', $path);
133  }
134 
145  private function assertUrlPath($urlPath)
146  {
147  if (!preg_match('/^([a-z0-9_]+[a-z0-9\._]*(\/[a-z0-9_]+[a-z0-9\._]*)*)?$/', $urlPath)) {
148  throw new \InvalidArgumentException(
149  "URL path must be relative directory path in lowercase with '/' directory separator: '{$urlPath}'"
150  );
151  }
152  }
153 
160  protected function prependRoot($path)
161  {
162  $root = $this->getRoot();
163  return $root . ($root && $path ? '/' : '') . $path;
164  }
165 
172  protected function isAbsolute($path)
173  {
174  $path = strtr($path, '\\', '/');
175 
176  if (strpos($path, '/') === 0) {
177  //is UnixRoot
178  return true;
179  } elseif (preg_match('#^\w{1}:/#', $path)) {
180  //is WindowsRoot
181  return true;
182  } elseif (parse_url($path, PHP_URL_SCHEME) !== null) {
183  //is WindowsLetter
184  return true;
185  }
186 
187  return false;
188  }
189 
195  public function getRoot()
196  {
197  return $this->root;
198  }
199 
207  public function getPath($code)
208  {
209  $this->assertCode($code);
210  return $this->directories[$code][self::PATH];
211  }
212 
219  public function getUrlPath($code)
220  {
221  $this->assertCode($code);
222  if (!isset($this->directories[$code][self::URL_PATH])) {
223  return false;
224  }
225  return $this->directories[$code][self::URL_PATH];
226  }
227 
235  private function assertCode($code)
236  {
237  if (!isset($this->directories[$code])) {
238  throw new \Magento\Framework\Exception\FileSystemException(
239  new \Magento\Framework\Phrase('Unknown directory type: \'%1\'', [$code])
240  );
241  }
242  }
243 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
$type
Definition: item.phtml:13
$code
Definition: info.phtml:12