|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\File; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Ktomk\Pipelines\Glob; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class PipelinesReferences |
|
12
|
|
|
* |
|
13
|
|
|
* Extract methods from Pipelines |
|
14
|
|
|
* |
|
15
|
|
|
* @package Ktomk\Pipelines\File |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class PipelinesReferences extends Pipelines |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param Pipelines $pipelines |
|
21
|
|
|
* @param string $id |
|
22
|
|
|
* |
|
23
|
|
|
* @throws InvalidArgumentException |
|
24
|
|
|
* @throws ParseException |
|
25
|
|
|
* |
|
26
|
|
|
* @return null|Pipeline |
|
27
|
|
|
*/ |
|
28
|
12 |
|
protected static function byId(Pipelines $pipelines, $id) |
|
29
|
|
|
{ |
|
30
|
12 |
|
if (!ReferenceTypes::isValidId($id)) { |
|
31
|
1 |
|
throw new InvalidArgumentException(sprintf("Invalid id '%s'", $id)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
11 |
|
if (!isset($pipelines->references[$id])) { |
|
35
|
1 |
|
return null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
10 |
|
$ref = $pipelines->references[$id]; |
|
39
|
10 |
|
if ($ref[2] instanceof Pipeline) { |
|
40
|
3 |
|
return $ref[2]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// bind to instance if yet an array |
|
44
|
10 |
|
if (!is_array($ref[2])) { |
|
45
|
1 |
|
throw new ParseException(sprintf('%s: named pipeline required', $id)); |
|
46
|
|
|
} |
|
47
|
9 |
|
$pipeline = new Pipeline($pipelines->file, $ref[2]); |
|
|
|
|
|
|
48
|
9 |
|
$ref[2] = $pipeline; |
|
49
|
|
|
|
|
50
|
9 |
|
return $pipeline; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* returns the id of the default pipeline in file or null if there is none |
|
55
|
|
|
* |
|
56
|
|
|
* @param Pipelines $pipelines |
|
57
|
|
|
* |
|
58
|
|
|
* @return null|string |
|
59
|
|
|
*/ |
|
60
|
3 |
|
protected static function idDefault(Pipelines $pipelines) |
|
61
|
|
|
{ |
|
62
|
3 |
|
$id = 'default'; |
|
63
|
|
|
|
|
64
|
3 |
|
if (!isset($pipelines->references[$id])) { |
|
65
|
1 |
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
return $id; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* get id of a pipeline |
|
73
|
|
|
* |
|
74
|
|
|
* @param Pipelines $pipelines |
|
75
|
|
|
* @param Pipeline $pipeline |
|
76
|
|
|
* |
|
77
|
|
|
* @return null|string |
|
78
|
|
|
*/ |
|
79
|
2 |
|
protected static function id(Pipelines $pipelines, Pipeline $pipeline) |
|
80
|
|
|
{ |
|
81
|
2 |
|
foreach ($pipelines->references as $id => $reference) { |
|
82
|
2 |
|
if ($pipeline === $reference[2]) { |
|
83
|
1 |
|
return $id; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param Pipelines $pipelines |
|
92
|
|
|
* @param null|string $type |
|
93
|
|
|
* @param null|string $reference |
|
94
|
|
|
* |
|
95
|
|
|
* @throws InvalidArgumentException |
|
96
|
|
|
* |
|
97
|
|
|
* @return null|string |
|
98
|
|
|
*/ |
|
99
|
9 |
|
protected static function idByTypeReference(Pipelines $pipelines, $type, $reference) |
|
100
|
|
|
{ |
|
101
|
9 |
|
if (!ReferenceTypes::isPatternSection($type)) { |
|
102
|
1 |
|
throw new InvalidArgumentException(sprintf('Invalid type %s', var_export($type, true))); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
8 |
|
list($resolve, $result) = self::idNonPatternMatch($pipelines, $type, $reference); |
|
106
|
8 |
|
if ($resolve) { |
|
107
|
5 |
|
return $result; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
6 |
|
list($resolve, $result) = self::idPattern($pipelines, $type, $reference); |
|
111
|
|
|
|
|
112
|
|
|
# fall-back to default pipeline on no match |
|
113
|
6 |
|
return $resolve ? $result : self::idDefault($pipelines); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param Pipelines $pipelines |
|
118
|
|
|
* @param null|string $section |
|
119
|
|
|
* @param null|string $reference |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
8 |
|
private static function idNonPatternMatch(Pipelines $pipelines, $section, $reference) |
|
124
|
|
|
{ |
|
125
|
|
|
# section is n/a, fall back to default pipeline |
|
126
|
8 |
|
if (!isset($pipelines->array[$section])) { |
|
127
|
2 |
|
return array(true, $pipelines->getIdDefault()); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
# check for direct (non-pattern) match |
|
131
|
7 |
|
if (isset($pipelines->array[$section][$reference])) { |
|
132
|
3 |
|
return array(true, "${section}/${reference}"); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
6 |
|
return array(false, null); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* get entry with largest pattern to match |
|
140
|
|
|
* |
|
141
|
|
|
* @param Pipelines $pipelines |
|
142
|
|
|
* @param string $section |
|
143
|
|
|
* @param string $reference |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
private static function idPattern(Pipelines $pipelines, $section, $reference) |
|
148
|
|
|
{ |
|
149
|
6 |
|
$patterns = array_keys($pipelines->array[$section]); |
|
150
|
|
|
|
|
151
|
6 |
|
$match = ''; |
|
152
|
6 |
|
foreach ($patterns as $pattern) { |
|
153
|
6 |
|
$pattern = (string)$pattern; |
|
154
|
6 |
|
$result = Glob::match($pattern, $reference); |
|
155
|
6 |
|
if ($result && (strlen($pattern) > strlen($match))) { |
|
156
|
4 |
|
$match = $pattern; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
6 |
|
return array('' !== $match, "${section}/${match}"); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|