refactoring

This commit is contained in:
Yuri Kuznetsov
2021-06-26 11:07:04 +03:00
parent f38811ae9d
commit e8c66db876
2 changed files with 53 additions and 13 deletions
+21 -10
View File
@@ -33,6 +33,7 @@ use Espo\Core\{
Utils\Autoload\Loader,
Utils\DataCache,
Utils\File\Manager as FileManager,
Utils\Resource\PathProvider,
};
use Exception;
@@ -43,11 +44,7 @@ class Autoload
private $cacheKey = 'autoload';
private $paths = [
'corePath' => 'application/Espo/Resources/autoload.json',
'modulePath' => 'application/Espo/Modules/{*}/Resources/autoload.json',
'customPath' => 'custom/Espo/Custom/Resources/autoload.json',
];
private $autoloadFileName = 'autoload.json';
private $config;
@@ -59,18 +56,22 @@ class Autoload
private $loader;
private $pathProvider;
public function __construct(
Config $config,
Metadata $metadata,
DataCache $dataCache,
FileManager $fileManager,
Loader $loader
Loader $loader,
PathProvider $pathProvider
) {
$this->config = $config;
$this->metadata = $metadata;
$this->dataCache = $dataCache;
$this->fileManager = $fileManager;
$this->loader = $loader;
$this->pathProvider = $pathProvider;
}
private function getData(): array
@@ -101,15 +102,25 @@ class Autoload
private function loadData(): array
{
$data = $this->loadDataFromFile($this->paths['corePath']);
$corePath = $this->pathProvider->getCore() . $this->autoloadFileName;
$data = $this->loadDataFromFile($corePath);
foreach ($this->metadata->getModuleList() as $moduleName) {
$modulePath = str_replace('{*}', $moduleName, $this->paths['modulePath']);
$modulePath = $this->pathProvider->getModule($moduleName) . $this->autoloadFileName;
$data = array_merge_recursive($data, $this->loadDataFromFile($modulePath));
$data = array_merge_recursive(
$data,
$this->loadDataFromFile($modulePath)
);
}
return array_merge_recursive($data, $this->loadDataFromFile($this->paths['customPath']));
$customPath = $this->pathProvider->getCustom() . $this->autoloadFileName;
return array_merge_recursive(
$data,
$this->loadDataFromFile($customPath)
);
}
private function loadDataFromFile(string $filePath): array
+32 -3
View File
@@ -29,7 +29,6 @@
namespace tests\unit\Espo\Core\Utils;
use Espo\Core\{
Utils\Autoload,
Utils\Config,
@@ -37,27 +36,57 @@ use Espo\Core\{
Utils\Autoload\Loader,
Utils\DataCache,
Utils\File\Manager as FileManager,
Utils\Resource\PathProvider,
};
class AutoloadTest extends \PHPUnit\Framework\TestCase
{
protected function setUp() : void
protected function setUp(): void
{
$this->config = $this->createMock(Config::class);
$this->metadata = $this->createMock(Metadata::class);
$this->dataCache = $this->createMock(DataCache::class);
$this->fileManager = $this->createMock(FileManager::class);
$this->loader = $this->createMock(Loader::class);
$this->pathProvider = $this->createMock(PathProvider::class);
$this->initPathProvider();
$this->autoload = new Autoload(
$this->config,
$this->metadata,
$this->dataCache,
$this->fileManager,
$this->loader
$this->loader,
$this->pathProvider
);
}
private function initPathProvider(string $rootPath = ''): void
{
$this->pathProvider
->method('getCustom')
->willReturn($rootPath . 'custom/Espo/Custom/Resources/');
$this->pathProvider
->method('getCore')
->willReturn($rootPath . 'application/Espo/Resources/');
$this->pathProvider
->method('getModule')
->willReturnCallback(
function (?string $moduleName) use ($rootPath): string {
$path = $rootPath . 'application/Espo/Modules/{*}/Resources/';
if ($moduleName === null) {
return $path;
}
return str_replace('{*}', $moduleName, $path);
}
);
}
public function testMerge()
{
$this->metadata