-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1001 from fredden/sort/repository-filtering
Sort repository filtering lists (only, exclude)
- Loading branch information
Showing
36 changed files
with
811 additions
and
66 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018-2023 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-normalizer | ||
*/ | ||
|
||
namespace Ergebnis\Json\Normalizer\Vendor\Composer; | ||
|
||
use Ergebnis\Json\Json; | ||
use Ergebnis\Json\Normalizer\Format; | ||
use Ergebnis\Json\Normalizer\Normalizer; | ||
|
||
final class RepositoriesHashNormalizer implements Normalizer | ||
{ | ||
private const PROPERTIES_WITH_WILDCARDS = [ | ||
/** | ||
* @see https://getcomposer.org/doc/articles/repository-priorities.md#filtering-packages | ||
*/ | ||
'exclude', | ||
'only', | ||
]; | ||
private readonly WildcardSorter $wildcardSorter; | ||
|
||
public function __construct() | ||
{ | ||
$this->wildcardSorter = new WildcardSorter(); | ||
} | ||
|
||
public function normalize(Json $json): Json | ||
{ | ||
$decoded = $json->decoded(); | ||
|
||
if (!\is_object($decoded)) { | ||
return $json; | ||
} | ||
|
||
if (!\property_exists($decoded, 'repositories')) { | ||
return $json; | ||
} | ||
|
||
if (!\is_object($decoded->repositories) && !\is_array($decoded->repositories)) { | ||
return $json; | ||
} | ||
|
||
/** @var array<string, mixed> $repositories */ | ||
$repositories = (array) $decoded->repositories; | ||
|
||
if ([] === $repositories) { | ||
return $json; | ||
} | ||
|
||
foreach ($repositories as &$repository) { | ||
$repository = (array) $repository; | ||
|
||
foreach (self::PROPERTIES_WITH_WILDCARDS as $property) { | ||
$this->wildcardSorter->sortPropertyWithWildcard( | ||
$repository, | ||
$property, | ||
false, | ||
); | ||
} | ||
} | ||
|
||
$decoded->repositories = $repositories; | ||
|
||
/** @var string $encoded */ | ||
$encoded = \json_encode( | ||
$decoded, | ||
Format\JsonEncodeOptions::default()->toInt(), | ||
); | ||
|
||
return Json::fromString($encoded); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018-2023 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-normalizer | ||
*/ | ||
|
||
namespace Ergebnis\Json\Normalizer\Vendor\Composer; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class WildcardSorter | ||
{ | ||
/** | ||
* When sorting with wildcards, special care needs to be taken. | ||
* | ||
* @see https://github.com/ergebnis/json-normalizer/pull/775#issuecomment-1346095415 | ||
* @see https://github.com/composer/composer/blob/2.6.5/src/Composer/Plugin/PluginManager.php#L85-L86 | ||
* @see https://github.com/composer/composer/blob/2.6.5/src/Composer/Plugin/PluginManager.php#L626-L646 | ||
* @see https://github.com/composer/composer/blob/2.6.5/src/Composer/Package/BasePackage.php#L252-L257 | ||
* @see https://github.com/composer/composer/blob/2.6.5/src/Composer/Plugin/PluginManager.php#L687-L691 | ||
*/ | ||
public function sortPropertyWithWildcard( | ||
array &$config, | ||
string $property, | ||
bool $sortByKey = true, | ||
): void { | ||
if (!\array_key_exists($property, $config)) { | ||
return; | ||
} | ||
|
||
if (!\is_object($config[$property]) && !\is_array($config[$property])) { | ||
return; | ||
} | ||
|
||
$value = (array) $config[$property]; | ||
|
||
if ([] === $value) { | ||
return; | ||
} | ||
|
||
$packages = $sortByKey ? \array_keys($value) : \array_values($value); | ||
|
||
foreach ($packages as $package) { | ||
/** @var string $package */ | ||
if (\str_contains(\rtrim($package, '*'), '*')) { | ||
// We cannot reliably sort allow-plugins when there's a wildcard other than at the end of the string. | ||
return; | ||
} | ||
} | ||
|
||
$normalize = static function (string $package): string { | ||
// Any key with an asterisk needs to be the last entry in its group | ||
return \str_replace( | ||
'*', | ||
'~', | ||
$package, | ||
); | ||
}; | ||
|
||
$callback = static function (string $a, string $b) use ($normalize): int { | ||
return \strcmp( | ||
$normalize($a), | ||
$normalize($b), | ||
); | ||
}; | ||
|
||
if ($sortByKey) { | ||
/** @var array<string, mixed> $value */ | ||
\uksort($value, $callback); | ||
} else { | ||
/** @var array<mixed, string> $value */ | ||
\usort($value, $callback); | ||
} | ||
|
||
$config[$property] = $value; | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
...ories/HasEntries/Yes/IsArray/UsesFiltering/Yes/WithExclude/HasWildcard/No/normalized.json
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "ergebnis/json-normalizer", | ||
"description": "Provides generic and vendor-specific normalizers for normalizing JSON documents.", | ||
"license": "MIT", | ||
"type": "library", | ||
"keywords": [ | ||
"json", | ||
"normalizer" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Andreas Möller", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"homepage": "https://getcomposer.org/doc/04-schema.md#repositories", | ||
"repositories": [ | ||
{ | ||
"type": "composer", | ||
"url": "http://packages.foo.com", | ||
"exclude": [ | ||
"vendor/one", | ||
"vendor/two" | ||
] | ||
} | ||
] | ||
} |
24 changes: 24 additions & 0 deletions
24
...itories/HasEntries/Yes/IsArray/UsesFiltering/Yes/WithExclude/HasWildcard/No/original.json
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "ergebnis/json-normalizer", | ||
"description": "Provides generic and vendor-specific normalizers for normalizing JSON documents.", | ||
"license": "MIT", | ||
"type": "library", | ||
"keywords": [ | ||
"json", | ||
"normalizer" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Andreas Möller", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"homepage": "https://getcomposer.org/doc/04-schema.md#repositories", | ||
"repositories": [ | ||
{ | ||
"url": "http://packages.foo.com", | ||
"exclude": ["vendor/two","vendor/one"], | ||
"type": "composer" | ||
} | ||
] | ||
} |
Oops, something went wrong.