refacoring

This commit is contained in:
Yuri Kuznetsov
2021-04-25 12:12:52 +03:00
parent 168f37dc8a
commit 02e2f42481
5 changed files with 41 additions and 38 deletions
+18 -14
View File
@@ -29,6 +29,8 @@
namespace Espo\Core\Utils;
use Espo\Core\Utils\File\ClassMap;
/**
* Finds classes of a specific category. Category examples: Services, Controllers.
* First it checks ib the custom folder, then modules, then the internal folder.
@@ -36,19 +38,19 @@ namespace Espo\Core\Utils;
*/
class ClassFinder
{
protected $classParser;
private $classMap;
protected $pathsTemplate = [
private $pathsTemplate = [
'corePath' => 'application/Espo/{category}',
'modulePath' => 'application/Espo/Modules/{*}/{category}',
'customPath' => 'custom/Espo/Custom/{category}',
];
protected $dataHash = [];
private $dataHashMap = [];
public function __construct(File\ClassParser $classParser)
public function __construct(ClassMap $classMap)
{
$this->classParser = $classParser;
$this->classMap = $classMap;
}
/**
@@ -64,37 +66,39 @@ class ClassFinder
}
/**
* Get [name => class-name] map.
* Get a name => class name map.
*
* @return array<string, string>
*/
public function getMap(string $category, bool $subDirs = false): array
{
if (!array_key_exists($category, $this->dataHash)) {
if (!array_key_exists($category, $this->dataHashMap)) {
$this->load($category, $subDirs);
}
return $this->dataHash[$category] ?? [];
return $this->dataHashMap[$category] ?? [];
}
protected function load(string $category, bool $subDirs = false)
private function load(string $category, bool $subDirs = false): void
{
$path = $this->buildPaths($category);
$cacheFile = $this->buildCacheKey($category);
$this->dataHash[$category] = $this->classParser->getData($path, $cacheFile, null, $subDirs);
$this->dataHashMap[$category] = $this->classMap->getData($path, $cacheFile, null, $subDirs);
}
protected function buildPaths(string $category): array
private function buildPaths(string $category): array
{
$paths = [];
foreach ($this->pathsTemplate as $key => $value) {
$path[$key] = str_replace('{category}', $category, $value);
$paths[$key] = str_replace('{category}', $category, $value);
}
return $path;
return $paths;
}
protected function buildCacheKey(string $category): string
private function buildCacheKey(string $category): string
{
return 'classmap' . str_replace('/', '', $category);
}
@@ -40,7 +40,7 @@ use Espo\Core\{
Utils\Metadata,
Utils\File\Manager as FileManager,
ORM\EntityManager,
Utils\File\ClassParser,
Utils\File\ClassMap,
Utils\Metadata\OrmMetadataData,
Utils\Util,
Utils\Database\Helper,
@@ -53,10 +53,14 @@ use Throwable;
class Schema
{
private $config;
private $metadata;
private $fileManager;
private $entityManager;
private $classParser;
private $classMap;
private $comparator;
@@ -93,14 +97,14 @@ class Schema
Metadata $metadata,
FileManager $fileManager,
EntityManager $entityManager,
ClassParser $classParser,
ClassMap $classMap,
OrmMetadataData $ormMetadataData
) {
$this->config = $config;
$this->metadata = $metadata;
$this->fileManager = $fileManager;
$this->entityManager = $entityManager;
$this->classParser = $classParser;
$this->classMap = $classMap;
$this->databaseHelper = new Helper($this->config);
@@ -145,11 +149,6 @@ class Schema
return $this->converter;
}
protected function getClassParser()
{
return $this->classParser;
}
public function getPlatform()
{
return $this->getConnection()->getDatabasePlatform();
@@ -287,7 +286,7 @@ class Schema
{
$methods = ['beforeRebuild', 'afterRebuild'];
$rebuildActions = $this->getClassParser()->getData($this->rebuildActionsPath, null, $methods);
$rebuildActions = $this->classMap->getData($this->rebuildActionsPath, null, $methods);
$classes = [];
@@ -30,18 +30,16 @@
namespace Espo\Core\Utils\File;
use Espo\Core\{
Exceptions\Error,
Utils\Util,
Utils\File\Manager as FileManager,
Utils\Config,
Utils\Metadata,
Utils\DataCache,
};
use ReflectionClass;
class ClassParser
class ClassMap
{
private $fileManager;
@@ -66,11 +64,13 @@ class ClassParser
/**
* Return paths to class files.
*
* @param string | array $paths in format [
* @param string|array $paths in the format ```
* [
* 'corePath' => '',
* 'modulePath' => '',
* 'customPath' => '',
* ]
* ```
* @param $allowedMethods If specified, classes w/o specified method will be ignored.
*/
public function getData(
@@ -122,7 +122,7 @@ class ClassParser
return $data;
}
protected function getClassNameHash($dirs, ?array $allowedMethods = [], bool $subDirs = false)
private function getClassNameHash($dirs, ?array $allowedMethods = [], bool $subDirs = false): array
{
if (is_string($dirs)) {
$dirs = (array) $dirs;
@@ -141,13 +141,13 @@ class ClassParser
return $data;
}
protected function fillHashFromFileList(
private function fillHashFromFileList(
array $fileList,
string $dir,
?array $allowedMethods,
array &$data,
string $category = ''
) {
): void {
foreach ($fileList as $key => $file) {
if (is_string($key)) {
@@ -8,9 +8,6 @@
"ormMetadataData": {
"className": "Espo\\Core\\Utils\\Metadata\\OrmMetadataData"
},
"classParser": {
"className": "Espo\\Core\\Utils\\File\\ClassParser"
},
"classFinder": {
"className": "Espo\\Core\\Utils\\ClassFinder"
},
@@ -31,11 +31,11 @@ namespace tests\unit\Espo\Core\Utils\File;
use tests\unit\ReflectionHelper;
use Espo\Core\Utils\File\ClassParser;
use Espo\Core\Utils\File\ClassMAp;
use Espo\Core\Utils\DataCache;
use Espo\Core\Utils\File\Manager as FileManager;
class ClassParserTest extends \PHPUnit\Framework\TestCase
class ClassMapTest extends \PHPUnit\Framework\TestCase
{
protected $object;
@@ -52,8 +52,11 @@ class ClassParserTest extends \PHPUnit\Framework\TestCase
$this->dataCache = $this->getMockBuilder(DataCache::class)->disableOriginalConstructor()->getMock();
$this->object = new ClassParser(
$this->objects['fileManager'], $this->objects['config'], $this->objects['metadata'], $this->dataCache
$this->object = new ClassMap(
$this->objects['fileManager'],
$this->objects['config'],
$this->objects['metadata'],
$this->dataCache
);
$this->reflection = new ReflectionHelper($this->object);