1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Git commit tree. |
4
|
|
|
* @package WordPress_GitHub_Sync |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class WordPress_GitHub_Sync_Tree |
9
|
|
|
*/ |
10
|
|
|
class WordPress_GitHub_Sync_Tree { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Current tree if retrieved, otherwise, error |
14
|
|
|
* |
15
|
|
|
* @var stdClass |
16
|
|
|
*/ |
17
|
|
|
protected $data; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Tree's sha. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $sha; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Tree's url. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $url; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Blobs keyed by path. |
35
|
|
|
* |
36
|
|
|
* @var WordPress_GitHub_Sync_Blob[] |
37
|
|
|
*/ |
38
|
|
|
protected $paths = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Blobs keyed by sha. |
42
|
|
|
* |
43
|
|
|
* @var WordPress_GitHub_Sync_Blob[] |
44
|
|
|
*/ |
45
|
|
|
protected $shas = array(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Whether the tree has changed. |
49
|
|
|
* |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
protected $changed = false; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Represents a commit tree. |
56
|
|
|
* |
57
|
|
|
* @param stdClass $data Raw tree data. |
58
|
|
|
*/ |
59
|
13 |
|
public function __construct( stdClass $data ) { |
60
|
13 |
|
$this->data = $data; |
61
|
|
|
|
62
|
13 |
|
$this->interpret_data(); |
63
|
13 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the tree's raw data. |
67
|
|
|
* |
68
|
|
|
* @return stdClass |
69
|
|
|
*/ |
70
|
1 |
|
public function get_data() { |
71
|
1 |
|
return $this->data; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return's the tree's sha. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
4 |
|
public function sha() { |
80
|
4 |
|
return $this->sha; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Updates the tree's sha. |
85
|
|
|
* |
86
|
|
|
* @param string $sha Tree sha. |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
1 |
|
public function set_sha( $sha ) { |
91
|
1 |
|
$this->sha = $sha; |
92
|
|
|
|
93
|
1 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the tree's url. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
2 |
|
public function url() { |
102
|
2 |
|
return $this->url; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns the tree's blobs. |
107
|
|
|
* |
108
|
|
|
* @return WordPress_GitHub_Sync_Blob[] |
109
|
|
|
*/ |
110
|
12 |
|
public function blobs() { |
111
|
12 |
|
return array_values( $this->paths ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Sets the tree's blobs to the provided array of blobs. |
116
|
|
|
* |
117
|
|
|
* @param WordPress_GitHub_Sync_Blob[] $blobs Array of blobs to set to tree. |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
4 |
|
public function set_blobs( array $blobs ) { |
122
|
4 |
|
$this->paths = array(); |
123
|
4 |
|
$this->shas = array(); |
124
|
|
|
|
125
|
4 |
|
foreach ( $blobs as $blob ) { |
126
|
3 |
|
$this->add_blob( $blob ); |
127
|
4 |
|
} |
128
|
|
|
|
129
|
4 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Adds the provided blob to the tree. |
134
|
|
|
* |
135
|
|
|
* @param WordPress_GitHub_Sync_Blob $blob Blob to add to tree. |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
8 |
|
public function add_blob( WordPress_GitHub_Sync_Blob $blob ) { |
140
|
8 |
|
$this->paths[ $blob->path() ] = $this->shas[ $blob->sha() ] = $blob; |
141
|
|
|
|
142
|
8 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Adds the provided post as a blob to the tree. |
147
|
|
|
* |
148
|
|
|
* @param WordPress_GitHub_Sync_Post $post Post to add to tree. |
149
|
|
|
* |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
4 |
|
public function add_post_to_tree( WordPress_GitHub_Sync_Post $post ) { |
153
|
4 |
|
$blob = $this->get_blob_for_post( $post ); |
154
|
|
|
|
155
|
|
|
if ( |
156
|
4 |
|
! $blob->sha() || |
157
|
3 |
|
$blob->content_import() !== $post->github_content() |
158
|
4 |
|
) { |
159
|
3 |
|
$this->shas[] = $this->paths[ $blob->path() ] = $post->to_blob(); |
160
|
3 |
|
$this->changed = true; |
161
|
|
|
|
162
|
3 |
|
if ( $blob->sha() ) { |
163
|
2 |
|
unset( $this->shas[ $blob->sha() ] ); |
164
|
2 |
|
} |
165
|
3 |
|
} |
166
|
|
|
|
167
|
4 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Removes the provided post's blob from the tree. |
172
|
|
|
* |
173
|
|
|
* @param WordPress_GitHub_Sync_Post $post Post to remove from tree. |
174
|
|
|
* |
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
2 |
|
public function remove_post_from_tree( WordPress_GitHub_Sync_Post $post ) { |
178
|
2 |
|
if ( isset( $this->shas[ $post->sha() ] ) ) { |
179
|
1 |
|
$blob = $this->shas[ $post->sha() ]; |
180
|
|
|
|
181
|
1 |
|
unset( $this->paths[ $blob->path() ] ); |
182
|
1 |
|
unset( $this->shas[ $post->sha() ] ); |
183
|
|
|
|
184
|
1 |
|
$this->changed = true; |
185
|
2 |
|
} else if ( isset( $this->paths[ $post->github_path() ] ) ) { |
186
|
1 |
|
unset( $this->paths[ $post->github_path() ] ); |
187
|
|
|
|
188
|
1 |
|
$this->changed = true; |
189
|
1 |
|
} |
190
|
|
|
|
191
|
2 |
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Retrieves a tree blob for a given path. |
196
|
|
|
* |
197
|
|
|
* @param string $path Path to retrieve blob by. |
198
|
|
|
* |
199
|
|
|
* @return false|WordPress_GitHub_Sync_Blob |
200
|
|
|
*/ |
201
|
3 |
|
public function get_blob_by_path( $path ) { |
202
|
3 |
|
return isset( $this->paths[ $path ] ) ? $this->paths[ $path ] : false; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Retrieves a tree blob for a given path. |
207
|
|
|
* |
208
|
|
|
* @param string $sha Sha to retrieve blob by. |
209
|
|
|
* |
210
|
|
|
* @return false|WordPress_GitHub_Sync_Blob |
211
|
|
|
*/ |
212
|
4 |
|
public function get_blob_by_sha( $sha ) { |
213
|
4 |
|
return isset( $this->shas[ $sha ] ) ? $this->shas[ $sha ] : false; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns whether the tree has changed. |
218
|
|
|
* |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
6 |
|
public function is_changed() { |
222
|
6 |
|
return $this->changed; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Formats the tree for an API call body. |
227
|
|
|
* |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
6 |
|
public function to_body() { |
231
|
6 |
|
$tree = array(); |
232
|
|
|
|
233
|
6 |
|
foreach ( $this->blobs() as $blob ) { |
234
|
4 |
|
$tree[] = $blob->to_body(); |
235
|
6 |
|
} |
236
|
|
|
|
237
|
6 |
|
return array( 'tree' => $tree ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Interprets the Tree from the data. |
242
|
|
|
*/ |
243
|
13 |
|
protected function interpret_data() { |
244
|
13 |
|
$this->sha = isset( $this->data->sha ) ? $this->data->sha : ''; |
245
|
13 |
|
$this->url = isset( $this->data->url ) ? $this->data->url : ''; |
246
|
13 |
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Returns a blob for the provided post. |
250
|
|
|
* |
251
|
|
|
* @param WordPress_GitHub_Sync_Post $post Post to retrieve blob for. |
252
|
|
|
* |
253
|
|
|
* @return WordPress_GitHub_Sync_Blob |
254
|
|
|
*/ |
255
|
4 |
|
protected function get_blob_for_post( WordPress_GitHub_Sync_Post $post ) { |
256
|
4 |
|
if ( $blob = $this->get_blob_by_sha( $post->sha() ) ) { |
257
|
1 |
|
return $blob; |
258
|
|
|
} |
259
|
|
|
|
260
|
3 |
|
if ( $blob = $this->get_blob_by_path( $post->github_path() ) ) { |
261
|
2 |
|
return $blob; |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
return $post->to_blob(); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|