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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Bootstrap;
use Magento\Framework\Filesystem\Glob;
/**
* Convenient access to the bootstrap settings
*/
class Settings
{
/**
* Base directory to be used to resolve relative paths
*
* @var string
*/
private $_baseDir;
/**
* Key-value pairs of the settings
*
* @var array
*/
private $_settings = [];
/**
* Constructor
*
* @param string $baseDir
* @param array $settings
* @throws \InvalidArgumentException
*/
public function __construct($baseDir, array $settings)
{
if (!is_dir($baseDir)) {
throw new \InvalidArgumentException("Base path '{$baseDir}' has to be an existing directory.");
}
$this->_baseDir = realpath($baseDir);
$this->_settings = $settings;
}
/**
* Retrieve a setting value as is
*
* @param string $settingName
* @param mixed $defaultValue
* @return mixed
*/
public function get($settingName, $defaultValue = null)
{
return array_key_exists($settingName, $this->_settings) ? $this->_settings[$settingName] : $defaultValue;
}
/**
* Interpret a setting value as a switch and return TRUE when it equals to the string "enabled" or FALSE otherwise
*
* @param string $settingName
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
*/
public function getAsBoolean($settingName)
{
return $this->get($settingName) === 'enabled';
}
/**
* Interpret a setting value as a relative file name and return absolute path to it
*
* @param string $settingName
* @param string $defaultValue
* @return string
*/
public function getAsFile($settingName, $defaultValue = '')
{
$result = $this->get($settingName, $defaultValue);
if ($result !== '') {
$result = $this->_resolvePath($result);
}
return $result;
}
/**
* Interpret a setting value as a file optionally falling back to the '.dist' file and return absolute path to it
*
* @param string $settingName
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAsConfigFile($settingName)
{
$result = $this->getAsFile($settingName);
if ($result !== '') {
if (!is_file($result) && substr($result, -5) != '.dist') {
$result .= '.dist';
}
if (is_file($result)) {
return $result;
}
}
throw new \Magento\Framework\Exception\LocalizedException(
__("Setting '%1' specifies the non-existing file '%2'.", $settingName, $result)
);
}
/**
* Interpret a setting value as a semicolon-separated relative glob pattern(s) and return matched absolute paths
*
* @param string $settingName
* @return array
*/
public function getAsMatchingPaths($settingName)
{
$settingValue = $this->get($settingName, '');
if ($settingValue !== '') {
return $this->_resolvePathPattern($settingValue);
}
return [];
}
/**
* Return an absolute path by a relative one without checking its validity
*
* @param string $relativePath
* @return string
*/
protected function _resolvePath($relativePath)
{
return $this->_baseDir . '/' . $relativePath;
}
/**
* Resolve semicolon-separated relative glob pattern(s) to matched absolute paths
*
* @param string $pattern
* @return array
*/
protected function _resolvePathPattern($pattern)
{
$result = [];
$allPatterns = preg_split('/\s*;\s*/', trim($pattern), -1, PREG_SPLIT_NO_EMPTY);
foreach ($allPatterns as $onePattern) {
$onePattern = $this->_resolvePath($onePattern);
$files = Glob::glob($onePattern, Glob::GLOB_BRACE);
$result = array_merge($result, $files);
}
return $result;
}
}