-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update storage signature, add storage of package metadata
- Loading branch information
1 parent
7838979
commit 62c32fc
Showing
6 changed files
with
158 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
use Winter\Packager\Package\Package; | ||
use Winter\Packager\Package\Packagist; | ||
use Winter\Packager\Package\VersionedPackage; | ||
use Winter\Packager\Storage\Storage; | ||
|
||
/** | ||
* Represents a Composer instance. | ||
|
@@ -467,8 +468,18 @@ public static function newConstraint(mixed ...$arguments): Constraint | |
* | ||
* `Name or Reference <[email protected]>` | ||
*/ | ||
public static function setAgent(string $agent): void | ||
public function setAgent(string $agent): static | ||
{ | ||
Packagist::setAgent($agent); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the metadata storage for Packagist requests. | ||
*/ | ||
public function setStorage(Storage $storage): static | ||
{ | ||
Packagist::setStorage($storage); | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Winter\Packager\Exceptions\PackagistException; | ||
use Winter\Packager\Storage\Storage; | ||
|
||
/** | ||
* Packagist class. | ||
|
@@ -27,13 +28,22 @@ class Packagist | |
|
||
protected static string $agent = 'Winter Packager <[email protected]>'; | ||
|
||
protected static ?Storage $storage = null; | ||
|
||
/** | ||
* Get information on a package in the Packagist API. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public static function getPackage(string $namespace, string $name, ?string $version = null): array | ||
{ | ||
public static function getPackage( | ||
string $namespace, | ||
string $name, | ||
?string $version = null | ||
): array { | ||
if (!is_null(static::$storage) && static::$storage->has($namespace, $name, $version)) { | ||
return static::$storage->get($namespace, $name, $version); | ||
} | ||
|
||
$client = static::getClient(); | ||
$request = static::newRepoRequest($namespace . '/' . $name . '.json'); | ||
|
||
|
@@ -49,29 +59,32 @@ public static function getPackage(string $namespace, string $name, ?string $vers | |
|
||
$body = json_decode($response->getBody()->getContents(), true); | ||
|
||
if (is_null($version)) { | ||
if (!isset($body['packages'][$namespace . '/' . $name][0])) { | ||
throw new PackagistException('Package information not found'); | ||
} | ||
} else { | ||
if (!isset($body['packages'][$namespace . '/' . $name])) { | ||
throw new PackagistException('Package information not found'); | ||
} | ||
if (!isset($body['packages'][$namespace . '/' . $name])) { | ||
throw new PackagistException('Package information not found'); | ||
} | ||
|
||
$versions = MetadataMinifier::expand($body['packages'][$namespace . '/' . $name]); | ||
$parser = new VersionParser; | ||
$packageVersionNormalized = $parser->normalize($version); | ||
$versions = []; | ||
foreach (MetadataMinifier::expand($body['packages'][$namespace . '/' . $name]) as $packageVersion) { | ||
$versions[$packageVersion['version_normalized']] = $packageVersion; | ||
|
||
foreach ($versions as $packageVersion) { | ||
if ($packageVersion['version_normalized'] === $packageVersionNormalized) { | ||
return $packageVersion; | ||
} | ||
// Store metadata | ||
if (!is_null(static::$storage)) { | ||
static::$storage->set($namespace, $name, $packageVersion['version_normalized'], $packageVersion); | ||
} | ||
} | ||
|
||
if (is_null($version)) { | ||
return reset($versions); | ||
} | ||
|
||
$parser = new VersionParser; | ||
$versionNormalized = $parser->normalize($version); | ||
|
||
if (!array_key_exists($versionNormalized, $versions)) { | ||
throw new PackagistException('Package version not found'); | ||
} | ||
|
||
return $body['packages'][$namespace . '/' . $name][0]; | ||
return $versions[$versionNormalized]; | ||
} | ||
|
||
public static function getClient(): ClientInterface | ||
|
@@ -104,6 +117,14 @@ public static function setAgent(string $agent): void | |
static::$agent = trim($name) . ' <' . trim($email) . '>'; | ||
} | ||
|
||
/** | ||
* Sets the storage for metadata. | ||
*/ | ||
public static function setStorage(?Storage $storage = null): void | ||
{ | ||
static::$storage = $storage; | ||
} | ||
|
||
public static function newApiRequest(string $url = ''): RequestInterface | ||
{ | ||
$request = Psr17FactoryDiscovery::findRequestFactory()->createRequest('GET', self::PACKAGIST_API_URL . ltrim($url, '/')); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.