From 3743d38c7d431cc96f7b812e939c762222f8eac9 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Wed, 4 Jul 2018 21:11:46 +0200 Subject: [PATCH] Lookup required PHP version from composer.json --- src/Checks/CorrectPhpVersionIsInstalled.php | 28 +++++++++- tests/CorrectPhpVersionIsInstalledTest.php | 19 +++++++ tests/fixtures/composer.json | 61 +++++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 tests/CorrectPhpVersionIsInstalledTest.php create mode 100644 tests/fixtures/composer.json diff --git a/src/Checks/CorrectPhpVersionIsInstalled.php b/src/Checks/CorrectPhpVersionIsInstalled.php index 6fcdd88..9a91add 100644 --- a/src/Checks/CorrectPhpVersionIsInstalled.php +++ b/src/Checks/CorrectPhpVersionIsInstalled.php @@ -2,8 +2,18 @@ namespace BeyondCode\SelfDiagnosis\Checks; +use Illuminate\Filesystem\Filesystem; + class CorrectPhpVersionIsInstalled implements Check { + /** @var Filesystem */ + private $filesystem; + + public function __construct(Filesystem $filesystem) + { + $this->filesystem = $filesystem; + } + /** * The name of the check. * @@ -17,20 +27,34 @@ public function name(): string /** * Perform the actual verification of this check. * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @return bool */ public function check(): bool { - return version_compare(phpversion(), '7.1.3', '>='); + return version_compare(phpversion(), $this->getRequiredPhpVersion(), '>='); } /** * The error message to display in case the check does not pass. * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @return string */ public function message() : string { - return 'You do not have the required PHP version installed.'; + return 'You do not have the required PHP version installed.'.PHP_EOL.'Required: '.$this->getRequiredPhpVersion().PHP_EOL.'Used: '.phpversion(); + } + + /** + * @return mixed + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + public function getRequiredPhpVersion() + { + $composer = json_decode($this->filesystem->get(base_path('composer.json')), true); + $versionString = array_get($composer, 'require.php'); + + return str_replace(['^', '~', '<', '>', '='], '', $versionString); } } \ No newline at end of file diff --git a/tests/CorrectPhpVersionIsInstalledTest.php b/tests/CorrectPhpVersionIsInstalledTest.php new file mode 100644 index 0000000..8a91e6b --- /dev/null +++ b/tests/CorrectPhpVersionIsInstalledTest.php @@ -0,0 +1,19 @@ +app->setBasePath(__DIR__ . '/fixtures'); + + $check = app(CorrectPhpVersionIsInstalled::class); + + $this->assertSame('7.1.3', $check->getRequiredPhpVersion()); + } +} \ No newline at end of file diff --git a/tests/fixtures/composer.json b/tests/fixtures/composer.json new file mode 100644 index 0000000..4ee4392 --- /dev/null +++ b/tests/fixtures/composer.json @@ -0,0 +1,61 @@ +{ + "name": "laravel/laravel", + "description": "The Laravel Framework.", + "keywords": [ + "framework", + "laravel" + ], + "license": "MIT", + "type": "project", + "require": { + "php": "^7.1.3", + "fideloper/proxy": "^4.0", + "laravel/framework": "5.6.*", + "laravel/tinker": "^1.0" + }, + "require-dev": { + "filp/whoops": "^2.0", + "fzaninotto/faker": "^1.4", + "mockery/mockery": "^1.0", + "nunomaduro/collision": "^2.0", + "phpunit/phpunit": "^7.0" + }, + "autoload": { + "classmap": [ + "database/seeds", + "database/factories" + ], + "psr-4": { + "App\\": "app/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "extra": { + "laravel": { + "dont-discover": [] + } + }, + "scripts": { + "post-root-package-install": [ + "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" + ], + "post-create-project-cmd": [ + "@php artisan key:generate" + ], + "post-autoload-dump": [ + "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", + "@php artisan package:discover" + ] + }, + "config": { + "preferred-install": "dist", + "sort-packages": true, + "optimize-autoloader": true + }, + "minimum-stability": "dev", + "prefer-stable": true +} \ No newline at end of file