|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Fetch API client class. |
|
4
|
|
|
* @package WordPress_GitHub_Sync |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class WordPress_GitHub_Sync_Fetch_Client |
|
9
|
|
|
*/ |
|
10
|
|
|
class WordPress_GitHub_Sync_Fetch_Client extends WordPress_GitHub_Sync_Base_Client { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Retrieve the last commit in the repository |
|
14
|
|
|
* |
|
15
|
|
|
* @return WordPress_GitHub_Sync_Commit|WP_Error |
|
16
|
|
|
*/ |
|
17
|
11 |
|
public function master() { |
|
18
|
11 |
|
$data = $this->call( 'GET', $this->reference_endpoint() ); |
|
19
|
|
|
|
|
20
|
11 |
|
if ( is_wp_error( $data ) ) { |
|
21
|
4 |
|
return $data; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
7 |
|
return $this->commit( $data->object->sha ); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Retrieves a commit by sha from the GitHub API |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $sha Sha for commit to retrieve. |
|
31
|
|
|
* |
|
32
|
|
|
* @return WordPress_GitHub_Sync_Commit|WP_Error |
|
33
|
|
|
*/ |
|
34
|
7 |
|
public function commit( $sha ) { |
|
35
|
7 |
|
if ( $cache = $this->app->cache()->fetch_commit( $sha ) ) { |
|
36
|
1 |
|
return $cache; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
6 |
|
$data = $this->call( 'GET', $this->commit_endpoint() . '/' . $sha ); |
|
40
|
|
|
|
|
41
|
6 |
|
if ( is_wp_error( $data ) ) { |
|
42
|
1 |
|
return $data; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
$commit = new WordPress_GitHub_Sync_Commit( $data ); |
|
46
|
5 |
|
$tree = $this->tree_recursive( $commit->tree_sha() ); |
|
47
|
|
|
|
|
48
|
5 |
|
if ( is_wp_error( $tree ) ) { |
|
49
|
1 |
|
return $tree; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
4 |
|
$commit->set_tree( $tree ); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
4 |
|
return $this->app->cache()->set_commit( $sha, $commit ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Calls the content API to get the post's contents and metadata |
|
59
|
|
|
* |
|
60
|
|
|
* Returns Object the response from the API |
|
61
|
|
|
* |
|
62
|
|
|
* @param WordPress_GitHub_Sync_Post $post Post to retrieve remote contents for. |
|
63
|
|
|
* |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
public function remote_contents( $post ) { |
|
67
|
|
|
return $this->call( 'GET', $this->content_endpoint() . $post->github_path() ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Retrieves a tree by sha recursively from the GitHub API |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $sha Commit sha to retrieve tree from. |
|
74
|
|
|
* |
|
75
|
|
|
* @return WordPress_GitHub_Sync_Tree|WP_Error |
|
76
|
|
|
*/ |
|
77
|
5 |
|
protected function tree_recursive( $sha ) { |
|
78
|
5 |
|
if ( $cache = $this->app->cache()->fetch_tree( $sha ) ) { |
|
79
|
1 |
|
return $cache; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
$data = $this->call( 'GET', $this->tree_endpoint() . '/' . $sha . '?recursive=1' ); |
|
83
|
|
|
|
|
84
|
4 |
|
if ( is_wp_error( $data ) ) { |
|
85
|
1 |
|
return $data; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
foreach ( $data->tree as $index => $thing ) { |
|
89
|
|
|
// We need to remove the trees because |
|
90
|
|
|
// the recursive tree includes both |
|
91
|
|
|
// the subtrees as well the subtrees' blobs. |
|
92
|
3 |
|
if ( 'tree' === $thing->type ) { |
|
93
|
3 |
|
unset( $data->tree[ $index ] ); |
|
94
|
3 |
|
} |
|
95
|
3 |
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
$tree = new WordPress_GitHub_Sync_Tree( $data ); |
|
98
|
3 |
|
$tree->set_blobs( $this->blobs( $data->tree ) ); |
|
99
|
|
|
|
|
100
|
3 |
|
return $this->app->cache()->set_tree( $sha, $tree ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Generates blobs for recursive tree blob data. |
|
105
|
|
|
* |
|
106
|
|
|
* @param stdClass[] $blobs Array of tree blob data. |
|
107
|
|
|
* |
|
108
|
|
|
* @return WordPress_GitHub_Sync_Blob[] |
|
109
|
|
|
*/ |
|
110
|
3 |
|
protected function blobs( array $blobs ) { |
|
111
|
3 |
|
$results = array(); |
|
112
|
|
|
|
|
113
|
3 |
|
foreach ( $blobs as $blob ) { |
|
114
|
3 |
|
$obj = $this->blob( $blob ); |
|
115
|
|
|
|
|
116
|
3 |
|
if ( ! is_wp_error( $obj ) ) { |
|
117
|
2 |
|
$results[] = $obj; |
|
118
|
2 |
|
} |
|
119
|
3 |
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
return $results; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Retrieves the blob data for a given sha |
|
126
|
|
|
* |
|
127
|
|
|
* @param stdClass $blob Tree blob data. |
|
128
|
|
|
* |
|
129
|
|
|
* @return WordPress_GitHub_Sync_Blob|WP_Error |
|
130
|
|
|
*/ |
|
131
|
3 |
|
protected function blob( $blob ) { |
|
132
|
3 |
|
if ( $cache = $this->app->cache()->fetch_blob( $blob->sha ) ) { |
|
133
|
1 |
|
return $cache; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
2 |
|
$data = $this->call( 'GET', $this->blob_endpoint() . '/' . $blob->sha ); |
|
137
|
|
|
|
|
138
|
2 |
|
if ( is_wp_error( $data ) ) { |
|
139
|
1 |
|
return $data; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
$data->path = $blob->path; |
|
143
|
1 |
|
$obj = new WordPress_GitHub_Sync_Blob( $data ); |
|
144
|
|
|
|
|
145
|
1 |
|
return $this->app->cache()->set_blob( $obj->sha(), $obj ); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.