Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
remove dependency on composer
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 24, 2020
1 parent 341f39e commit bcf39b4
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 59 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"license": "MIT",
"require": {
"php": ">=7.2",
"composer/composer": "^2.0",
"composer/xdebug-handler": "^1.4",
"erusev/parsedown": "^1.7",
"erusev/parsedown-extra": "^0.7.1",
Expand Down
1 change: 0 additions & 1 deletion packages/composer-json-manipulator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"type": "symfony-bundle",
"require": {
"php": ">=7.2",
"composer/composer": "^2.0",
"nette/utils": "^3.0",
"symfony/config": "^4.4|^5.1",
"symfony/dependency-injection": "^4.4|^5.1",
Expand Down
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);
}
}
30 changes: 13 additions & 17 deletions packages/composer-json-manipulator/src/ValueObject/ComposerJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace Symplify\ComposerJsonManipulator\ValueObject;

use Composer\Json\JsonManipulator;
use Nette\Utils\Arrays;
use Symplify\PackageBuilder\Reflection\PrivatesCaller;
use Symplify\ComposerJsonManipulator\Sorter\ComposerPackageSorter;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SymplifyKernel\Exception\ShouldNotHappenException;

Expand Down Expand Up @@ -97,6 +96,16 @@ final class ComposerJson
*/
private $fileInfo;

/**
* @var ComposerPackageSorter
*/
private $composerPackageSorter;

public function __construct()
{
$this->composerPackageSorter = new ComposerPackageSorter();
}

public function setOriginalFileInfo(SmartFileInfo $fileInfo): void
{
$this->fileInfo = $fileInfo;
Expand All @@ -112,9 +121,7 @@ public function setName(string $name): void
*/
public function setRequire(array $require): void
{
$require = $this->sortPackages($require);

$this->require = $require;
$this->require = $this->composerPackageSorter->sortPackages($require);
}

/**
Expand All @@ -135,7 +142,7 @@ public function getRequireDev(): array

public function setRequireDev(array $requireDev): void
{
$this->requireDev = $this->sortPackages($requireDev);
$this->requireDev = $this->composerPackageSorter->sortPackages($requireDev);
}

/**
Expand Down Expand Up @@ -505,17 +512,6 @@ private function getPsr4AndClassmapDevDirectories(): array
return array_merge($psr4Directories, $classmapDirectories);
}

/**
* @param string[] $packages
* @return string[]
*/
private function sortPackages(array $packages): array
{
$privatesCaller = new PrivatesCaller();

return $privatesCaller->callPrivateMethodWithReference(JsonManipulator::class, 'sortPackages', $packages);
}

private function moveValueToBack(string $valueName): void
{
$key = array_search($valueName, $this->orderedKeys, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
},
"autoload-dev": {
"classmap": ["tests"]
},
"require": {
"php": "^7.4",
"symfony/console": "^5.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

namespace Symplify\ComposerJsonManipulator\Tests\ComposerJsonSchemaValidation;

use Composer\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symplify\ComposerJsonManipulator\FileSystem\JsonFileManager;
use Symplify\ComposerJsonManipulator\Tests\HttpKernel\ComposerJsonManipulatorKernel;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileSystem;

class ComposerJsonSchemaValidationTest extends AbstractKernelTestCase
final class ComposerJsonSchemaValidationTest extends AbstractKernelTestCase
{
/**
* @var JsonFileManager
Expand Down Expand Up @@ -49,25 +47,5 @@ public function testCheckEmptyKeysAreRemoved(): void
$this->assertArrayHasKey('auto-scripts', $sourceJson['scripts']);
$this->assertArrayNotHasKey('require-dev', $targetJson);
$this->assertArrayNotHasKey('auto-scripts', $targetJson['scripts']);

/*
* Validate composer.json schema using `composer validate`
*/
$arrayInput = new ArrayInput([
'command' => 'validate',
'file' => $targetJsonPath,
// https://getcomposer.org/doc/03-cli.md#validate
'--no-check-publish' => true,
'--no-interaction' => true,
'--quiet' => true,
]);
$application = new Application();
// prevent `$application->run` method from exiting the script
$application->setAutoExit(false);
$this->assertSame(
0,
$application->run($arrayInput),
'Dumped composer.json did not pass validation ("composer validate --no-check-publish" exited with non-zero status)'
);
}
}
1 change: 0 additions & 1 deletion packages/monorepo-builder/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
],
"require": {
"php": ">=7.2",
"composer/composer": "^2.0",
"nette/utils": "^3.0",
"jean85/pretty-package-versions": "^1.2",
"phar-io/version": "^2.0.1|^3.0",
Expand Down
16 changes: 0 additions & 16 deletions packages/monorepo-builder/src/Composer/ComposerFactory.php

This file was deleted.

0 comments on commit bcf39b4

Please sign in to comment.