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