1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Paro\EnvironmentParameters; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
6
|
|
|
|
7
|
|
|
class FileHandler |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private $arguments; |
13
|
|
|
/** |
14
|
|
|
* @var Filesystem |
15
|
|
|
*/ |
16
|
|
|
private $fs; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* FileHandler constructor. |
20
|
|
|
* @param Filesystem $fs |
21
|
|
|
* @param array $arguments |
22
|
|
|
*/ |
23
|
17 |
|
public function __construct(Filesystem $fs, array $arguments) |
24
|
|
|
{ |
25
|
17 |
|
$this->fs = $fs; |
26
|
17 |
|
$this->arguments = $arguments; |
27
|
17 |
|
} |
28
|
|
|
|
29
|
4 |
|
public function resolvePath($currentPath, $importPath) |
30
|
|
|
{ |
31
|
4 |
|
if ($this->fs->isAbsolutePath($importPath)) { |
32
|
1 |
|
return $this->preparePath($importPath); |
33
|
|
|
} else { |
34
|
3 |
|
$path = dirname($currentPath) . DIRECTORY_SEPARATOR . $importPath; |
35
|
3 |
|
if (substr($path, 0, 4) == '././') { |
36
|
|
|
$path = substr($path, 2); |
37
|
|
|
} |
38
|
3 |
|
return $this->preparePath($path); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $path |
44
|
|
|
* @param null $env |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
9 |
|
public function preparePath($path, $env = null) |
48
|
|
|
{ |
49
|
9 |
|
if (is_null($env)) { |
50
|
9 |
|
$env = $this->getArgumentValue('env'); |
51
|
9 |
|
} |
52
|
|
|
|
53
|
9 |
|
if ($env !== false) { |
54
|
9 |
|
return str_replace('{env}', $env, $path); |
55
|
|
|
} else { |
56
|
|
|
return $path; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $name |
62
|
|
|
* @return string|false |
63
|
|
|
*/ |
64
|
|
|
public function getArgumentValue($name) |
65
|
|
|
{ |
66
|
9 |
|
return array_reduce($this->arguments, function($carry, $item) use ($name) { |
67
|
9 |
|
if (substr(strtolower($item), 0, strlen($name) + 2) == '--' . $name) { |
68
|
8 |
|
$val = explode('=', $item); |
69
|
8 |
|
return trim($val[1]); |
70
|
|
|
} else { |
71
|
1 |
|
return $carry; |
72
|
|
|
} |
73
|
9 |
|
}, false); |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function initDirectory($dir) |
78
|
|
|
{ |
79
|
1 |
|
if (!$this->fs->exists($dir)) { |
80
|
1 |
|
$this->fs->mkdir($dir); |
81
|
1 |
|
} |
82
|
1 |
|
} |
83
|
|
|
|
84
|
2 |
|
public function findFile($path) |
85
|
|
|
{ |
86
|
2 |
|
$env = $this->getArgumentValue('env'); |
87
|
2 |
|
if ($env !== false && strpos($env, DIRECTORY_SEPARATOR) > 0) { |
88
|
|
|
$envParts = explode(DIRECTORY_SEPARATOR, $env); |
89
|
|
|
while (count($envParts) > 0) { |
90
|
1 |
|
$fileName = $this->preparePath($path, join(DIRECTORY_SEPARATOR, $envParts)); |
91
|
|
|
if ($this->fs->exists($fileName)) { |
92
|
|
|
return $fileName; |
93
|
|
|
} |
94
|
|
|
unset($envParts[count($envParts) - 1]); |
95
|
|
|
} |
96
|
|
|
//root folder |
97
|
|
|
$fileName = str_replace('{env}/', '', $path); |
98
|
|
|
if ($this->fs->exists($fileName)) { |
99
|
|
|
return $fileName; |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
2 |
|
$fileName = $this->preparePath($path); |
103
|
2 |
|
if ($this->fs->exists($fileName)) { |
104
|
2 |
|
return $fileName; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
throw new \InvalidArgumentException(sprintf('File "%s" for environment "%s" doesn\'t exists', $path, $env)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $itemSource |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
7 |
|
public function processEnvironmentalVariable($itemSource) |
116
|
|
|
{ |
117
|
7 |
|
$item = trim($itemSource); |
118
|
6 |
|
if (substr(strtolower($item), 0, 5) === "%env(" && substr(strtolower($item), -2) == ')%') { |
119
|
1 |
|
$envName = substr(trim($item), 5); |
120
|
1 |
|
$envName = substr($envName, 0, strlen($envName) - 2); |
121
|
1 |
|
if (($envValue = getenv($envName)) === false) { |
122
|
|
|
throw new \InvalidArgumentException(sprintf('Environment variable "%s" doesn\'t exist', $envName)); |
123
|
|
|
} else { |
124
|
1 |
|
return $envValue; |
125
|
|
|
} |
126
|
|
|
} else { |
127
|
5 |
|
return $itemSource; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|