2023-12-24 12:52:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2024-01-02 02:40:38 +00:00
|
|
|
namespace Rector\Php;
|
2023-12-24 12:52:27 +00:00
|
|
|
|
2024-04-01 16:51:34 +00:00
|
|
|
use RectorPrefix202404\Nette\Utils\FileSystem;
|
|
|
|
use RectorPrefix202404\Nette\Utils\Json;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Configuration\Option;
|
|
|
|
use Rector\Configuration\Parameter\SimpleParameterProvider;
|
|
|
|
use Rector\ValueObject\PolyfillPackage;
|
2023-12-24 12:52:27 +00:00
|
|
|
final class PolyfillPackagesProvider
|
|
|
|
{
|
|
|
|
/**
|
2023-12-25 03:00:38 +00:00
|
|
|
* @var null|array<int, PolyfillPackage::*>
|
2023-12-24 12:52:27 +00:00
|
|
|
*/
|
2023-12-25 03:00:38 +00:00
|
|
|
private $cachedPolyfillPackages = null;
|
2023-12-24 12:52:27 +00:00
|
|
|
/**
|
2023-12-25 03:00:38 +00:00
|
|
|
* @return array<int, PolyfillPackage::*>
|
2023-12-24 12:52:27 +00:00
|
|
|
*/
|
|
|
|
public function provide() : array
|
|
|
|
{
|
2024-01-13 01:51:20 +00:00
|
|
|
// disable cache in tests
|
2023-12-24 12:52:27 +00:00
|
|
|
if (SimpleParameterProvider::hasParameter(Option::POLYFILL_PACKAGES)) {
|
|
|
|
return SimpleParameterProvider::provideArrayParameter(Option::POLYFILL_PACKAGES);
|
|
|
|
}
|
2023-12-25 03:00:38 +00:00
|
|
|
// already cached, even only empty array
|
|
|
|
if ($this->cachedPolyfillPackages !== null) {
|
|
|
|
return $this->cachedPolyfillPackages;
|
|
|
|
}
|
2023-12-24 12:52:27 +00:00
|
|
|
$projectComposerJson = \getcwd() . '/composer.json';
|
|
|
|
if (!\file_exists($projectComposerJson)) {
|
2023-12-25 03:00:38 +00:00
|
|
|
$this->cachedPolyfillPackages = [];
|
2023-12-24 12:52:27 +00:00
|
|
|
return $this->cachedPolyfillPackages;
|
|
|
|
}
|
|
|
|
$composerContents = FileSystem::read($projectComposerJson);
|
2024-04-09 20:37:51 +00:00
|
|
|
$composerJson = Json::decode($composerContents, \true);
|
2023-12-24 12:52:27 +00:00
|
|
|
$this->cachedPolyfillPackages = $this->filterPolyfillPackages($composerJson['require'] ?? []);
|
|
|
|
return $this->cachedPolyfillPackages;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param array<string, string> $require
|
2023-12-25 03:00:38 +00:00
|
|
|
* @return array<int, PolyfillPackage::*>
|
2023-12-24 12:52:27 +00:00
|
|
|
*/
|
|
|
|
private function filterPolyfillPackages(array $require) : array
|
|
|
|
{
|
2023-12-25 03:00:38 +00:00
|
|
|
return \array_filter(\array_keys($require), static function (string $packageName) : bool {
|
|
|
|
return \strncmp($packageName, 'symfony/polyfill-', \strlen('symfony/polyfill-')) === 0;
|
2023-12-24 12:52:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|