Skip to content

Commit c9c4896

Browse files
committed
feat: add support for composer version checks
1 parent a42cc11 commit c9c4896

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

src/Sprout/Process/Composer.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,24 @@ public function __construct(bool $global = false)
2121
*/
2222
public function json(): array
2323
{
24-
$composerJson = getcwd() . '/composer.json';
24+
return $this->jsonFromFile(
25+
getcwd() . '/composer.json'
26+
);
27+
}
2528

26-
if (!file_exists($composerJson)) {
29+
/**
30+
* Return the composer.json from file path
31+
* @param string $path The path to the composer.json file
32+
* @return array
33+
*/
34+
public function jsonFromFile(string $path): array
35+
{
36+
if (!file_exists($path)) {
2737
return [];
2838
}
2939

3040
return json_decode(
31-
file_get_contents($composerJson),
41+
file_get_contents($path),
3242
true
3343
);
3444
}
@@ -39,16 +49,33 @@ public function json(): array
3949
*/
4050
public function lock(): array
4151
{
42-
$composerLock = getcwd() . '/composer.lock';
52+
return $this->jsonFromFile(
53+
getcwd() . '/composer.lock'
54+
);
55+
}
4356

44-
if (!file_exists($composerLock)) {
45-
return [];
46-
}
57+
/**
58+
* Get version of current composer package (if available)
59+
* @return string|null
60+
*/
61+
public function version(): ?string
62+
{
63+
$json = $this->json();
64+
return $json['version'] ?? null;
65+
}
4766

48-
return json_decode(
49-
file_get_contents($composerLock),
50-
true
67+
/**
68+
* Get latest version of a package from packagist.org
69+
* @param string $package The package to check
70+
* @return string|null
71+
*/
72+
public function latestVersion(string $package): ?string
73+
{
74+
$package = json_decode(
75+
file_get_contents("https://repo.packagist.org/p2/$package.json")
5176
);
77+
78+
return $package->packages->{$package}[0];
5279
}
5380

5481
/**

0 commit comments

Comments
 (0)