|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Runner\Containers; |
|
6
|
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\File\Pipeline\Step; |
|
8
|
|
|
use Ktomk\Pipelines\Value\Prefix; |
|
9
|
|
|
use UnexpectedValueException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class NameBuilder |
|
13
|
|
|
* |
|
14
|
|
|
* @package Ktomk\Pipelines\Runner\Containers |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class NameBuilder |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $string |
|
20
|
|
|
* @param string $replacement [optional] defaults to dash "-" |
|
21
|
|
|
* @param string $fallBack [optional] defaults to empty string |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
19 |
|
public static function slugify($string, $replacement = null, $fallBack = null) |
|
26
|
|
|
{ |
|
27
|
19 |
|
null === $replacement && $replacement = '-'; |
|
28
|
|
|
|
|
29
|
|
|
// all non-allowed characters -> replacement (which is normally a separator "_", "." or "-") |
|
30
|
19 |
|
$buffer = preg_replace('([^a-zA-Z0-9_.-]+)', (string)$replacement, (string)$string); |
|
31
|
19 |
|
if (false === $buffer) { |
|
32
|
|
|
// @codeCoverageIgnoreStart |
|
33
|
|
|
throw new UnexpectedValueException('regex operation failed'); |
|
34
|
|
|
// @codeCoverageIgnoreEnd |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// multiple separator(s) after each other -> one replacement (which is normally a separator) |
|
38
|
19 |
|
$buffer = preg_replace('(([_.-])[_.-]+)', (string)$replacement, $buffer); |
|
39
|
|
|
|
|
40
|
|
|
// not starting nor ending with a separator |
|
41
|
19 |
|
$buffer = trim($buffer, '_.-'); |
|
42
|
|
|
|
|
43
|
|
|
// not starting with a number |
|
44
|
19 |
|
$buffer = preg_replace('(^\d+([_.-]\d+)*)', (string)$replacement, $buffer); |
|
45
|
|
|
|
|
46
|
|
|
// multiple separator(s) after each other -> one replacement (which is normally a separator) |
|
47
|
19 |
|
$buffer = preg_replace('(([_.-])[_.-]+)', (string)$replacement, $buffer); |
|
48
|
|
|
|
|
49
|
|
|
// not starting nor ending with a separator |
|
50
|
19 |
|
$buffer = trim($buffer, '_.-'); |
|
51
|
|
|
|
|
52
|
|
|
// separator(s) only -> empty string |
|
53
|
19 |
|
$buffer = preg_replace('(^[_.-]+$)', '', $buffer); |
|
54
|
|
|
|
|
55
|
19 |
|
return '' === (string)$buffer ? (string)$fallBack : (string)$buffer; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* service container name |
|
60
|
|
|
* |
|
61
|
|
|
* example: pipelines.service-redis.pipelines |
|
62
|
|
|
* ^ ` ^ ` ^ ` ^ |
|
63
|
|
|
* | | | | |
|
64
|
|
|
* | "service" | project |
|
65
|
|
|
* prefix service name |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $prefix |
|
68
|
|
|
* @param string $serviceName |
|
69
|
|
|
* @param string $projectName |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public static function serviceContainerName($prefix, $serviceName, $projectName) |
|
74
|
|
|
{ |
|
75
|
2 |
|
return self::slugify( |
|
76
|
2 |
|
sprintf( |
|
77
|
2 |
|
'%s.service-%s', |
|
78
|
|
|
$prefix, |
|
79
|
2 |
|
implode( |
|
80
|
2 |
|
'.', |
|
81
|
|
|
array( |
|
82
|
2 |
|
self::slugify($serviceName, '-', 'unnamed'), |
|
83
|
2 |
|
$projectName, |
|
84
|
|
|
) |
|
85
|
|
|
) |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* step container name |
|
92
|
|
|
* |
|
93
|
|
|
* example: pipelines-1.pipeline-features-and-introspection.default.app |
|
94
|
|
|
* ^ `^` ^ ` ^ ` ^ |
|
95
|
|
|
* | | | | | |
|
96
|
|
|
* | step number step name pipeline id | |
|
97
|
|
|
* prefix project |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $pipelineId |
|
100
|
|
|
* @param string $stepName |
|
101
|
|
|
* @param int $stepNumber (step numbers start at one) |
|
102
|
|
|
* @param string $prefix |
|
103
|
|
|
* @param string $projectName |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public static function stepContainerName($pipelineId, $stepName, $stepNumber, $prefix, $projectName) |
|
108
|
|
|
{ |
|
109
|
2 |
|
return self::slugify( |
|
110
|
2 |
|
sprintf( |
|
111
|
2 |
|
'%s-%s', |
|
112
|
2 |
|
Prefix::verify($prefix), |
|
113
|
2 |
|
implode( |
|
114
|
2 |
|
'.', |
|
115
|
|
|
array( |
|
116
|
2 |
|
(string)(int)max(1, $stepNumber), |
|
117
|
2 |
|
self::slugify($stepName, '-', 'no-name'), |
|
118
|
2 |
|
self::slugify($pipelineId, '-', 'null'), |
|
119
|
2 |
|
$projectName, |
|
120
|
|
|
) |
|
121
|
|
|
) |
|
122
|
|
|
), |
|
123
|
2 |
|
'' |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* generate step container name |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $prefix |
|
131
|
|
|
* @param Step $step |
|
132
|
|
|
* @param string $projectName |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
* |
|
136
|
|
|
* @see StepContainer::generateName() |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public static function stepContainerNameByStep(Step $step, $prefix, $projectName) |
|
139
|
|
|
{ |
|
140
|
1 |
|
$pipelineId = $step->getPipeline()->getId(); |
|
141
|
1 |
|
$stepName = $step->getName(); |
|
142
|
1 |
|
$stepNumber = $step->getIndex() + 1; |
|
143
|
|
|
|
|
144
|
1 |
|
return self::stepContainerName($pipelineId, $stepName, $stepNumber, $prefix, $projectName); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|