2019-10-13 07:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2019-08-18 12:56:56 +02:00
|
|
|
namespace Rector\PSR4\Composer;
|
|
|
|
|
2021-06-20 00:37:20 +00:00
|
|
|
use RectorPrefix20210620\Symplify\ComposerJsonManipulator\ValueObject\ComposerJsonSection;
|
|
|
|
use RectorPrefix20210620\Symplify\SmartFileSystem\Json\JsonFileSystem;
|
2019-08-18 12:56:56 +02:00
|
|
|
final class PSR4AutoloadPathsProvider
|
|
|
|
{
|
|
|
|
/**
|
2021-01-15 21:27:36 +01:00
|
|
|
* @var array<string, array<string, string>>
|
2019-08-18 12:56:56 +02:00
|
|
|
*/
|
|
|
|
private $cachedComposerJsonPSR4AutoloadPaths = [];
|
2020-07-24 14:21:34 +02:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Symplify\SmartFileSystem\Json\JsonFileSystem
|
2020-07-24 14:21:34 +02:00
|
|
|
*/
|
2021-01-15 21:27:36 +01:00
|
|
|
private $jsonFileSystem;
|
2021-06-20 00:37:20 +00:00
|
|
|
public function __construct(\RectorPrefix20210620\Symplify\SmartFileSystem\Json\JsonFileSystem $jsonFileSystem)
|
2020-07-24 14:21:34 +02:00
|
|
|
{
|
2021-01-15 21:27:36 +01:00
|
|
|
$this->jsonFileSystem = $jsonFileSystem;
|
2020-07-24 14:21:34 +02:00
|
|
|
}
|
2019-08-18 12:56:56 +02:00
|
|
|
/**
|
2021-03-12 23:20:25 +01:00
|
|
|
* @return array<string, string|string[]>
|
2019-08-18 12:56:56 +02:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function provide() : array
|
2019-08-18 12:56:56 +02:00
|
|
|
{
|
|
|
|
if ($this->cachedComposerJsonPSR4AutoloadPaths !== []) {
|
|
|
|
return $this->cachedComposerJsonPSR4AutoloadPaths;
|
|
|
|
}
|
2021-01-15 21:27:36 +01:00
|
|
|
$composerJson = $this->jsonFileSystem->loadFilePathToJson($this->getComposerJsonPath());
|
2021-06-20 00:37:20 +00:00
|
|
|
$psr4Autoloads = \array_merge($composerJson[\RectorPrefix20210620\Symplify\ComposerJsonManipulator\ValueObject\ComposerJsonSection::AUTOLOAD]['psr-4'] ?? [], $composerJson[\RectorPrefix20210620\Symplify\ComposerJsonManipulator\ValueObject\ComposerJsonSection::AUTOLOAD_DEV]['psr-4'] ?? []);
|
2019-08-18 12:56:56 +02:00
|
|
|
$this->cachedComposerJsonPSR4AutoloadPaths = $this->removeEmptyNamespaces($psr4Autoloads);
|
|
|
|
return $this->cachedComposerJsonPSR4AutoloadPaths;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function getComposerJsonPath() : string
|
2019-10-30 10:49:07 +01:00
|
|
|
{
|
|
|
|
// assume the project has "composer.json" in root directory
|
2021-05-09 20:15:43 +00:00
|
|
|
return \getcwd() . '/composer.json';
|
2019-10-30 10:49:07 +01:00
|
|
|
}
|
2019-08-18 12:56:56 +02:00
|
|
|
/**
|
2021-01-15 21:27:36 +01:00
|
|
|
* @param array<string, array<string, string>> $psr4Autoloads
|
|
|
|
* @return array<string, array<string, string>>
|
2019-08-18 12:56:56 +02:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function removeEmptyNamespaces(array $psr4Autoloads) : array
|
2019-08-18 12:56:56 +02:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
return \array_filter($psr4Autoloads, function (string $psr4Autoload) : bool {
|
2020-04-25 16:45:36 +02:00
|
|
|
return $psr4Autoload !== '';
|
2021-05-09 20:15:43 +00:00
|
|
|
}, \ARRAY_FILTER_USE_KEY);
|
2019-08-18 12:56:56 +02:00
|
|
|
}
|
|
|
|
}
|