phabel/src/Target/Php.php

72 lines
1.8 KiB
PHP
Raw Normal View History

2020-11-01 20:44:11 +01:00
<?php
namespace Phabel\Target;
use Phabel\Plugin;
/**
* Makes changes necessary to polyfill syntaxes of various PHP versions.
*
* @author Daniil Gentili <daniil@daniil.it>
* @license MIT
*/
class Php extends Plugin
{
/**
* PHP versions.
*/
private const VERSIONS = [
'55',
'56',
'70',
'71',
'72',
'73',
'74',
'80',
];
/**
* Default target.
*/
private const DEFAULT_TARGET = '70';
/**
* Get PHP version range to target.
*
* @param array $config
* @return array
*/
private static function getRange(array $config): array
{
$target = $config['target'] ?? PHP_MAJOR_VERSION.PHP_MINOR_VERSION;
2020-11-04 12:00:16 +01:00
if (\preg_match(":^\D*(\d+\.\d+)\..*:", $config['target'], $matches)) {
2020-11-01 20:44:11 +01:00
$target = $matches[1];
}
2020-11-04 12:00:16 +01:00
$key = \array_search(\str_replace('.', '', $target), self::VERSIONS);
2020-11-01 20:44:11 +01:00
return \array_slice(
self::VERSIONS,
$key === false ? self::DEFAULT_TARGET : $key
);
}
public static function composerRequires(array $config): array
{
return \array_fill_keys(
\array_map(fn (string $version): string => "symfony/polyfill-$version", self::getRange($config)),
'*'
);
}
public static function runWithAfter(array $config): array
{
$classes = [];
foreach (self::getRange($config) as $version) {
2020-11-04 12:00:16 +01:00
foreach (\scandir(__DIR__."/Php$version") as $file) {
if (\substr($file, -4) !== '.php') {
continue;
}
$class = \basename($version, '.php');
2020-11-01 20:44:11 +01:00
$classes[$class] = $config[$class] ?? [];
}
}
return $classes;
}
}