swooletw /
laravel-swoole
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SwooleTW\Http\Helpers; |
||
| 4 | |||
| 5 | use LogicException; |
||
| 6 | use Illuminate\Support\Arr; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class FW |
||
| 10 | */ |
||
| 11 | final class FW |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Laravel |
||
| 15 | * |
||
| 16 | * @const string |
||
| 17 | */ |
||
| 18 | public const LARAVEL = 'laravel'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Lumen |
||
| 22 | * |
||
| 23 | * @const string |
||
| 24 | */ |
||
| 25 | public const LUMEN = 'lumen'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Version regular expression |
||
| 29 | * |
||
| 30 | * @const string |
||
| 31 | */ |
||
| 32 | public const VERSION_FULL = '/\s*(?:\d+\.?)+/i'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Version without bugfix regular expression |
||
| 36 | * |
||
| 37 | * @const string |
||
| 38 | */ |
||
| 39 | public const VERSION_WITHOUT_BUG_FIX = '/\s*(?:\d+\.*?){2}/i'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Returns true if current Framework in types |
||
| 43 | * |
||
| 44 | * @param string ...$types |
||
| 45 | * |
||
| 46 | * @return bool |
||
| 47 | */ |
||
| 48 | public static function is(string ...$types): bool |
||
| 49 | { |
||
| 50 | return in_array(static::current(), $types, true); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Current Framework |
||
| 55 | * |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public static function current(): string |
||
| 59 | { |
||
| 60 | return class_exists('Illuminate\Foundation\Application') ? static::LARAVEL : static::LUMEN; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns application version |
||
| 65 | * |
||
| 66 | * @param string $expression |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public static function version(string $expression = self::VERSION_WITHOUT_BUG_FIX): string |
||
| 71 | { |
||
| 72 | if (static::is(static::LARAVEL)) { |
||
| 73 | return static::extractVersion(constant('Illuminate\Foundation\Application::VERSION'), $expression); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 74 | } |
||
| 75 | |||
| 76 | /** @var \Laravel\Lumen\Application $app */ |
||
| 77 | $app = call_user_func('Laravel\Lumen\Application::getInstance'); |
||
| 78 | |||
| 79 | if ($version = static::extractVersion($app->version(), $expression)) { |
||
| 80 | return $version; |
||
| 81 | } |
||
| 82 | |||
| 83 | throw new LogicException('No any version found.'); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Extracts lumen version from $app->version() |
||
| 88 | * |
||
| 89 | * @param string $version |
||
| 90 | * @param string $expression |
||
| 91 | * |
||
| 92 | * @return string|null |
||
| 93 | */ |
||
| 94 | protected static function extractVersion(string $version, string $expression): ?string |
||
| 95 | { |
||
| 96 | if (preg_match($expression, $version, $result)) { |
||
| 97 | return Arr::first($result); |
||
| 98 | } |
||
| 99 | |||
| 100 | return null; |
||
| 101 | } |
||
| 102 | } |