-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForeachKeyTask.php
161 lines (132 loc) · 3.27 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
<?php
/**
* @file ForeachKeyTask.php
*
* Iterate over property values.
*
* @code
* <foreachkey prefix="drupal.sites" omitKeys="_defaults" target="mytarget" keyParam="key" prefixParam="prefix" />
* @endcode
*
* @copyright 2018 Palantir.net, Inc.
*/
namespace TheBuild;
use BuildException;
use StringHelper;
class ForeachKeyTask extends \Task {
/**
* @var string
* Prefix of properties to iterate over.
*/
protected $prefix = '';
/**
* @var string
* Name of target to execute.
*/
protected $target = '';
/**
* @var string
* Name of parameter to use for the key.
*/
protected $keyParam = '';
/**
* @var string
* Name of parameter to use for the prefix.
*/
protected $prefixParam = '';
/**
* @var array
*/
protected $omitKeys = [];
/**
* @var \PhingCallTask
*/
protected $callee;
/**
*
*/
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 ($project->getProperties() as $name => $value) {
if (strpos($name, $this->prefix) === 0) {
$property_children = substr($name, strlen($this->prefix));
list($key, $property_grandchildren) = 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 ($keys as $key => $prefix) {
$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);
}
}
}
/**
* @param string $value
*/
public function setPrefix($value) {
if (!StringHelper::endsWith(".", $value)) {
$value .= ".";
}
$this->prefix = $value;
}
/**
* @param string $value
*/
public function setTarget($value) {
$this->target = $value;
}
/**
* @param string $value
*/
public function setKeyParam($value) {
$this->keyParam = $value;
}
/**
* @param string $value
*/
public function setPrefixParam($value) {
$this->prefixParam = $value;
}
/**
* @param string $value
*/
public function setOmitKeys($value) {
$this->omitKeys = array_map('trim', explode(',', $value));
}
}