Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions migrations/PHP-86/migrate
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

declare(strict_types=1);

use Laminas\Internal\Migrations\ComposerFile;
use Laminas\Internal\Migrations\LaminasCI;
use Laminas\Internal\Migrations\LaminasCIWorkflow;
use Laminas\Internal\Migrations\MagoConfig;
use Laminas\Internal\Migrations\Makefile;
use Laminas\Internal\Migrations\PatchComposer;
use Laminas\Internal\Migrations\ProjectInformation;

require __DIR__ . '/../autoloader.php';
Expand All @@ -25,10 +25,10 @@ if (
$project = ProjectInformation::fromDirectory($rootDirectory);

// Patch Composer
(new PatchComposer($project->composerFile))(
'8.3.99',
'~8.3 || ~8.4 || ~8.5 || ~8.6',
);
$composer = new ComposerFile($project->composerFile);
$composer->setPlatform('8.3.99');
$composer->setPhpVersionConstraint('~8.3 || ~8.4 || ~8.5 || ~8.6');
$composer->write();

// Enable BC Checker and Ignore Platform Reqs on 8.6
if ($project->laminasCiConfig !== null) {
Expand Down Expand Up @@ -74,3 +74,25 @@ if ($project->makefile !== null) {
$makefile = new Makefile($project->makefile);
$makefile->updatePHPVersionTo('8.3');
}

// Update the composer files for anything in the tools directory
$toolsDirectory = $project->projectDirectory . '/tools';
if (is_dir($toolsDirectory) && file_exists($toolsDirectory)) {
$iterator = new DirectoryIterator($toolsDirectory);
/** @var SplFileInfo&DirectoryIterator $item */
foreach ($iterator as $item) {
if ($item->isDot() || ! $item->isDir()) {
continue;
}

$file = $item->getRealPath() . '/composer.json';
if (! file_exists($file)) {
continue;
}

$composer = new ComposerFile($file);
$composer->setPlatform('8.3.99');
$composer->setPhpVersionConstraint('~8.3 || ~8.4 || ~8.5 || ~8.6');
$composer->write();
}
}
89 changes: 80 additions & 9 deletions migrations/src/ComposerFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,46 @@
namespace Laminas\Internal\Migrations;

use Composer\Semver\VersionParser;
use JsonException;
use RuntimeException;
use Throwable;

use function array_slice;
use function assert;
use function count;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function json_decode;
use function json_encode;

final readonly class ComposerFile
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;

final class ComposerFile
{
/**
* @var array{
* config?: array{
* platform?: array{
* php?: non-empty-string,
* },
* },
* require: array{
* php: non-empty-string,
* }
* php?: non-empty-string,
* },
* }
*/
private array $data;
private bool $dirty = false;

/** @param non-empty-string $file */
/**
* @param non-empty-string $file
* @throws JsonException
*/
public function __construct(
private string $file,
private readonly string $file,
) {
/** @mago-expect analysis:mixed-property-type-coercion(1) */
$this->data = json_decode(
Expand All @@ -36,7 +53,10 @@ public function __construct(
);
}

/** @return non-empty-string */
/**
* @return non-empty-string
* @throws Throwable
*/
public function minimumPHPVersionAsMinor(): string
{
$parser = new VersionParser();
Expand All @@ -50,13 +70,48 @@ public function minimumPHPVersionAsMinor(): string
return $minor;
}

/** @return non-empty-string */
/**
* @return non-empty-string
* @throws RuntimeException
*/
public function phpVersionConstraint(): string
{
return $this->data['require']['php'];
$version = $this->data['require']['php'] ?? null;
if ($version === null) {
throw new RuntimeException('This composer file does not declare a PHP version constraint');
}

return $version;
}

/** @param non-empty-string $constraint */
public function setPhpVersionConstraint(string $constraint): void
{
if (($this->data['require']['php'] ?? null) === $constraint) {
return;
}

$this->data['require']['php'] = $constraint;
$this->dirty = true;
}

/** @return non-empty-string */
/** @param non-empty-string $version */
public function setPlatform(string $version): void
{
$this->data['config'] ??= [];
$this->data['config']['platform'] ??= [];
if (($this->data['config']['platform']['php'] ?? null) === $version) {
return;
}

$this->data['config']['platform']['php'] = $version;
$this->dirty = true;
}

/**
* @return non-empty-string
* @throws JsonException
*/
private function read(): string
{
$contents = file_get_contents($this->file);
Expand All @@ -67,4 +122,20 @@ private function read(): string

return $contents;
}

/** @throws JsonException */
public function write(): void
{
if (! $this->dirty) {
return;
}

file_put_contents(
$this->file,
json_encode(
$this->data,
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT,
),
);
}
}
83 changes: 0 additions & 83 deletions migrations/src/PatchComposer.php

This file was deleted.

Loading
Loading