From 69f0f8d8bf152ca5614eb31e1f1a262edb34eae8 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 17 Jun 2020 18:32:50 +0300 Subject: [PATCH] grand impromenets for customization abilities --- application/Espo/Core/AclManager.php | 41 +++---- application/Espo/Core/Container.php | 46 +++----- application/Espo/Core/ControllerManager.php | 47 +++----- application/Espo/Core/CronManager.php | 6 +- application/Espo/Core/EntryPointManager.php | 53 ++------- .../Espo/Core/Loaders/NotificatorFactory.php | 40 +++++++ .../Core/Loaders/SelectManagerFactory.php | 3 +- .../Espo/Core/Loaders/ServiceFactory.php | 40 +++++++ application/Espo/Core/NotificatorFactory.php | 28 ++--- application/Espo/Core/ORM/EntityManager.php | 22 ++-- application/Espo/Core/Portal/AclManager.php | 42 ++++---- application/Espo/Core/Portal/Container.php | 23 ++-- .../Espo/Core/SelectManagerFactory.php | 41 ++++--- application/Espo/Core/Utils/ClassFinder.php | 30 ++++-- application/Espo/Core/Utils/Metadata.php | 43 -------- application/Espo/Core/Utils/ScheduledJob.php | 101 ++---------------- application/Espo/ORM/EntityFactory.php | 5 +- application/Espo/ORM/EntityManager.php | 6 +- application/Espo/ORM/RepositoryFactory.php | 7 +- .../metadata/app/containerServices.json | 4 + .../metadata/app/portalContainerServices.json | 6 ++ .../Espo/Core/Utils/ClassFinder.php | 2 +- 22 files changed, 254 insertions(+), 382 deletions(-) create mode 100644 application/Espo/Core/Loaders/NotificatorFactory.php create mode 100644 application/Espo/Core/Loaders/ServiceFactory.php create mode 100644 application/Espo/Resources/metadata/app/portalContainerServices.json diff --git a/application/Espo/Core/AclManager.php b/application/Espo/Core/AclManager.php index 0ad4e72ff3..5a6860886c 100644 --- a/application/Espo/Core/AclManager.php +++ b/application/Espo/Core/AclManager.php @@ -29,11 +29,11 @@ namespace Espo\Core; -use \Espo\Core\Exceptions\Error; +use Espo\Core\Exceptions\Error; -use \Espo\ORM\Entity; -use \Espo\Entities\User; -use \Espo\Core\Utils\Util; +use Espo\ORM\Entity; +use Espo\Entities\User; +use Espo\Core\Utils\Util; class AclManager { @@ -49,6 +49,8 @@ class AclManager protected $userAclClassName = '\\Espo\\Core\\Acl'; + protected $baseImplementationClassName = '\\Espo\\Core\\Acl\\Base'; + protected $globalRestricton; public function __construct(Container $container) @@ -77,31 +79,22 @@ class AclManager public function getImplementation($scope) { if (empty($this->implementationHashMap[$scope])) { - $normalizedName = Util::normilizeClassName($scope); + $className = $this->getContainer()->get('classFinder')->find('Acl', $scope); + + if (!$className) { + $className = $this->baseImplementationClassName; + } - $className = '\\Espo\\Custom\\Acl\\' . $normalizedName; if (!class_exists($className)) { - $moduleName = $this->metadata->getScopeModuleName($scope); - if ($moduleName) { - $className = '\\Espo\\Modules\\' . $moduleName . '\\Acl\\' . $normalizedName; - } else { - $className = '\\Espo\\Acl\\' . $normalizedName; - } - if (!class_exists($className)) { - $className = '\\Espo\\Core\\Acl\\Base'; - } + throw new Error("{$className} does not exist."); } - if (class_exists($className)) { - $acl = new $className($scope); - $dependencyList = $acl->getDependencyList(); - foreach ($dependencyList as $name) { - $acl->inject($name, $this->getContainer()->get($name)); - } - $this->implementationHashMap[$scope] = $acl; - } else { - throw new Error(); + $acl = new $className($scope); + $dependencyList = $acl->getDependencyList(); + foreach ($dependencyList as $name) { + $acl->inject($name, $this->getContainer()->get($name)); } + $this->implementationHashMap[$scope] = $acl; } return $this->implementationHashMap[$scope]; diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index 6f740b1f95..60c198c54b 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -29,9 +29,11 @@ namespace Espo\Core; +/** + * DI container. Lazy initialization is used. See https://docs.espocrm.com/development/di/. + */ class Container { - private $data = []; public function __construct() @@ -49,6 +51,11 @@ class Container return null; } + public function setUser(\Espo\Entities\User $user) + { + $this->set('user', $user); + } + protected function set($name, $obj) { $this->data[$name] = $obj; @@ -88,7 +95,7 @@ class Container if ($className && class_exists($className)) { - $dependencyList = $metadata->get(['app', 'containerServices', $name, 'dependencyList']) ?? []; + $dependencyList = $this->getServiceDependencyList($name); $dependencyObjectList = []; foreach ($dependencyList as $item) { $dependencyObjectList[] = $this->get($item); @@ -110,7 +117,12 @@ class Container return null; } - public function getServiceClassName(string $name, ?string $default = null) + protected function getServiceDependencyList(string $name) : array + { + return $this->get('metadata')->get(['app', 'containerServices', $name, 'dependencyList']) ?? []; + } + + protected function getServiceClassName(string $name, ?string $default = null) { $metadata = $this->get('metadata'); @@ -221,20 +233,6 @@ class Container ); } - protected function loadServiceFactory() - { - return new \Espo\Core\ServiceFactory( - $this - ); - } - - protected function loadNotificatorFactory() - { - return new \Espo\Core\NotificatorFactory( - $this - ); - } - protected function loadMetadata() { return new \Espo\Core\Utils\Metadata( @@ -243,15 +241,6 @@ class Container ); } - protected function loadLayout() - { - return new \Espo\Core\Utils\Layout( - $this->get('fileManager'), - $this->get('metadata'), - $this->get('user') - ); - } - protected function loadAclManager() { $className = $this->getServiceClassName('acl', '\\Espo\\Core\\AclManager'); @@ -386,9 +375,4 @@ class Container $this ); } - - public function setUser(\Espo\Entities\User $user) - { - $this->set('user', $user); - } } diff --git a/application/Espo/Core/ControllerManager.php b/application/Espo/Core/ControllerManager.php index 225215790f..1351cb01dc 100644 --- a/application/Espo/Core/ControllerManager.php +++ b/application/Espo/Core/ControllerManager.php @@ -29,15 +29,11 @@ namespace Espo\Core; -use \Espo\Core\Utils\Util; -use \Espo\Core\Exceptions\NotFound; +use Espo\Core\Utils\Util; +use Espo\Core\Exceptions\NotFound; class ControllerManager { - private $config; - - private $metadata; - private $container; private $controllersHash = null; @@ -46,41 +42,27 @@ class ControllerManager { $this->container = $container; - $this->config = $this->container->get('config'); - $this->metadata = $this->container->get('metadata'); - $this->controllersHash = (object) []; } - protected function getConfig() + protected function getContainer() { - return $this->config; + return $this->container; } - protected function getMetadata() + protected function getControllerClassName(string $name) : string { - return $this->metadata; - } + $className = $this->getContainer()->get('classFinder')->find('Controllers', $name); - protected function getControllerClassName($controllerName) - { - $customClassName = '\\Espo\\Custom\\Controllers\\' . Util::normilizeClassName($controllerName); - if (class_exists($customClassName)) { - $controllerClassName = $customClassName; - } else { - $moduleName = $this->metadata->getScopeModuleName($controllerName); - if ($moduleName) { - $controllerClassName = '\\Espo\\Modules\\' . $moduleName . '\\Controllers\\' . Util::normilizeClassName($controllerName); - } else { - $controllerClassName = '\\Espo\\Controllers\\' . Util::normilizeClassName($controllerName); - } + if (!$className) { + throw new NotFound("Controller '{$name}' does not exist."); } - if (!class_exists($controllerClassName)) { - throw new NotFound("Controller '$controllerName' is not found"); + if (!class_exists($className)) { + throw new NotFound("Class not found for controller '{$name}'."); } - return $controllerClassName; + return $className; } public function createController($name) @@ -129,9 +111,12 @@ class ControllerManager throw new NotFound("Action {$requestMethod} '{$actionName}' does not exist in controller '".$controller->getName()."'."); } - // TODO Remove in 5.1.0 + // TODO Remove in 6.0.0 if ($data instanceof \stdClass) { - if ($this->getMetadata()->get(['app', 'deprecatedControllerActions', $controller->getName(), $primaryActionMethodName])) { + if ( + $this->container->get('metadata')->get( + ['app', 'deprecatedControllerActions', $controller->getName(), $primaryActionMethodName]) + ) { $data = get_object_vars($data); } } diff --git a/application/Espo/Core/CronManager.php b/application/Espo/Core/CronManager.php index 92e1f6ae04..8c998c7347 100644 --- a/application/Espo/Core/CronManager.php +++ b/application/Espo/Core/CronManager.php @@ -342,7 +342,7 @@ class CronManager { $jobName = $job->get('scheduledJobJob'); - $className = $this->getScheduledJobUtil()->get($jobName); + $className = $this->getScheduledJobUtil()->getJobClassName($jobName); if ($className === false) throw new Error("No class name for job {$jobName}."); @@ -386,7 +386,7 @@ class CronManager { $jobName = $job->get('job'); - $className = $this->getScheduledJobUtil()->get($jobName); + $className = $this->getScheduledJobUtil()->getJobClassName($jobName); if ($className === false) throw new Error("No class name for job {$jobName}."); @@ -430,7 +430,7 @@ class CronManager if ($existingJob) continue; } - $className = $this->getScheduledJobUtil()->get($scheduledJob->get('job')); + $className = $this->getScheduledJobUtil()->getJobClassName($scheduledJob->get('job')); if ($className) { if (method_exists($className, 'prepare')) { $implementation = new $className($this->container); diff --git a/application/Espo/Core/EntryPointManager.php b/application/Espo/Core/EntryPointManager.php index fc80a706f3..ecada6ef81 100644 --- a/application/Espo/Core/EntryPointManager.php +++ b/application/Espo/Core/EntryPointManager.php @@ -36,29 +36,9 @@ class EntryPointManager { private $container; - private $fileManager; - - protected $data = null; - - protected $cacheFile = 'data/cache/application/entryPoints.php'; - - protected $allowedMethods = [ - 'run', - ]; - - /** - * @var array - path to entryPoint files - */ - private $paths = [ - 'corePath' => 'application/Espo/EntryPoints', - 'modulePath' => 'application/Espo/Modules/{*}/EntryPoints', - 'customPath' => 'custom/Espo/Custom/EntryPoints', - ]; - public function __construct(\Espo\Core\Container $container) { $this->container = $container; - $this->fileManager = $container->get('fileManager'); } protected function getContainer() @@ -66,21 +46,18 @@ class EntryPointManager return $this->container; } - protected function getFileManager() - { - return $this->fileManager; - } - - public function checkAuthRequired($name) + public function checkAuthRequired(string $name) : bool { $className = $this->getClassName($name); if (!$className) { + echo $name; + die; throw new NotFound(); } return $className::$authRequired; } - public function checkNotStrictAuth($name) + public function checkNotStrictAuth(string $name) : bool { $className = $this->getClassName($name); if (!$className) { @@ -89,7 +66,7 @@ class EntryPointManager return $className::$notStrictAuth; } - public function run($name, $data = array()) + public function run(string $name, array $data = []) { $className = $this->getClassName($name); if (!$className) { @@ -100,25 +77,9 @@ class EntryPointManager $entryPoint->run($data); } - protected function getClassName($name) + protected function getClassName(string $name) : ?string { - $name = Util::normilizeClassName($name); - - if (!isset($this->data)) { - $this->init(); - } - $name = ucfirst($name); - if (isset($this->data[$name])) { - return $this->data[$name]; - } - - return false; - } - - protected function init() - { - $classParser = $this->getContainer()->get('classParser'); - $this->data = $classParser->getData($this->paths, $this->cacheFile, $this->allowedMethods); + return $this->getContainer()->get('classFinder')->find('EntryPoints', $name); } } diff --git a/application/Espo/Core/Loaders/NotificatorFactory.php b/application/Espo/Core/Loaders/NotificatorFactory.php new file mode 100644 index 0000000000..928cacdd5d --- /dev/null +++ b/application/Espo/Core/Loaders/NotificatorFactory.php @@ -0,0 +1,40 @@ +getContainer() + ); + } +} diff --git a/application/Espo/Core/Loaders/SelectManagerFactory.php b/application/Espo/Core/Loaders/SelectManagerFactory.php index 4ba5c09535..04587f96f1 100644 --- a/application/Espo/Core/Loaders/SelectManagerFactory.php +++ b/application/Espo/Core/Loaders/SelectManagerFactory.php @@ -41,7 +41,8 @@ class SelectManagerFactory extends Base $this->getContainer()->get('metadata'), $this->getContainer()->get('config'), $this->getContainer()->get('fieldManagerUtil'), - $this->getContainer()->get('injectableFactory') + $this->getContainer()->get('injectableFactory'), + $this->getContainer()->get('classFinder') ); } } diff --git a/application/Espo/Core/Loaders/ServiceFactory.php b/application/Espo/Core/Loaders/ServiceFactory.php new file mode 100644 index 0000000000..9864f71620 --- /dev/null +++ b/application/Espo/Core/Loaders/ServiceFactory.php @@ -0,0 +1,40 @@ +getContainer() + ); + } +} diff --git a/application/Espo/Core/NotificatorFactory.php b/application/Espo/Core/NotificatorFactory.php index 9622310ab0..34faeb6935 100644 --- a/application/Espo/Core/NotificatorFactory.php +++ b/application/Espo/Core/NotificatorFactory.php @@ -29,28 +29,20 @@ namespace Espo\Core; -use \Espo\Core\Exceptions\Error; - -use \Espo\Core\Utils\Util; -use \Espo\Core\InjectableFactory; +use Espo\Core\Utils\Util; +use Espo\Core\InjectableFactory; +use Espo\Core\Notificators\Base as BaseNotificator; class NotificatorFactory extends InjectableFactory { - public function create($entityType) - { - $normalizedName = Util::normilizeClassName($entityType); + protected $baseClassName = '\\Espo\\Core\\Notificators\\Base'; - $className = '\\Espo\\Custom\\Notificators\\' . $normalizedName; - if (!class_exists($className)) { - $moduleName = $this->getMetadata()->getScopeModuleName($entityType); - if ($moduleName) { - $className = '\\Espo\\Modules\\' . $moduleName . '\\Notificators\\' . $normalizedName; - } else { - $className = '\\Espo\\Notificators\\' . $normalizedName; - } - if (!class_exists($className)) { - $className = '\\Espo\\Core\\Notificators\\Base'; - } + public function create(string $entityType) : BaseNotificator + { + $className = $this->getContainer()->get('classFinder')->find('Notificators', $entityType); + + if (!$className || !class_exists($className)) { + $className = $this->baseClassName; } $obj = $this->createByClassName($className); diff --git a/application/Espo/Core/ORM/EntityManager.php b/application/Espo/Core/ORM/EntityManager.php index d77b99f287..2a556da961 100644 --- a/application/Espo/Core/ORM/EntityManager.php +++ b/application/Espo/Core/ORM/EntityManager.php @@ -29,7 +29,7 @@ namespace Espo\Core\ORM; -use \Espo\Core\Utils\Util; +use Espo\Core\Utils\Util; class EntityManager extends \Espo\ORM\EntityManager { @@ -87,26 +87,18 @@ class EntityManager extends \Espo\ORM\EntityManager return $this->hookManager; } - public function normalizeRepositoryName($name) + public function getRepositoryClassName($name) { - if (empty($this->repositoryClassNameHash[$name])) { - $className = '\\Espo\\Custom\\Repositories\\' . Util::normilizeClassName($name); - if (!class_exists($className)) { - $className = $this->espoMetadata->getRepositoryPath($name); - } - $this->repositoryClassNameHash[$name] = $className; + if (!array_key_exists($name, $this->repositoryClassNameHash)) { + $this->repositoryClassNameHash[$name] = $this->getContainer()->get('classFinder')->find('Repositories', $name); } return $this->repositoryClassNameHash[$name]; } - public function normalizeEntityName($name) + public function getEntityClassName($name) { - if (empty($this->entityClassNameHash[$name])) { - $className = '\\Espo\\Custom\\Entities\\' . Util::normilizeClassName($name); - if (!class_exists($className)) { - $className = $this->espoMetadata->getEntityPath($name); - } - $this->entityClassNameHash[$name] = $className; + if (!array_key_exists($name, $this->entityClassNameHash)) { + $this->entityClassNameHash[$name] = $this->getContainer()->get('classFinder')->find('Entities', $name); } return $this->entityClassNameHash[$name]; } diff --git a/application/Espo/Core/Portal/AclManager.php b/application/Espo/Core/Portal/AclManager.php index 5384e4321d..8c495d63db 100644 --- a/application/Espo/Core/Portal/AclManager.php +++ b/application/Espo/Core/Portal/AclManager.php @@ -29,9 +29,9 @@ namespace Espo\Core\Portal; -use \Espo\ORM\Entity; -use \Espo\Entities\User; -use \Espo\Core\Utils\Util; +use Espo\ORM\Entity; +use Espo\Entities\User; +use Espo\Core\Utils\Util; class AclManager extends \Espo\Core\AclManager { @@ -43,34 +43,27 @@ class AclManager extends \Espo\Core\AclManager protected $userAclClassName = '\\Espo\\Core\\Portal\\Acl'; + protected $baseImplementationClassName = '\\Espo\\Core\\AclPortal\\Base'; + public function getImplementation($scope) { if (empty($this->implementationHashMap[$scope])) { - $normalizedName = Util::normilizeClassName($scope); + $className = $this->getContainer()->get('classFinder')->find('AclPortal', $scope); + + if (!$className) { + $className = $this->baseImplementationClassName; + } - $className = '\\Espo\\Custom\\AclPortal\\' . $normalizedName; if (!class_exists($className)) { - $moduleName = $this->getMetadata()->getScopeModuleName($scope); - if ($moduleName) { - $className = '\\Espo\\Modules\\' . $moduleName . '\\AclPortal\\' . $normalizedName; - } else { - $className = '\\Espo\\AclPortal\\' . $normalizedName; - } - if (!class_exists($className)) { - $className = '\\Espo\\Core\\AclPortal\\Base'; - } + throw new Error("{$className} does not exist."); } - if (class_exists($className)) { - $acl = new $className($scope); - $dependencyList = $acl->getDependencyList(); - foreach ($dependencyList as $name) { - $acl->inject($name, $this->getContainer()->get($name)); - } - $this->implementationHashMap[$scope] = $acl; - } else { - throw new Error(); + $acl = new $className($scope); + $dependencyList = $acl->getDependencyList(); + foreach ($dependencyList as $name) { + $acl->inject($name, $this->getContainer()->get($name)); } + $this->implementationHashMap[$scope] = $acl; } return $this->implementationHashMap[$scope]; @@ -113,7 +106,8 @@ class AclManager extends \Espo\Core\AclManager $fieldManager = $this->getContainer()->get('fieldManagerUtil'); $portal = $this->getPortal(); - $this->tableHashMap[$key] = new $this->tableClassName($user, $portal, $config, $fileManager, $metadata, $fieldManager); + $this->tableHashMap[$key] = new $this->tableClassName( + $user, $portal, $config, $fileManager, $metadata, $fieldManager); } return $this->tableHashMap[$key]; diff --git a/application/Espo/Core/Portal/Container.php b/application/Espo/Core/Portal/Container.php index a1e08aa835..89e0568109 100644 --- a/application/Espo/Core/Portal/Container.php +++ b/application/Espo/Core/Portal/Container.php @@ -31,12 +31,24 @@ namespace Espo\Core\Portal; class Container extends \Espo\Core\Container { - public function getServicePortalClassName(string $name, ?string $default = null) + protected function getServiceClassName(string $name, ?string $default = null) + { + return $this->get('metadata')->get(['app', 'portalContainerServices', $name, 'className']) ?? + parent::getServiceClassName($name, $default); + } + + protected function getServicePortalClassName(string $name, ?string $default = null) { $metadata = $this->get('metadata'); return $metadata->get(['app', 'portalContainerServices', $name, 'className'], $default); } + protected function getServiceDependencyList(string $name) : array + { + return $this->get('metadata')->get(['app', 'portalContainerServices', $name, 'dependencyList']) ?? + parent::getServiceDependencyList($name); + } + protected function getServiceMainClassName(string $name, ?string $default = null) { return parent::getServiceClassName($name, $default); @@ -76,15 +88,6 @@ class Container extends \Espo\Core\Container ); } - protected function loadLayout() - { - return new \Espo\Core\Portal\Utils\Layout( - $this->get('fileManager'), - $this->get('metadata'), - $this->get('user') - ); - } - protected function loadLanguage() { $language = new \Espo\Core\Portal\Utils\Language( diff --git a/application/Espo/Core/SelectManagerFactory.php b/application/Espo/Core/SelectManagerFactory.php index b7f2fb1418..6deda4429f 100644 --- a/application/Espo/Core/SelectManagerFactory.php +++ b/application/Espo/Core/SelectManagerFactory.php @@ -29,10 +29,13 @@ namespace Espo\Core; -use \Espo\Core\Exceptions\Error; -use \Espo\Core\Utils\Util; +use Espo\Core\Exceptions\Error; +use Espo\Core\Utils\Util; -use \Espo\Core\InjectableFactory; +use Espo\Core\InjectableFactory; +use Espo\Core\SelectManagers\Base as BaseSelectManager; +use Espo\Entities\User; +use Espo\Core\ORM\EntityManager; class SelectManagerFactory { @@ -46,18 +49,22 @@ class SelectManagerFactory private $injectableFactory; - private $FieldManagerUtil; + private $fieldManagerUtil; + + private $classFinder; + + protected $baseClassName = '\\Espo\\Core\\SelectManagers\\Base'; public function __construct( - $entityManager, - \Espo\Entities\User $user, + EntityManager $entityManager, + User $user, Acl $acl, AclManager $aclManager, Utils\Metadata $metadata, Utils\Config $config, Utils\FieldManagerUtil $fieldManagerUtil, - InjectableFactory $injectableFactory - ) + InjectableFactory $injectableFactory, + Utils\ClassFinder $classFinder) { $this->entityManager = $entityManager; $this->user = $user; @@ -67,23 +74,15 @@ class SelectManagerFactory $this->config = $config; $this->fieldManagerUtil = $fieldManagerUtil; $this->injectableFactory = $injectableFactory; + $this->classFinder = $classFinder; } - public function create(string $entityType, ?\Espo\Entities\User $user = null) : \Espo\Core\SelectManagers\Base + public function create(string $entityType, ?User $user = null) : BaseSelectManager { - $normalizedName = Util::normilizeClassName($entityType); + $className = $this->classFinder->find('SelectManagers', $entityType); - $className = '\\Espo\\Custom\\SelectManagers\\' . $normalizedName; - if (!class_exists($className)) { - $moduleName = $this->metadata->getScopeModuleName($entityType); - if ($moduleName) { - $className = '\\Espo\\Modules\\' . $moduleName . '\\SelectManagers\\' . $normalizedName; - } else { - $className = '\\Espo\\SelectManagers\\' . $normalizedName; - } - if (!class_exists($className)) { - $className = '\\Espo\\Core\\SelectManagers\\Base'; - } + if (!$className || !class_exists($className)) { + $className = $this->baseClassName; } if ($user) { diff --git a/application/Espo/Core/Utils/ClassFinder.php b/application/Espo/Core/Utils/ClassFinder.php index 006f9eff52..d761aa566b 100644 --- a/application/Espo/Core/Utils/ClassFinder.php +++ b/application/Espo/Core/Utils/ClassFinder.php @@ -51,17 +51,29 @@ class ClassFinder */ 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; - + $map = $this->getMap($category); + $className = $map[$name] ?? null; return $className; } + /** + * Get [name => class-name] map. + */ + public function getMap(string $category) : array + { + if (!array_key_exists($category, $this->dataHash)) { + $this->load($category); + } + return $this->dataHash[$category] ?? []; + } + + protected function load(string $category) + { + $path = $this->buildPaths($category); + $cacheFile = $this->buildCacheFilePath($category); + $this->dataHash[$category] = $this->classParser->getData($path, $cacheFile); + } + protected function buildPaths(string $category) : array { $paths = []; @@ -73,7 +85,7 @@ class ClassFinder protected function buildCacheFilePath(string $category) : string { - $path = 'data/cache/application/' . str_replace('/', '_', strtolower($category)) . '.php'; + $path = 'data/cache/application/classmap_' . str_replace('/', '_', strtolower($category)) . '.php'; return $path; } } diff --git a/application/Espo/Core/Utils/Metadata.php b/application/Espo/Core/Utils/Metadata.php index cd8e89d3c2..ec1265e69f 100644 --- a/application/Espo/Core/Utils/Metadata.php +++ b/application/Espo/Core/Utils/Metadata.php @@ -567,28 +567,6 @@ class Metadata return (bool) $result; } - /** - * Get Entity path, ex. Espo.Entities.Account or Modules\Crm\Entities\MyModule - * - * @param string $entityName - * @param bool $delim - delimiter - * - * @return string - */ - public function getEntityPath($entityName, $delim = '\\') - { - $path = $this->getScopePath($entityName, $delim); - - return implode($delim, array($path, 'Entities', Util::normilizeClassName(ucfirst($entityName)))); - } - - public function getRepositoryPath($entityName, $delim = '\\') - { - $path = $this->getScopePath($entityName, $delim); - - return implode($delim, array($path, 'Repositories', Util::normilizeClassName(ucfirst($entityName)))); - } - /** * Load modules * @@ -638,27 +616,6 @@ class Metadata return $this->get('scopes.' . $scopeName . '.module', false); } - /** - * Get Scope path, ex. "Modules/Crm" for Account - * - * @param string $scopeName - * @param string $delim - delimiter - * - * @return string - */ - public function getScopePath($scopeName, $delim = '/') - { - $moduleName = $this->getScopeModuleName($scopeName); - - $path = ($moduleName !== false) ? 'Espo/Modules/'.$moduleName : 'Espo'; - - if ($delim != '/') { - $path = str_replace('/', $delim, $path); - } - - return $path; - } - /** * Clear metadata variables when reload meta * diff --git a/application/Espo/Core/Utils/ScheduledJob.php b/application/Espo/Core/Utils/ScheduledJob.php index 665a2cb297..421c638e1e 100644 --- a/application/Espo/Core/Utils/ScheduledJob.php +++ b/application/Espo/Core/Utils/ScheduledJob.php @@ -37,14 +37,8 @@ class ScheduledJob private $systemUtil; - protected $data = null; - - protected $cacheFile = 'data/cache/application/jobs.php'; - protected $cronFile = 'cron.php'; - protected $allowedMethod = 'run'; - /** * Period to check if crontab is configured properly * @@ -52,15 +46,6 @@ class ScheduledJob */ protected $checkingCronPeriod = '25 hours'; - /** - * @var array - path to cron job files - */ - private $paths = [ - 'corePath' => 'application/Espo/Jobs', - 'modulePath' => 'application/Espo/Modules/{*}/Jobs', - 'customPath' => 'custom/Espo/Custom/Jobs', - ]; - protected $cronSetup = [ 'linux' => '* * * * * cd {DOCUMENT_ROOT}; {PHP-BIN-DIR} -f {CRON-FILE} > /dev/null 2>&1', 'windows' => '{PHP-BINARY} -f {FULL-CRON-PATH}', @@ -89,87 +74,18 @@ class ScheduledJob return $this->systemUtil; } - public function getMethodName() - { - return $this->allowedMethod; - } - - /** - * Get list of all jobs - * - * @return array - */ - public function getAll() - { - if (!isset($this->data)) { - $this->init(); - } - - return $this->data; - } - - /** - * Get class name of a job by name - * - * @param string $name - * @return string - */ - public function get($name) - { - return $this->getClassName($name); - } - public function getAvailableList() { - $data = $this->getAll(); - - $list = array_keys($data); - + $map = $this->getContainer()->get('classFinder')->getMap('Jobs'); + $list = array_keys($map); return $list; } - /** - * Get list of all job names - * - * @return array - */ - public function getAllNamesOnly() + public function getJobClassName(string $name) : ?string { - $data = $this->getAll(); - - $namesOnly = array_keys($data); - - return $namesOnly; - } - - /** - * Get class name of a job - * - * @param string $name - * @return string - */ - protected function getClassName($name) - { - $name = Util::normilizeClassName($name); - - $data = $this->getAll(); - $name = ucfirst($name); - if (isset($data[$name])) { - return $data[$name]; - } - - return false; - } - - /** - * Load scheduler classes. It loads from ...Jobs, ex. \Espo\Jobs - * @return null - */ - protected function init() - { - $classParser = $this->getContainer()->get('classParser'); - $this->data = $classParser->getData($this->paths, $this->cacheFile, [$this->allowedMethod]); + $className = $this->getContainer()->get('classFinder')->find('Jobs', $name); + return $className; } public function getSetupMessage() @@ -194,14 +110,14 @@ class ScheduledJob $command = str_replace('{'.$name.'}', $value, $command); } - return array( + return [ 'message' => $message, 'command' => $command, - ); + ]; } /** - * Check if crontab is configured properly + * Check if crontab is configured properly. * * @return boolean */ @@ -233,7 +149,6 @@ class ScheduledJob ] ]; - return !!$this->getEntityManager()->getRepository('Job')->findOne($selectParams); } } diff --git a/application/Espo/ORM/EntityFactory.php b/application/Espo/ORM/EntityFactory.php index 913a64cae9..e17767dd2d 100644 --- a/application/Espo/ORM/EntityFactory.php +++ b/application/Espo/ORM/EntityFactory.php @@ -40,9 +40,10 @@ class EntityFactory $this->entityManager = $entityManager; $this->metadata = $metadata; } + public function create($name) { - $className = $this->entityManager->normalizeEntityName($name); + $className = $this->entityManager->getEntityClassName($name); if (!class_exists($className)) { return null; } @@ -53,6 +54,4 @@ class EntityFactory $entity = new $className($defs, $this->entityManager); return $entity; } - } - diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index d30dc315f9..2507abcd93 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -29,7 +29,7 @@ namespace Espo\ORM; -use \Espo\Core\Exceptions\Error; +use Espo\Core\Exceptions\Error; class EntityManager { @@ -234,12 +234,12 @@ class EntityManager return $this->pdo; } - public function normalizeRepositoryName($name) + public function getRepositoryClassName($name) { return $name; } - public function normalizeEntityName($name) + public function getEntityClassName($name) { return $name; } diff --git a/application/Espo/ORM/RepositoryFactory.php b/application/Espo/ORM/RepositoryFactory.php index 25cb9a92a4..e9e06016a2 100644 --- a/application/Espo/ORM/RepositoryFactory.php +++ b/application/Espo/ORM/RepositoryFactory.php @@ -45,7 +45,7 @@ class RepositoryFactory public function create($name) { - $className = $this->entityManager->normalizeRepositoryName($name); + $className = $this->entityManager->getRepositoryClassName($name); if (!class_exists($className)) { $className = $this->defaultRepositoryClassName; @@ -55,11 +55,6 @@ class RepositoryFactory return $repository; } - protected function normalizeName($name) - { - return $name; - } - public function setDefaultRepositoryClassName($defaultRepositoryClassName) { $this->defaultRepositoryClassName = $defaultRepositoryClassName; diff --git a/application/Espo/Resources/metadata/app/containerServices.json b/application/Espo/Resources/metadata/app/containerServices.json index 7499f843c2..22faeb6208 100644 --- a/application/Espo/Resources/metadata/app/containerServices.json +++ b/application/Espo/Resources/metadata/app/containerServices.json @@ -2,5 +2,9 @@ "clientManager": { "className": "\\Espo\\Core\\Utils\\ClientManager", "dependencyList": ["config", "themeManager", "metadata"] + }, + "layout": { + "className": "\\Espo\\Core\\Utils\\Layout", + "dependencyList": ["fileManager", "metadata", "user"] } } \ No newline at end of file diff --git a/application/Espo/Resources/metadata/app/portalContainerServices.json b/application/Espo/Resources/metadata/app/portalContainerServices.json new file mode 100644 index 0000000000..40490fe486 --- /dev/null +++ b/application/Espo/Resources/metadata/app/portalContainerServices.json @@ -0,0 +1,6 @@ +{ + "layout": { + "className": "\\Espo\\Core\\Portal\\Utils\\Layout", + "dependencyList": ["fileManager", "metadata", "user"] + } +} \ No newline at end of file diff --git a/tests/integration/Espo/Core/Utils/ClassFinder.php b/tests/integration/Espo/Core/Utils/ClassFinder.php index 5a6f38455a..1e3dc9fdef 100644 --- a/tests/integration/Espo/Core/Utils/ClassFinder.php +++ b/tests/integration/Espo/Core/Utils/ClassFinder.php @@ -45,6 +45,6 @@ class ClassFinder extends \tests\integration\Core\BaseTestCase $classFinder->find('Services', 'Record') ); - $this->assertTrue(file_exists('data/cache/application/services.php')); + $this->assertTrue(file_exists('data/cache/application/classmap_services.php')); } }