mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-02 09:20:52 +00:00
check minimal php version
This commit is contained in:
parent
8cec2ea607
commit
78e518c105
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
|
||||
use Rector\Caching\ChangedFilesDetector;
|
||||
use Rector\Core\Configuration\Configuration;
|
||||
use Rector\Core\Configuration\MinimalVersionChecker;
|
||||
use Rector\Core\Console\Application;
|
||||
use Rector\Core\Console\Style\SymfonyStyleFactory;
|
||||
use Rector\Core\DependencyInjection\RectorContainerFactory;
|
||||
|
@ -30,6 +31,13 @@ $autoloadIncluder->includeDependencyOrRepositoryVendorAutoloadIfExists();
|
|||
$autoloadIncluder->autoloadFromCommandLine();
|
||||
|
||||
try {
|
||||
$composerJsonReader = new MinimalVersionChecker\ComposerJsonReader(__DIR__ . '/../composer.json');
|
||||
$versionChecker = new MinimalVersionChecker(
|
||||
PHP_VERSION,
|
||||
new MinimalVersionChecker\ComposerJsonParser($composerJsonReader->read())
|
||||
);
|
||||
$versionChecker->check();
|
||||
|
||||
$rectorConfigsResolver = new RectorConfigsResolver();
|
||||
$configs = $rectorConfigsResolver->provide();
|
||||
|
||||
|
|
|
@ -15,6 +15,11 @@ services:
|
|||
- '../src/PhpParser/Builder/*'
|
||||
- '../src/HttpKernel/*'
|
||||
- '../src/ValueObject/*'
|
||||
- '../src/Configuration/MinimalVersionChecker'
|
||||
- '../src/Configuration/MinimalVersionChecker/*'
|
||||
|
||||
Rector\Core\Configuration\MinimalVersionChecker:
|
||||
autowire: false
|
||||
|
||||
Symfony\Component\Console\Application:
|
||||
alias: 'Rector\Core\Console\Application'
|
||||
|
|
41
src/Configuration/MinimalVersionChecker.php
Normal file
41
src/Configuration/MinimalVersionChecker.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Configuration;
|
||||
|
||||
use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonParser;
|
||||
use Rector\Core\Exception\Application\PhpVersionException;
|
||||
|
||||
final class MinimalVersionChecker
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $installedPhpVersion;
|
||||
|
||||
/**
|
||||
* @var ComposerJsonParser
|
||||
*/
|
||||
private $composerJsonParser;
|
||||
|
||||
public function __construct(string $installedPhpVersion, ComposerJsonParser $composerJsonParser)
|
||||
{
|
||||
$this->installedPhpVersion = $installedPhpVersion;
|
||||
$this->composerJsonParser = $composerJsonParser;
|
||||
}
|
||||
|
||||
public function check(): void
|
||||
{
|
||||
$minimumPhpVersion = $this->composerJsonParser->getPhpVersion();
|
||||
|
||||
// Check minimum required PHP version
|
||||
if (version_compare($this->installedPhpVersion, $minimumPhpVersion, '<')) {
|
||||
throw new PhpVersionException(sprintf(
|
||||
'PHP version %s or higher is required, but you currently have %s installed.',
|
||||
$minimumPhpVersion,
|
||||
$this->installedPhpVersion
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Configuration\MinimalVersionChecker;
|
||||
|
||||
use Nette\Utils\Json;
|
||||
use Nette\Utils\Strings;
|
||||
|
||||
final class ComposerJsonParser
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $composerJson;
|
||||
|
||||
public function __construct(string $composerJson)
|
||||
{
|
||||
$this->composerJson = $composerJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPhpVersion()
|
||||
{
|
||||
$composerArray = Json::decode($this->composerJson, Json::FORCE_ARRAY);
|
||||
return Strings::trim($composerArray['require']['php'], '~^>=*.');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Configuration\MinimalVersionChecker;
|
||||
|
||||
use Nette\Utils\FileSystem;
|
||||
|
||||
final class ComposerJsonReader
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $filename;
|
||||
|
||||
public function __construct(string $composerJsonFilename)
|
||||
{
|
||||
$this->filename = $composerJsonFilename;
|
||||
}
|
||||
|
||||
public function read(): string
|
||||
{
|
||||
return FileSystem::read($this->filename);
|
||||
}
|
||||
}
|
11
src/Exception/Application/PhpVersionException.php
Normal file
11
src/Exception/Application/PhpVersionException.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Exception\Application;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class PhpVersionException extends Exception
|
||||
{
|
||||
}
|
46
tests/Configuration/ComposerJsonParserTest.php
Normal file
46
tests/Configuration/ComposerJsonParserTest.php
Normal file
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Configuration;
|
||||
|
||||
use Nette\Utils\Json;
|
||||
use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonParser;
|
||||
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
|
||||
|
||||
final class ComposerJsonParserTest extends AbstractKernelTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function test($expectedVersion, string $version): void
|
||||
{
|
||||
$actualPhpVersion = $this->getComposerJsonPhpVersion($version);
|
||||
|
||||
$this->assertSame($expectedVersion, $actualPhpVersion);
|
||||
}
|
||||
|
||||
public function dataProvider()
|
||||
{
|
||||
return [
|
||||
['7.2.0', '7.2.0'],
|
||||
['7.2.0', '~7.2.0'],
|
||||
['7.2', '7.2.*'],
|
||||
['7', '7.*.*'],
|
||||
['7.2.0', '~7.2.0'],
|
||||
['7.2.0', '^7.2.0'],
|
||||
['7.2.0', '>=7.2.0'],
|
||||
];
|
||||
}
|
||||
|
||||
private function getComposerJsonPhpVersion(string $version): string
|
||||
{
|
||||
$parser = new ComposerJsonParser(Json::encode([
|
||||
'require' =>
|
||||
[
|
||||
'php' => $version,
|
||||
],
|
||||
]));
|
||||
return $parser->getPhpVersion();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"require": {
|
||||
"php": "7.2.0"
|
||||
}
|
||||
}
|
39
tests/Configuration/MinimalVersionCheckerTest.php
Normal file
39
tests/Configuration/MinimalVersionCheckerTest.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Configuration;
|
||||
|
||||
use Rector\Core\Configuration\MinimalVersionChecker;
|
||||
use Rector\Core\Exception\Application\PhpVersionException;
|
||||
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
|
||||
|
||||
final class MinimalVersionCheckerTest extends AbstractKernelTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function test(string $version, bool $shouldThrowException): void
|
||||
{
|
||||
$composerJsonReader = new MinimalVersionChecker\ComposerJsonReader(
|
||||
__DIR__ . '/MinimalVersionChecker/composer-7.2.0.json'
|
||||
);
|
||||
$minimalVersionChecker = new MinimalVersionChecker(
|
||||
$version,
|
||||
new MinimalVersionChecker\ComposerJsonParser($composerJsonReader->read())
|
||||
);
|
||||
|
||||
if ($shouldThrowException) {
|
||||
$this->expectException(PhpVersionException::class);
|
||||
} else {
|
||||
$this->expectNotToPerformAssertions();
|
||||
}
|
||||
|
||||
$minimalVersionChecker->check();
|
||||
}
|
||||
|
||||
public function dataProvider()
|
||||
{
|
||||
return [['7.5.0', false], ['7.5.0-13ubuntu3.2', false], ['7.1.0', true], ['7.1.0-13ubuntu3.2', true]];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user