1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Helpers; |
4
|
|
|
|
5
|
|
|
class GistFinder |
6
|
|
|
{ |
7
|
|
|
/** @var \Github\Api\ApiInterface $gistsApi */ |
8
|
|
|
private $gistsApi; |
9
|
|
|
|
10
|
|
|
/** @var \Github\ResultPager $paginator */ |
11
|
|
|
private $paginator; |
12
|
|
|
|
13
|
|
|
/** @var \GuzzleHttp\Client $guzzle */ |
14
|
|
|
private $guzzle; |
15
|
|
|
|
16
|
|
|
public function __construct( |
17
|
|
|
\Github\Client $githubClient, |
18
|
|
|
\Github\ResultPager $paginator, |
19
|
|
|
\GuzzleHttp\Client $guzzleClient |
20
|
|
|
) { |
21
|
|
|
$githubClient->authenticate(\Auth::user()->token, null, \Github\Client::AUTH_HTTP_TOKEN); |
22
|
|
|
|
23
|
|
|
$this->gistsApi = $githubClient->api('gists'); |
24
|
|
|
$this->paginator = $paginator; |
25
|
|
|
$this->guzzle = $guzzleClient; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get gists and tags. |
30
|
|
|
* |
31
|
|
|
* @return array `['gists' => [...], 'tags' => [...]]` |
32
|
|
|
*/ |
33
|
|
|
public function getGistsAndTags() |
34
|
|
|
{ |
35
|
|
|
$rawMergedGists = $this->fetchRawGists(); |
36
|
|
|
|
37
|
|
|
// prepare gists and tags arrays |
38
|
|
|
$gists = []; |
39
|
|
|
$tags = []; |
40
|
|
|
foreach ($rawMergedGists as $gist) { |
41
|
|
|
// separate description and tags |
42
|
|
|
$descAndTags = $this->separateDescriptionAndTags($gist['description']); |
43
|
|
|
|
44
|
|
|
// set gist owner |
45
|
|
|
$gistOwner = isset($gist['owner']) ? $gist['owner']['login'] : 'anonymous'; |
46
|
|
|
|
47
|
|
|
// set gist type |
48
|
|
|
$gistType = []; |
49
|
|
|
if (isset($gist['starred'])) { |
50
|
|
|
$gistType[] = '@starred'; |
51
|
|
|
} |
52
|
|
|
if (\Auth::user()->nickname == $gistOwner) { |
53
|
|
|
$gistType[] = '@owned'; |
54
|
|
|
} |
55
|
|
|
if ($gist['public']) { |
56
|
|
|
$gistType[] = '@public'; |
57
|
|
|
} else { |
58
|
|
|
$gistType[] = '@private'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// prepare files array |
62
|
|
|
$files = []; |
63
|
|
|
foreach ($gist['files'] as $file) { |
64
|
|
|
$files[] = [ |
65
|
|
|
'filename' => $file['filename'], |
66
|
|
|
'raw_url' => $file['raw_url'] |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// add gist to gists array |
71
|
|
|
$gists[] = [ |
72
|
|
|
'id' => $gist['id'], |
73
|
|
|
'description' => $descAndTags['description'] ? $descAndTags['description'] : 'No description', |
74
|
|
|
'tags' => $descAndTags['tags'], |
75
|
|
|
'owner' => $gistOwner, |
76
|
|
|
'created' => date('Y-m-d H:i:s', strtotime($gist['created_at'])), |
77
|
|
|
'updated' => date('Y-m-d H:i:s', strtotime($gist['updated_at'])), |
78
|
|
|
'type' => $gistType, |
79
|
|
|
'html_url' => $gist['html_url'], |
80
|
|
|
'files' => $files |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
// add tags to tags array |
84
|
|
|
foreach ($descAndTags['tags'] as $tag) { |
85
|
|
|
if (!in_array($tag, $tags)) { |
86
|
|
|
$tags[] = $tag; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return compact('gists', 'tags'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Fetch raw gists from GitHub. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function fetchRawGists() |
100
|
|
|
{ |
101
|
|
|
// fetch user and starred gists |
102
|
|
|
$rawUserGists = collect($this->paginator->fetchAll($this->gistsApi, 'all')) |
103
|
|
|
->keyBy('id') |
104
|
|
|
->toArray(); |
105
|
|
|
$rawStarredGists = collect($this->paginator->fetchAll($this->gistsApi, 'all', ['starred'])) |
106
|
|
|
->keyBy('id') |
107
|
|
|
->toArray(); |
108
|
|
|
|
109
|
|
|
// add starred label to starred gists |
110
|
|
|
foreach ($rawStarredGists as $key => $gist) { |
111
|
|
|
$rawStarredGists[$key]['starred'] = true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// merge user and starred gists |
115
|
|
|
$rawMergedGists = $rawStarredGists + $rawUserGists; |
116
|
|
|
|
117
|
|
|
return $rawMergedGists; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the content of a Gist file. |
122
|
|
|
* |
123
|
|
|
* @param string $url |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getGistFileContents($url) |
128
|
|
|
{ |
129
|
|
|
return (string)$this->guzzle->get($url)->getBody(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Separate gist description and tags. |
134
|
|
|
* |
135
|
|
|
* Seeks tags in original gist description and saves them and description separately. |
136
|
|
|
* |
137
|
|
|
* @param string $descriptionWithTags Gist description with optional tags. |
138
|
|
|
* |
139
|
|
|
* @return array `['description' => '...', 'tags' => [...]]` |
140
|
|
|
*/ |
141
|
|
|
private function separateDescriptionAndTags($descriptionWithTags) |
142
|
|
|
{ |
143
|
|
|
$tags = []; |
144
|
|
|
$description = trim( |
145
|
|
|
preg_replace_callback( |
146
|
|
|
'~(?:^|\s)(#\S+)~', |
147
|
|
|
function ($matches) use (&$tags) { |
148
|
|
|
$tags[] = $matches[1]; |
149
|
|
|
}, |
150
|
|
|
$descriptionWithTags |
151
|
|
|
) |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
if (empty($tags)) { |
155
|
|
|
$tags[] = '#none'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return compact('description', 'tags'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|