|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Composer\Factory; |
|
4
|
|
|
use Composer\IO\NullIO; |
|
5
|
|
|
use Composer\Package\Loader\ArrayLoader; |
|
6
|
|
|
use Composer\Repository\ComposerRepository; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Interacts with Packagist to retrieve package listings and details. |
|
10
|
|
|
*/ |
|
11
|
|
|
class PackagistService |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Composer\Composer |
|
16
|
|
|
*/ |
|
17
|
|
|
private $composer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Composer\Repository\RepositoryInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $repository; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->composer = Factory::create(new NullIO()); |
|
27
|
|
|
$this->client = new Packagist\Api\Client(); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return Composer\Composer |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getComposer() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->composer; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Gets all SilverStripe packages. |
|
40
|
|
|
* |
|
41
|
|
|
* @return Packagist\Api\Package[] |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getPackages() |
|
44
|
|
|
{ |
|
45
|
|
|
$packages = array(); |
|
46
|
|
|
$loader = new ArrayLoader(); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$addonTypes = array( |
|
49
|
|
|
'silverstripe-module', |
|
50
|
|
|
'silverstripe-vendormodule', |
|
51
|
|
|
'silverstripe-theme' |
|
52
|
|
|
); |
|
53
|
|
|
foreach ($addonTypes as $type) { |
|
54
|
|
|
$repositoriesNames = $this->client->all(array('type' => $type)); |
|
55
|
|
|
foreach ($repositoriesNames as $name) { |
|
56
|
|
|
$packages[] = $this->client->get($name); |
|
57
|
|
|
echo $name . PHP_EOL; //output to give feedback when running |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
return $packages; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Gets all SilverStripe packages, grouped by package name. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getGroupedPackages() |
|
69
|
|
|
{ |
|
70
|
|
|
$grouped = array(); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($this->getPackages() as $package) { |
|
73
|
|
|
$name = $package->getName(); |
|
74
|
|
|
|
|
75
|
|
|
if (array_key_exists($name, $grouped)) { |
|
76
|
|
|
$grouped[$name][] = $package; |
|
77
|
|
|
} else { |
|
78
|
|
|
$grouped[$name] = array($package); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $grouped; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Gets detailed information for a package. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getPackageDetails($name) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->client->get($name); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Gets all versions of a package by name. |
|
98
|
|
|
* |
|
99
|
|
|
* @param $name |
|
100
|
|
|
* @return \Composer\Package\PackageInterface[] |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getPackageVersions($name) |
|
103
|
|
|
{ |
|
104
|
|
|
$packages = array(); |
|
105
|
|
|
$loader = new ArrayLoader(); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$package = $this->client->get($name); |
|
108
|
|
|
|
|
109
|
|
|
foreach ($package->getVersions() as $repo) { |
|
110
|
|
|
$packages[] = $repo; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $packages; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: