/
Node.php
177 lines (133 loc) · 4.13 KB
/
Node.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
172
173
174
175
176
177
<?php
namespace phake;
class Node
{
private $parent;
private $name;
private $hidden = false;
private $deps = array();
private $desc = null;
private $has_run = false;
private $before = array();
private $lambdas = array();
private $after = array();
private $children = array();
public function __construct(Node $parent = null, $name = '') {
$this->parent = $parent;
$this->name = $name;
}
public function get_name() {
$name = '';
$parent = $this->parent;
while ($parent !== null && $parent->parent !== null) {
$name = $parent->name . ':' . $name;
$parent = $parent->parent;
}
return $name . $this->name;
}
public function get_parent() {
return $this->parent;
}
public function get_root() {
$root = $this;
while ($root->parent !== null) {
$root = $root->parent;
}
return $root;
}
public function child_with_name($task_name, &$levels = 0) {
$parts = explode(':', $task_name);
$task = $this;
foreach ($parts as $part) {
if (!isset($task->children[$part])) {
$task->children[$part] = new Node($task, $part);
}
$task = $task->children[$part];
}
$levels = sizeof($parts);
return $task;
}
public function add_before($closure) { $this->before[] = $closure; }
public function add_lambda($closure) { $this->lambdas[] = $closure; }
public function add_after($closure) { $this->after[] = $closure; }
public function is_hidden() {
return $this->hidden;
}
public function hide() {
$this->hidden = true;
}
public function has_description() {
return $this->desc !== null;
}
public function get_description() {
return $this->desc;
}
public function set_description($d) {
$this->desc = $d;
}
public function add_dependency($taskname) {
$this->deps[] = $taskname;
}
public function reset() {
$this->has_run = false;
foreach ($this->children as $c) $c->reset();
}
public function invoke(Application $application) {
foreach ($this->get_dependencies() as $t) $t->invoke($application);
if ($this->has_run) {
return;
}
foreach ($this->before as $t) $t($application);
foreach ($this->lambdas as $t) $t($application);
foreach ($this->after as $t) $t($application);
$this->has_run = true;
}
public function get_dependencies() {
$deps = array();
foreach ($this->deps as $depName) {
$task = $this->parent->get_task($depName);
$deps[$task->get_name()] = $task;
}
return $deps;
}
public function has_dependencies() {
return !!$this->get_dependencies();
}
public function has_body() {
return !!$this->lambdas;
}
public function get_task($task_name) {
if ($task_name[0] != ':') {
$parts = explode(':', $task_name);
$task = $this;
foreach ($parts as $part) {
if (isset($task->children[$part])) {
$task = $task->children[$part];
} else {
$task = null;
break;
}
}
if ($task !== null) {
return $task;
} else if ($this->parent) {
return $this->parent->get_task($task_name);
} else {
throw new TaskNotFoundException($task_name);
}
} else {
return $this->get_root()->get_task(substr($task_name, 1));
}
}
public function is_visible() {
return (!$this->is_hidden() && ($this->has_body() || $this->has_dependencies()));
}
public function get_tasks() {
$tasks = array();
foreach ($this->children as $child) {
$tasks[$child->get_name()] = $child;
$tasks += $child->get_tasks();
}
return $tasks;
}
}