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