1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace G2R\Config; |
4
|
|
|
|
5
|
|
|
use G2R\Exception\Exception; |
6
|
|
|
|
7
|
|
|
class ProjectConfig extends AbstractConfig |
8
|
|
|
{ |
9
|
12 |
|
public function __construct($path) |
10
|
|
|
{ |
11
|
12 |
|
parent::__construct($path); |
12
|
|
|
|
13
|
12 |
|
$this->setProjectDefaults(); |
14
|
12 |
|
} |
15
|
|
|
|
16
|
12 |
|
protected function getDefaults() |
17
|
|
|
{ |
18
|
|
|
return [ |
19
|
12 |
|
'base_url' => 'http://gitlab.local', |
20
|
|
|
]; |
21
|
|
|
} |
22
|
|
|
|
23
|
12 |
|
protected function getRequirements() |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
12 |
|
'base_url', |
27
|
|
|
'projects', |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
12 |
|
protected function getProjectDefaults() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
12 |
|
'ref' => 'master', |
35
|
|
|
'runOnFail' => false, |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
12 |
|
protected function setProjectDefaults() |
40
|
|
|
{ |
41
|
12 |
|
$projects = $this['projects']; |
42
|
|
|
|
43
|
|
|
array_walk($projects, function (&$project) { |
44
|
12 |
|
$project['project'] = array_merge( |
45
|
12 |
|
$this->getProjectDefaults(), |
46
|
12 |
|
$project['project'] |
47
|
|
|
); |
48
|
12 |
|
}); |
49
|
|
|
|
50
|
12 |
|
$this['projects'] = $projects; |
51
|
12 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Try to find a project and return it. |
55
|
|
|
* |
56
|
|
|
* @return array $project |
57
|
|
|
*/ |
58
|
10 |
|
protected function findProject($name, $ref) |
59
|
|
|
{ |
60
|
10 |
|
$projects = array_filter( |
61
|
10 |
|
$this->get('projects'), |
62
|
10 |
|
function ($config) use ($name, $ref) { |
63
|
|
|
return |
64
|
10 |
|
$config['project']['name'] == $name && |
65
|
10 |
|
$config['project']['ref'] == $ref; |
66
|
10 |
|
} |
67
|
|
|
); |
68
|
|
|
|
69
|
10 |
|
if (count($projects) > 1) { |
70
|
2 |
|
throw new Exception("Duplicate project {$name}[{$ref}]"); |
71
|
|
|
} |
72
|
|
|
|
73
|
8 |
|
if (count($projects) === 0) { |
74
|
2 |
|
throw new Exception("Project {$name}[{$ref}] not found"); |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
return array_shift($projects)['project']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return a project identified by its short url. |
82
|
|
|
* |
83
|
|
|
* @param $name |
84
|
|
|
* @param string $ref |
85
|
|
|
* |
86
|
|
|
* @throws Exception |
87
|
|
|
* |
88
|
|
|
* @return array $project |
89
|
|
|
*/ |
90
|
10 |
|
public function getProject($name, $ref = 'master') |
91
|
|
|
{ |
92
|
10 |
|
$project = $this->findProject($name, $ref); |
93
|
|
|
|
94
|
6 |
|
if (!isset($project['jobId'])) { |
95
|
2 |
|
throw new Exception( |
96
|
2 |
|
"The jobId is missing for the {$name}[{$ref}] project" |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
return $project; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|