From af7a73ec428044e6ffa59f5f0750afacee3dd039 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 17 Jun 2020 16:22:06 +0300 Subject: [PATCH] class finder --- application/Espo/Core/Loaders/ClassFinder.php | 40 ++++++++++ application/Espo/Core/ServiceFactory.php | 44 ++--------- application/Espo/Core/Utils/ClassFinder.php | 79 +++++++++++++++++++ .../Espo/Core/Utils/ClassFinder.php | 50 ++++++++++++ 4 files changed, 175 insertions(+), 38 deletions(-) create mode 100644 application/Espo/Core/Loaders/ClassFinder.php create mode 100644 application/Espo/Core/Utils/ClassFinder.php create mode 100644 tests/integration/Espo/Core/Utils/ClassFinder.php diff --git a/application/Espo/Core/Loaders/ClassFinder.php b/application/Espo/Core/Loaders/ClassFinder.php new file mode 100644 index 0000000000..240c74d5cd --- /dev/null +++ b/application/Espo/Core/Loaders/ClassFinder.php @@ -0,0 +1,40 @@ +getContainer()->get('classParser') + ); + } +} diff --git a/application/Espo/Core/ServiceFactory.php b/application/Espo/Core/ServiceFactory.php index ee0265a7b4..8db3a6bb60 100644 --- a/application/Espo/Core/ServiceFactory.php +++ b/application/Espo/Core/ServiceFactory.php @@ -28,72 +28,40 @@ ************************************************************************/ namespace Espo\Core; -use \Espo\Core\Exceptions\Error; -use \Espo\Core\Utils\Util; +use Espo\Core\Exceptions\Error; class ServiceFactory { private $container; - protected $cacheFile = 'data/cache/application/services.php'; - - /** - * @var array - path to Service files - */ - protected $paths = [ - 'corePath' => 'application/Espo/Services', - 'modulePath' => 'application/Espo/Modules/{*}/Services', - 'customPath' => 'custom/Espo/Custom/Services', - ]; - - protected $data; - public function __construct(Container $container) { $this->container = $container; } - protected function getFileManager() - { - return $this->container->get('fileManager'); - } - protected function getContainer() { return $this->container; } - protected function init() - { - $classParser = $this->getContainer()->get('classParser'); - $this->data = $classParser->getData($this->paths, $this->cacheFile); - } - protected function getClassName($name) { - if (!isset($this->data)) { - $this->init(); - } - - if (isset($this->data[$name])) { - return $this->data[$name]; - } - - return false; + return $this->getContainer()->get('classFinder')->find('Services', $name); } public function checkExists($name) { $className = $this->getClassName($name); - if (!empty($className)) { - return true; + if (!$className) { + return false; } + return true; } public function create($name) { $className = $this->getClassName($name); - if (empty($className)) { + if (!$className) { throw new Error("Service '{$name}' was not found."); } return $this->createByClassName($className); diff --git a/application/Espo/Core/Utils/ClassFinder.php b/application/Espo/Core/Utils/ClassFinder.php new file mode 100644 index 0000000000..006f9eff52 --- /dev/null +++ b/application/Espo/Core/Utils/ClassFinder.php @@ -0,0 +1,79 @@ + 'application/Espo/{category}', + 'modulePath' => 'application/Espo/Modules/{*}/{category}', + 'customPath' => 'custom/Espo/Custom/{category}', + ]; + + protected $dataHash = []; + + public function __construct(File\ClassParser $classParser) + { + $this->classParser = $classParser; + } + + /** + * Find class name by category (e.g. Controllers, Services) and name. + */ + public function find(string $category, string $name) : ?string + { + if (!array_key_exists($category, $this->dataHash)) { + $path = $this->buildPaths($category); + $cacheFile = $this->buildCacheFilePath($category); + $this->dataHash[$category] = $this->classParser->getData($path, $cacheFile); + } + + $className = $this->dataHash[$category][$name] ?? null; + + return $className; + } + + protected function buildPaths(string $category) : array + { + $paths = []; + foreach ($this->pathsTemplate as $key => $value) { + $path[$key] = str_replace('{category}', $category, $value); + } + return $path; + } + + protected function buildCacheFilePath(string $category) : string + { + $path = 'data/cache/application/' . str_replace('/', '_', strtolower($category)) . '.php'; + return $path; + } +} diff --git a/tests/integration/Espo/Core/Utils/ClassFinder.php b/tests/integration/Espo/Core/Utils/ClassFinder.php new file mode 100644 index 0000000000..5a6f38455a --- /dev/null +++ b/tests/integration/Espo/Core/Utils/ClassFinder.php @@ -0,0 +1,50 @@ +getContainer()->get('classFinder'); + + $this->assertEquals( + '\\Espo\\Modules\\Crm\\Services\\Account', + $classFinder->find('Services', 'Account') + ); + + $this->assertEquals( + '\\Espo\\Services\\Record', + $classFinder->find('Services', 'Record') + ); + + $this->assertTrue(file_exists('data/cache/application/services.php')); + } +}