|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Utility; |
|
6
|
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\Cli\Args; |
|
8
|
|
|
use Ktomk\Pipelines\Cli\ArgsException; |
|
9
|
|
|
use Ktomk\Pipelines\Lib; |
|
10
|
|
|
use Ktomk\Pipelines\Runner\Env; |
|
11
|
|
|
use Ktomk\Pipelines\Runner\Reference; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class EnvParser |
|
15
|
|
|
* |
|
16
|
|
|
* @package Ktomk\Pipelines\Utility |
|
17
|
|
|
*/ |
|
18
|
|
|
class EnvParser |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Args |
|
22
|
|
|
*/ |
|
23
|
|
|
private $arguments; |
|
24
|
|
|
|
|
25
|
2 |
|
public static function create(Args $arguments) |
|
26
|
|
|
{ |
|
27
|
2 |
|
return new self($arguments); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* EnvParser constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param Args $arguments |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function __construct(Args $arguments) |
|
36
|
|
|
{ |
|
37
|
2 |
|
$this->arguments = $arguments; |
|
38
|
2 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $inherit from this environment |
|
42
|
|
|
* @param Reference $reference |
|
43
|
|
|
* @param string $workingDir |
|
44
|
|
|
* |
|
45
|
|
|
* @throws InvalidArgumentException |
|
46
|
|
|
* @throws ArgsException |
|
47
|
|
|
* |
|
48
|
|
|
* @return Env |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function parse(array $inherit, $reference, $workingDir) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$args = $this->arguments; |
|
53
|
|
|
|
|
54
|
1 |
|
Lib::v($inherit['BITBUCKET_REPO_SLUG'], basename($workingDir)); |
|
55
|
|
|
|
|
56
|
1 |
|
$env = Env::create($inherit); |
|
57
|
|
|
|
|
58
|
1 |
|
$noDotEnvFiles = $args->hasOption('no-dot-env-files'); |
|
59
|
1 |
|
$noDotEnvDotDist = $args->hasOption('no-dot-env-dot-dist'); |
|
60
|
|
|
|
|
61
|
1 |
|
if (false === $noDotEnvFiles) { |
|
62
|
1 |
|
$filesToCollect = array(); |
|
63
|
1 |
|
if (false === $noDotEnvDotDist) { |
|
64
|
1 |
|
$filesToCollect[] = $workingDir . '/.env.dist'; |
|
65
|
|
|
} |
|
66
|
1 |
|
$filesToCollect[] = $workingDir . '/.env'; |
|
67
|
1 |
|
$env->collectFiles($filesToCollect); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
$env->collect($args, array('e', 'env', 'env-file')); |
|
71
|
1 |
|
$resolved = $env->getVariables(); |
|
72
|
|
|
|
|
73
|
1 |
|
$env->initDefaultVars($resolved + $inherit); |
|
74
|
1 |
|
$env->addReference($reference); |
|
75
|
|
|
|
|
76
|
1 |
|
return $env; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|