|
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
|
11 |
|
public function __construct(Filesystem $fs, array $arguments) |
|
24
|
|
|
{ |
|
25
|
11 |
|
$this->fs = $fs; |
|
26
|
10 |
|
$this->arguments = $arguments; |
|
27
|
10 |
|
} |
|
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
|
9 |
|
public function preparePath($path) |
|
43
|
|
|
{ |
|
44
|
9 |
|
if (($env = $this->getArgumentValue('env')) !== false) { |
|
45
|
9 |
|
return str_replace("{env}", $env, $path); |
|
46
|
|
|
} else { |
|
47
|
|
|
return $path; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $name |
|
53
|
|
|
* @return string|false |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getArgumentValue($name) |
|
56
|
|
|
{ |
|
57
|
9 |
|
return array_reduce($this->arguments, function($carry, $item) use ($name) { |
|
58
|
9 |
|
if (substr(strtolower($item), 0, strlen($name) + 2) == '--' . $name) { |
|
59
|
8 |
|
$val = explode('=', $item); |
|
60
|
8 |
|
return trim($val[1]); |
|
61
|
|
|
} else { |
|
62
|
1 |
|
return $carry; |
|
63
|
|
|
} |
|
64
|
9 |
|
}, false); |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function initDirectory($dir) |
|
69
|
|
|
{ |
|
70
|
1 |
|
if (!$this->fs->exists($dir)) { |
|
71
|
1 |
|
$this->fs->mkdir($dir); |
|
72
|
1 |
|
} |
|
73
|
1 |
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|