-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForeachKeyTask.php
171 lines (146 loc) · 3.63 KB
/
ForeachKeyTask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace TheBuild;
/**
* Phing task to run a target for each property in an array.
*/
class ForeachKeyTask extends \Task {
/**
* Prefix of properties to iterate over.
*
* @var string
*/
protected $prefix = '';
/**
* Name of target to execute.
*
* @var string
*/
protected $target = '';
/**
* Name of parameter to use for the key.
*
* @var string
*/
protected $keyParam = '';
/**
* Name of parameter to use for the prefix.
*
* @var string
*/
protected $prefixParam = '';
/**
* Keys to ignore.
*
* @var array
*/
protected $omitKeys = [];
/**
* Instance of PhingCallTask to use/run.
*
* @var \PhingCallTask
*/
protected $callee;
/**
* {@inheritdoc}
*/
public function init() {
parent::init();
$this->callee = $this->project->createTask("phingcall");
$this->callee->setOwningTarget($this->getOwningTarget());
$this->callee->setTaskName($this->getTaskName());
$this->callee->setLocation($this->getLocation());
$this->callee->init();
}
/**
* Copy properties.
*/
public function main() {
$this->validate();
$this->callee->setTarget($this->target);
$this->callee->setInheritAll(TRUE);
$this->callee->setInheritRefs(TRUE);
// Extract matching keys from the properties array.
$keys = [];
$project = $this->getProject();
foreach (array_keys($project->getProperties()) as $name) {
if (strpos($name, $this->prefix) === 0) {
$property_children = substr($name, strlen($this->prefix));
// phpcs:ignore
[$key] = explode('.', $property_children, 2);
$keys[$key] = $key;
}
}
// Remove keys based on the 'omitKeys' attribute.
$keys = array_diff($keys, $this->omitKeys);
// Iterate over each extracted key.
foreach (array_keys($keys) as $key) {
$prop = $this->callee->createProperty();
$prop->setOverride(TRUE);
$prop->setName($this->keyParam);
$prop->setValue($key);
$prop = $this->callee->createProperty();
$prop->setOverride(TRUE);
$prop->setName($this->prefixParam);
$prop->setValue($this->prefix);
$this->callee->main();
}
}
/**
* Verify that the required attributes are set.
*/
public function validate() {
foreach (['prefix', 'target', 'keyParam', 'prefixParam'] as $attribute) {
if (empty($this->$attribute)) {
throw new \BuildException("$attribute attribute is required.", $this->location);
}
}
}
/**
* Use only keys with a certain prefix.
*
* @param string $value
* The key prefix.
*/
public function setPrefix($value) {
if (!\StringHelper::endsWith(".", $value)) {
$value .= ".";
}
$this->prefix = $value;
}
/**
* Set the target to run for each item.
*
* @param string $value
* Name of the target to run for each item.
*/
public function setTarget($value) {
$this->target = $value;
}
/**
* Set the parameter name to pass to the target.
*
* @param string $value
* Name of the parameter to pass to the target.
*/
public function setKeyParam($value) {
$this->keyParam = $value;
}
/**
* Name of the parameter where we can find the prefix.
*
* @param string $value
* The parameter name.
*/
public function setPrefixParam($value) {
$this->prefixParam = $value;
}
/**
* Remove a list of keys from the set of properties.
*
* @param string $value
* A comma-separated list of keys to remove from the array.
*/
public function setOmitKeys($value) {
$this->omitKeys = array_map('trim', explode(',', $value));
}
}