|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Value\Env; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* --env-file <value> |
|
9
|
|
|
*/ |
|
10
|
|
|
class EnvFile implements \IteratorAggregate, \Countable |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array|EnvVar[] |
|
14
|
|
|
*/ |
|
15
|
|
|
private $env = array(); |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $path; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $path |
|
24
|
|
|
*/ |
|
25
|
7 |
|
public function __construct($path) |
|
26
|
|
|
{ |
|
27
|
7 |
|
$this->path = (string)$path; |
|
28
|
7 |
|
$this->loadPath($this->path); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function __toString() |
|
32
|
|
|
{ |
|
33
|
1 |
|
return $this->path; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function getPairs() |
|
40
|
|
|
{ |
|
41
|
1 |
|
$pairs = array(); |
|
42
|
1 |
|
foreach ($this->env as $var) { |
|
43
|
1 |
|
$pairs[] = $var->getPair(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
return $pairs; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function count() |
|
50
|
|
|
{ |
|
51
|
1 |
|
return count($this->env); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function getIterator() |
|
55
|
|
|
{ |
|
56
|
1 |
|
return new \ArrayIterator($this->env); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
7 |
|
private function loadPath($path) |
|
60
|
|
|
{ |
|
61
|
7 |
|
$lines = @file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
62
|
7 |
|
if (false === $lines) { |
|
63
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
64
|
|
|
"File read error: '%s'", |
|
65
|
|
|
$path |
|
66
|
|
|
)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
6 |
|
$definitions = preg_grep('~^(\s*#.*|\s*)$~', $lines, PREG_GREP_INVERT); |
|
70
|
6 |
|
if (false === $definitions) { |
|
71
|
|
|
// @codeCoverageIgnoreStart |
|
72
|
|
|
throw new \UnexpectedValueException(sprintf( |
|
73
|
|
|
"Failure getting definitions from file: '%s'", |
|
74
|
|
|
$path |
|
75
|
|
|
)); |
|
76
|
|
|
// @codeCoverageIgnoreEnd |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
6 |
|
$this->addEnvVarDefs($definitions); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
6 |
|
private function addEnvVarDefs(array $envVarDefs) |
|
83
|
|
|
{ |
|
84
|
6 |
|
foreach ($envVarDefs as $index => $envVarDef) { |
|
85
|
|
|
try { |
|
86
|
6 |
|
$var = new EnvVar($envVarDef); |
|
87
|
1 |
|
} catch (\InvalidArgumentException $exception) { |
|
88
|
1 |
|
$context = new \ErrorException( |
|
89
|
1 |
|
$exception->getMessage(), |
|
90
|
|
|
0, |
|
91
|
|
|
1, |
|
92
|
1 |
|
$this->path, |
|
93
|
1 |
|
$index + 1, |
|
94
|
|
|
$exception |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
1 |
|
throw new \InvalidArgumentException( |
|
98
|
1 |
|
sprintf('%s:%d %s', $this->path, $index + 1, $exception->getMessage()), |
|
99
|
|
|
0, |
|
100
|
|
|
$context |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
5 |
|
$this->env[] = $var; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|