This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
341f39e
commit bcf39b4
Showing
8 changed files
with
90 additions
and
59 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
72 changes: 72 additions & 0 deletions
72
packages/composer-json-manipulator/src/Sorter/ComposerPackageSorter.php
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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\ComposerJsonManipulator\Sorter; | ||
|
||
use Nette\Utils\Strings; | ||
|
||
/** | ||
* Mostly inspired by https://github.com/composer/composer/blob/master/src/Composer/Json/JsonManipulator.php | ||
*/ | ||
final class ComposerPackageSorter | ||
{ | ||
/** | ||
* @see https://regex101.com/r/tMrjMY/1 | ||
* @var string | ||
*/ | ||
private const PLATFORM_PACKAGE_REGEX = '#^(?:php(?:-64bit|-ipv6|-zts|-debug)?|hhvm|(?:ext|lib)-[a-z0-9](?:[_.-]?[a-z0-9]+)*|composer-(?:plugin|runtime)-api)$#iD'; | ||
|
||
/** | ||
* @see https://regex101.com/r/SXZcfb/1 | ||
* @var string | ||
*/ | ||
private const REQUIREMENT_TYPE_REGEX = '#^(?<name>php|hhvm|ext|lib|\D)#'; | ||
|
||
/** | ||
* Sorts packages by importance (platform packages first, then PHP dependencies) and alphabetically. | ||
* @link https://getcomposer.org/doc/02-libraries.md#platform-packages | ||
* | ||
* @param string[] $packages | ||
* @return string[] | ||
*/ | ||
public function sortPackages(array $packages = []): array | ||
{ | ||
uksort($packages, function (string $firstPackageName, string $secondPackageName): int { | ||
return $this->createRequirement($firstPackageName) <=> $this->createRequirement($secondPackageName); | ||
}); | ||
|
||
return $packages; | ||
} | ||
|
||
private function createRequirement(string $requirement): string | ||
{ | ||
if ($this->isPlatformPackage($requirement)) { | ||
return (string) Strings::replace( | ||
$requirement, | ||
self::REQUIREMENT_TYPE_REGEX, | ||
function (array $match): string { | ||
$name = $match['name']; | ||
if ($name === 'php' || $name === 'hhvm') { | ||
return '0-' . $name; | ||
} | ||
if ($name === 'ext') { | ||
return '1-' . $name; | ||
} | ||
if ($name === 'lib') { | ||
return '2-' . $name; | ||
} | ||
|
||
return '3-' . $name; | ||
} | ||
); | ||
} | ||
|
||
return '4-' . $requirement; | ||
} | ||
|
||
private function isPlatformPackage(string $name): bool | ||
{ | ||
return (bool) Strings::match($name, self::PLATFORM_PACKAGE_REGEX); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -7,5 +7,9 @@ | |
}, | ||
"autoload-dev": { | ||
"classmap": ["tests"] | ||
}, | ||
"require": { | ||
"php": "^7.4", | ||
"symfony/console": "^5.2" | ||
} | ||
} |
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
16 changes: 0 additions & 16 deletions
16
packages/monorepo-builder/src/Composer/ComposerFactory.php
This file was deleted.
Oops, something went wrong.