|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stats\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Joomla\Controller\AbstractController; |
|
6
|
|
|
use Stats\CommandInterface; |
|
7
|
|
|
use Stats\GitHub\GitHub; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstract command for processing tags from GitHub |
|
11
|
|
|
* |
|
12
|
|
|
* @method \Stats\CliApplication getApplication() Get the application object. |
|
13
|
|
|
* @property-read \Stats\CliApplication $app Application object |
|
14
|
|
|
* |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractTagCommand extends AbstractController implements CommandInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* GitHub API object. |
|
21
|
|
|
* |
|
22
|
|
|
* @var GitHub |
|
23
|
|
|
* @since 1.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $github; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The GitHub repository to query. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
* @since 1.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $repoName; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The owner of the GitHub repository to query. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
* @since 1.0 |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $repoOwner; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param GitHub $github GitHub API object |
|
47
|
|
|
* |
|
48
|
|
|
* @since 1.0 |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(GitHub $github) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->github = $github; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the tags for a repository |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
* |
|
60
|
|
|
* @since 1.0 |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getTags() |
|
63
|
|
|
{ |
|
64
|
|
|
$tags = []; |
|
65
|
|
|
|
|
66
|
|
|
$this->getApplication()->out('<info>Fetching page 1 of tags.</info>'); |
|
67
|
|
|
|
|
68
|
|
|
// Get the first page so we can process the headers to figure out how many times we need to do this |
|
69
|
|
|
$tags = array_merge($tags, $this->github->repositories->getTags($this->repoOwner, $this->repoName, 1)); |
|
70
|
|
|
|
|
71
|
|
|
$response = $this->github->repositories->getApiResponse(); |
|
72
|
|
|
|
|
73
|
|
|
if (isset($response->headers['Link'])) |
|
74
|
|
|
{ |
|
75
|
|
|
preg_match('/(\?page=[0-9]+>; rel=\"last\")/', $response->headers['Link'], $matches); |
|
76
|
|
|
|
|
77
|
|
|
if ($matches && isset($matches[0])) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
preg_match('/\d+/', $matches[0], $pages); |
|
80
|
|
|
|
|
81
|
|
|
$lastPage = $pages[0]; |
|
82
|
|
|
|
|
83
|
|
|
for ($page = 2; $page <= $lastPage; $page++) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->getApplication()->out("<info>Fetching page $page of $lastPage pages of tags.</info>"); |
|
86
|
|
|
|
|
87
|
|
|
$tags = array_merge($tags, $this->github->repositories->getTags($this->repoOwner, $this->repoName, $page)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $tags; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.