|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the humbug/php-scoper package. |
| 5 | + * |
| 6 | + * Copyright (c) 2017 Théo FIDRY <[email protected]>, |
| 7 | + * Pádraic Brady <[email protected]> |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Humbug\PhpScoper\Autoload; |
| 14 | + |
| 15 | +use Composer\Semver\Semver; |
| 16 | +use Symfony\Requirements\RequirementCollection; |
| 17 | + |
| 18 | +/** |
| 19 | + * Collect the list of requirements for running the project. Code in this file must be PHP 5.3+ compatible as is used |
| 20 | + * to know if PHP-Scoper can be run or not. |
| 21 | + */ |
| 22 | +final class Requirements extends RequirementCollection |
| 23 | +{ |
| 24 | + public function __construct($composerJson) |
| 25 | + { |
| 26 | + $composerConfig = $this->readComposer($composerJson); |
| 27 | + |
| 28 | + $this->addPhpVersionRequirement($composerConfig); |
| 29 | + $this->addExtensionRequirements($composerConfig); |
| 30 | + } |
| 31 | + |
| 32 | + private function readComposer($composerJson) |
| 33 | + { |
| 34 | + $composer = json_decode(file_get_contents($composerJson), true); |
| 35 | + |
| 36 | + return $composer['require']; |
| 37 | + } |
| 38 | + |
| 39 | + private function addPhpVersionRequirement(array $composerConfig) |
| 40 | + { |
| 41 | + $installedPhpVersion = phpversion(); |
| 42 | + $requiredPhpVersion = $composerConfig['php']; |
| 43 | + |
| 44 | + $this->addRequirement( |
| 45 | + Semver::satisfies(phpversion(), $requiredPhpVersion), |
| 46 | + sprintf( |
| 47 | + 'PHP version must satisfies "%s" ("%s" installed)', |
| 48 | + $requiredPhpVersion, |
| 49 | + $installedPhpVersion |
| 50 | + ), |
| 51 | + '' |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + private function addExtensionRequirements(array $composerConfig) |
| 56 | + { |
| 57 | + foreach ($composerConfig as $package => $constraint) { |
| 58 | + if (preg_match('/^ext-(?<extension>.+)$/', $package, $matches)) { |
| 59 | + $this->addExtensionRequirement($matches['extension']); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param string $extension Extension name, e.g. `iconv`. |
| 66 | + */ |
| 67 | + private function addExtensionRequirement($extension) |
| 68 | + { |
| 69 | + $this->addRequirement( |
| 70 | + extension_loaded($extension), |
| 71 | + sprintf( |
| 72 | + 'The extension "%s" must be enabled.', |
| 73 | + $extension |
| 74 | + ), |
| 75 | + '' |
| 76 | + ); |
| 77 | + } |
| 78 | +} |
0 commit comments