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

Commit

Permalink
decouple ComposerJsonUpdater
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 25, 2020
1 parent 0ab072d commit 3ca9d36
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\KernelInterface;

abstract class AbstractKernelTestCase
abstract class AbstractAnyTestCase
{
/**
* @var KernelInterface
Expand All @@ -19,4 +19,4 @@ abstract class AbstractKernelTestCase
* @var ContainerInterface|Container
*/
protected static $container;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function provideData(): Iterator
yield [__DIR__ . '/Fixture/SkipAbstractClassWithConstructorSetValues.php', []];
yield [__DIR__ . '/Fixture/AbstractClassWithAutowireInjection.php', []];
yield [__DIR__ . '/Fixture/AbstractClassWithTestCaseSetUp.php', []];
yield [__DIR__ . '/Fixture/AbstractKernelTestCase.php', []];
yield [__DIR__ . '/Fixture/AbstractAnyTestCase.php', []];
yield [__DIR__ . '/Fixture/HasProtectedPropertyAndConstant.php',
[
[ForbiddenProtectedPropertyRule::ERROR_MESSAGE, 11],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@ final class ComposerPackageSorter
* 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[]
* @param array<string, string> $packages
* @return array<string, string>
*/
public function sortPackages(array $packages = []): array
{
uksort($packages, function (string $firstPackageName, string $secondPackageName): int {
return $this->createRequirement($firstPackageName) <=> $this->createRequirement($secondPackageName);
return $this->createNameWithPriority($firstPackageName) <=> $this->createNameWithPriority(
$secondPackageName
);
});

return $packages;
}

private function createRequirement(string $requirement): string
private function createNameWithPriority(string $requirementName): string
{
if ($this->isPlatformPackage($requirement)) {
if ($this->isPlatformPackage($requirementName)) {
return (string) Strings::replace(
$requirement,
$requirementName,
self::REQUIREMENT_TYPE_REGEX,
function (array $match): string {
$name = $match['name'];
Expand All @@ -62,7 +64,7 @@ function (array $match): string {
);
}

return '4-' . $requirement;
return '4-' . $requirementName;
}

private function isPlatformPackage(string $name): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Symplify\ComposerJsonManipulator\Tests\Sorter;

use Iterator;
use Symplify\ComposerJsonManipulator\Sorter\ComposerPackageSorter;
use Symplify\ComposerJsonManipulator\Tests\HttpKernel\ComposerJsonManipulatorKernel;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;

final class ComposerPackageSorterTest extends AbstractKernelTestCase
{
/**
* @var ComposerPackageSorter
*/
private $composerPackageSorter;

protected function setUp(): void
{
$this->bootKernel(ComposerJsonManipulatorKernel::class);

$this->composerPackageSorter = self::$container->get(ComposerPackageSorter::class);
}

/**
* @dataProvider provideData()
*/
public function test(array $packages, array $expectedSortedPackages): void
{
$sortedPackages = $this->composerPackageSorter->sortPackages($packages);
$this->assertSame($expectedSortedPackages, $sortedPackages);
}

public function provideData(): Iterator
{
yield [
[
'symfony/console' => '^5.2',
'php' => '^8.0',
'ext-json' => '*',
],
[
'php' => '^8.0',
'ext-json' => '*',
'symfony/console' => '^5.2',
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\ComposerJsonManipulator\FileSystem\JsonFileManager;
use Symplify\MonorepoBuilder\FileSystem\ComposerJsonProvider;
use Symplify\MonorepoBuilder\Package\PackageNamesProvider;
use Symplify\MonorepoBuilder\Testing\ComposerJson\ComposerJsonSymlinker;
use Symplify\MonorepoBuilder\Testing\ComposerJson\ComposerVersionManipulator;
use Symplify\MonorepoBuilder\Testing\PackageDependency\UsedPackagesResolver;
use Symplify\MonorepoBuilder\Testing\ComposerJsonUpdater;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\SmartFileSystem\SmartFileInfo;

final class LocalizeComposerPathsCommand extends Command
{
Expand All @@ -25,54 +20,27 @@ final class LocalizeComposerPathsCommand extends Command
*/
private $composerJsonProvider;

/**
* @var PackageNamesProvider
*/
private $packageNamesProvider;

/**
* @var JsonFileManager
*/
private $jsonFileManager;

/**
* @var SymfonyStyle
*/
private $symfonyStyle;

/**
* @var ComposerJsonSymlinker
* @var ComposerJsonUpdater
*/
private $composerJsonSymlinker;

/**
* @var ComposerVersionManipulator
*/
private $composerVersionManipulator;

/**
* @var UsedPackagesResolver
*/
private $usedPackagesResolver;
private $composerJsonUpdater;

public function __construct(
ComposerJsonProvider $composerJsonProvider,
PackageNamesProvider $packageNamesProvider,
JsonFileManager $jsonFileManager,
SymfonyStyle $symfonyStyle,
ComposerJsonSymlinker $composerJsonSymlinker,
ComposerVersionManipulator $composerVersionManipulator,
UsedPackagesResolver $usedPackagesResolver
ComposerJsonUpdater $composerJsonUpdater
) {
$this->composerJsonProvider = $composerJsonProvider;
$this->packageNamesProvider = $packageNamesProvider;
$this->jsonFileManager = $jsonFileManager;
$this->symfonyStyle = $symfonyStyle;
$this->composerJsonSymlinker = $composerJsonSymlinker;
$this->composerVersionManipulator = $composerVersionManipulator;
$this->usedPackagesResolver = $usedPackagesResolver;

parent::__construct();

$this->composerJsonUpdater = $composerJsonUpdater;
}

protected function configure(): void
Expand All @@ -87,36 +55,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$packagesFileInfos = $this->composerJsonProvider->getPackagesComposerFileInfos();
foreach ($packagesFileInfos as $packagesFileInfo) {
$this->processPackage($packagesFileInfo, $rootFileInfo);
$this->composerJsonUpdater->processPackage($packagesFileInfo, $rootFileInfo);
}

$this->symfonyStyle->success('Package paths have been updated');

return ShellCode::SUCCESS;
}

private function processPackage(SmartFileInfo $packageFileInfo, SmartFileInfo $mainComposerJsonFileInfo): void
{
$packageComposerJson = $this->jsonFileManager->loadFromFileInfo($packageFileInfo);

$usedPackageNames = $this->usedPackagesResolver->resolveForPackage($packageComposerJson);
$packageComposerJson = $this->composerVersionManipulator->setAsteriskVersionForUsedPackages(
$packageComposerJson,
$usedPackageNames
);

// possibly replace them all to cover recursive secondary dependencies
$packageNames = $this->packageNamesProvider->provide();

$packageComposerJson = $this->composerJsonSymlinker->decoratePackageComposerJsonWithPackageSymlinks(
$packageComposerJson,
$packageNames,
$mainComposerJsonFileInfo
);

$message = sprintf('File "%s" was updated', $packageFileInfo->getRelativeFilePathFromCwd());
$this->symfonyStyle->note($message);

$this->jsonFileManager->saveJsonWithFileInfo($packageComposerJson, $packageFileInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public function decoratePackageComposerJsonWithPackageSymlinks(
'options' => [
'symlink' => false,
],
// since composer 2.0 - see https://getcomposer.org/doc/articles/repository-priorities.md#default-behavior
'canonical' => false,
];

if (array_key_exists('repositories', $packageComposerJson)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Symplify\MonorepoBuilder\Testing;

use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\ComposerJsonManipulator\FileSystem\JsonFileManager;
use Symplify\MonorepoBuilder\Package\PackageNamesProvider;
use Symplify\MonorepoBuilder\Testing\ComposerJson\ComposerJsonSymlinker;
use Symplify\MonorepoBuilder\Testing\ComposerJson\ComposerVersionManipulator;
use Symplify\MonorepoBuilder\Testing\PackageDependency\UsedPackagesResolver;
use Symplify\SmartFileSystem\SmartFileInfo;

final class ComposerJsonUpdater
{
/**
* @var PackageNamesProvider
*/
private $packageNamesProvider;

/**
* @var JsonFileManager
*/
private $jsonFileManager;

/**
* @var SymfonyStyle
*/
private $symfonyStyle;

/**
* @var ComposerJsonSymlinker
*/
private $composerJsonSymlinker;

/**
* @var ComposerVersionManipulator
*/
private $composerVersionManipulator;

/**
* @var UsedPackagesResolver
*/
private $usedPackagesResolver;

public function __construct(
PackageNamesProvider $packageNamesProvider,
JsonFileManager $jsonFileManager,
SymfonyStyle $symfonyStyle,
ComposerJsonSymlinker $composerJsonSymlinker,
ComposerVersionManipulator $composerVersionManipulator,
UsedPackagesResolver $usedPackagesResolver
) {
$this->packageNamesProvider = $packageNamesProvider;
$this->jsonFileManager = $jsonFileManager;
$this->symfonyStyle = $symfonyStyle;
$this->composerJsonSymlinker = $composerJsonSymlinker;
$this->composerVersionManipulator = $composerVersionManipulator;
$this->usedPackagesResolver = $usedPackagesResolver;
}

public function processPackage(SmartFileInfo $packageFileInfo, SmartFileInfo $mainComposerJsonFileInfo): void
{
$packageComposerJson = $this->jsonFileManager->loadFromFileInfo($packageFileInfo);

$usedPackageNames = $this->usedPackagesResolver->resolveForPackage($packageComposerJson);
$packageComposerJson = $this->composerVersionManipulator->setAsteriskVersionForUsedPackages(
$packageComposerJson,
$usedPackageNames
);

// possibly replace them all to cover recursive secondary dependencies
$packageNames = $this->packageNamesProvider->provide();

$packageComposerJson = $this->composerJsonSymlinker->decoratePackageComposerJsonWithPackageSymlinks(
$packageComposerJson,
$packageNames,
$mainComposerJsonFileInfo
);

$message = sprintf('File "%s" was updated', $packageFileInfo->getRelativeFilePathFromCwd());
$this->symfonyStyle->note($message);

$this->jsonFileManager->saveJsonWithFileInfo($packageComposerJson, $packageFileInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function testItCanAppendPathRepository(): void
'options' => [
'symlink' => false,
],
'canonical' => false,
],
[
'type' => 'composer',
Expand Down Expand Up @@ -84,6 +85,7 @@ public function testItCanAddPathRepository(): void
'options' => [
'symlink' => false,
],
'canonical' => false,
],
],
], $packageComposerJson);
Expand Down

0 comments on commit 3ca9d36

Please sign in to comment.