diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index c5286e929e..55cddd5981 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -45,6 +45,7 @@ use Espo\Core\{ Loaders\Config as ConfigLoader, Loaders\Log as LogLoader, Loaders\FileManager as FileManagerLoader, + Loaders\DataManager as DataManagerLoader, Loaders\Metadata as MetadataLoader, }; @@ -62,6 +63,7 @@ class Application 'config' => ConfigLoader::class, 'log' => LogLoader::class, 'fileManager' => FileManagerLoader::class, + 'dataManager' => DataManagerLoader::class, 'metadata' => MetadataLoader::class, ]; diff --git a/application/Espo/Core/Console/CommandManager.php b/application/Espo/Core/Console/CommandManager.php index caa52b56b4..2231ae0891 100644 --- a/application/Espo/Core/Console/CommandManager.php +++ b/application/Espo/Core/Console/CommandManager.php @@ -29,15 +29,23 @@ namespace Espo\Core\Console; -use Espo\Core\Utils\Util; +use Espo\Core\{ + InjectableFactory, + Utils\Metadata, + Utils\Util, +}; + +use Espo\Core\Exceptions\Error; class CommandManager { - protected $container; + protected $injectableFactory; + protected $metadata; - public function __construct(\Espo\Core\Container $container) + public function __construct(InjectableFactory $injectableFactory, Metadata $metadata) { - $this->container = $container; + $this->injectableFactory = $injectableFactory; + $this->metadata = $metadata; } public function run(string $command) @@ -52,24 +60,27 @@ class CommandManager $className = $this->getClassName($command); - $impl = new $className($this->container); - return $impl->run($options, $flagList, $argumentList); + $obj = $this->injectableFactory->create($className); + + return $obj->run($options, $flagList, $argumentList); } - protected function getClassName(string $command) + protected function getClassName(string $command) : string { - $className = '\\Espo\\Core\\Console\\Commands\\' . $command; - $className = $this->container->get('metadata')->get(['app', 'consoleCommands', $command, 'className'], $className); + $className = + $this->metadata->get(['app', 'consoleCommands', $command, 'className']) ?? + 'Espo\\Core\\Console\\Commands\\' . $command; + if (!class_exists($className)) { $msg = "Command '{$command}' does not exist."; echo $msg . "\n"; - throw new \Espo\Core\Exceptions\Error($msg); + throw new Error($msg); } return $className; } - protected function getParams(array $argv) + protected function getParams(array $argv) : array { $argumentList = []; $options = []; diff --git a/application/Espo/Core/Console/Commands/AclCheck.php b/application/Espo/Core/Console/Commands/AclCheck.php index bbf08fff41..8eb350282a 100644 --- a/application/Espo/Core/Console/Commands/AclCheck.php +++ b/application/Espo/Core/Console/Commands/AclCheck.php @@ -29,41 +29,51 @@ namespace Espo\Core\Console\Commands; -class AclCheck extends Base +use Espo\Core\Portal\Application as PortalApplication; +use Espo\Core\Container; + +class AclCheck implements Command { - public function run($options) + protected $container; + + public function __construct(Container $container) + { + $this->container = $container; + } + + public function run(array $options) : ?string { $userId = $options['userId'] ?? null; $scope = $options['scope'] ?? null; $id = $options['id'] ?? null; $action = $options['action'] ?? null; - if (empty($userId)) return; - if (empty($scope)) return; - if (empty($id)) return; + if (empty($userId)) return null; + if (empty($scope)) return null; + if (empty($id)) return null; - $container = $this->getContainer(); + $container = $this->container; $entityManager = $container->get('entityManager'); $user = $entityManager->getEntity('User', $userId); - if (!$user) return; + if (!$user) return null; if ($user->isPortal()) { $portalIdList = $user->getLinkMultipleIdList('portals'); foreach ($portalIdList as $portalId) { - $application = new \Espo\Core\Portal\Application($portalId); + $application = new PortalApplication($portalId); $containerPortal = $application->getContainer(); $entityManager = $containerPortal->get('entityManager'); $user = $entityManager->getEntity('User', $userId); - if (!$user) return; + if (!$user) return null; $result = $this->check($user, $scope, $id, $action, $containerPortal); if ($result) { return 'true'; } } - return; + return null; } if ($this->check($user, $scope, $id, $action, $container)) { @@ -76,12 +86,14 @@ class AclCheck extends Base $entityManager = $container->get('entityManager'); $entity = $entityManager->getEntity($scope, $id); - if (!$entity) return; + if (!$entity) return false; $aclManager = $container->get('aclManager'); if ($aclManager->check($user, $entity, $action)) { return true; } + + return false; } } diff --git a/application/Espo/Core/Console/Commands/AuthTokenCheck.php b/application/Espo/Core/Console/Commands/AuthTokenCheck.php index 5c1450902d..52c1b69b03 100644 --- a/application/Espo/Core/Console/Commands/AuthTokenCheck.php +++ b/application/Espo/Core/Console/Commands/AuthTokenCheck.php @@ -29,27 +29,36 @@ namespace Espo\Core\Console\Commands; -class AuthTokenCheck extends Base +use Espo\Core\ORM\EntityManager; + +class AuthTokenCheck implements Command { - public function run($options, $flagList, $argumentList) + protected $entityManager; + + public function __construct(EntityManager $entityManager) + { + $this->entityManager = $entityManager; + } + + public function run(array $options, array $flagList, array $argumentList) : ?string { $token = $argumentList[0] ?? null; - if (empty($token)) return; + if (empty($token)) return null; - $entityManager = $this->getContainer()->get('entityManager'); + $entityManager = $this->entityManager; $authToken = $entityManager->getRepository('AuthToken')->where([ 'token' => $token, 'isActive' => true, ])->findOne(); - if (!$authToken) return; - if (!$authToken->get('userId')) return; + if (!$authToken) return null; + if (!$authToken->get('userId')) return null; $userId = $authToken->get('userId'); $user = $entityManager->getEntity('User', $userId); - if (!$user) return; + if (!$user) return null; return $user->id; } diff --git a/application/Espo/Core/Console/Commands/Base.php b/application/Espo/Core/Console/Commands/Base.php index ee90641f86..de72375eef 100644 --- a/application/Espo/Core/Console/Commands/Base.php +++ b/application/Espo/Core/Console/Commands/Base.php @@ -29,6 +29,7 @@ namespace Espo\Core\Console\Commands; +/** Deprecated */ abstract class Base { private $container; diff --git a/application/Espo/Core/Console/Commands/ClearCache.php b/application/Espo/Core/Console/Commands/ClearCache.php index f94b01fdc1..a2d3c0f35b 100644 --- a/application/Espo/Core/Console/Commands/ClearCache.php +++ b/application/Espo/Core/Console/Commands/ClearCache.php @@ -29,11 +29,20 @@ namespace Espo\Core\Console\Commands; -class ClearCache extends Base +use Espo\Core\DataManager; + +class ClearCache implements Command { + protected $dataManager; + + public function __construct(DataManager $dataManager) + { + $this->dataManager = $dataManager; + } + public function run() { - $this->getContainer()->get('dataManager')->clearCache(); + $this->dataManager->clearCache(); echo "Cache has been cleared.\n"; } } diff --git a/application/Espo/Core/Loaders/HtmlizerFactory.php b/application/Espo/Core/Console/Commands/Command.php similarity index 87% rename from application/Espo/Core/Loaders/HtmlizerFactory.php rename to application/Espo/Core/Console/Commands/Command.php index 3e80d03e98..80e81c745c 100644 --- a/application/Espo/Core/Loaders/HtmlizerFactory.php +++ b/application/Espo/Core/Console/Commands/Command.php @@ -27,14 +27,9 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -namespace Espo\Core\Loaders; +namespace Espo\Core\Console\Commands; -class HtmlizerFactory extends Base +interface Command { - public function load() - { - return new \Espo\Core\Htmlizer\Factory( - $this->getContainer() - ); - } + } diff --git a/application/Espo/Core/Console/Commands/Extension.php b/application/Espo/Core/Console/Commands/Extension.php index b31b1ed5f7..2784d915f4 100644 --- a/application/Espo/Core/Console/Commands/Extension.php +++ b/application/Espo/Core/Console/Commands/Extension.php @@ -31,11 +31,21 @@ namespace Espo\Core\Console\Commands; use Espo\Core\Exceptions\Error; -class Extension extends Base +use Espo\Core\Container; +use Espo\Core\ExtensionManager; + +class Extension implements Command { protected $extensionManager = null; - public function run($options, $flagList, $argumentList) + protected $container; + + public function __construct(Container $container) + { + $this->container = $container; + } + + public function run(array $options, array $flagList) { if (in_array('u', $flagList)) { // uninstall @@ -180,12 +190,12 @@ class Extension extends Base protected function createExtensionManager() { - return new \Espo\Core\ExtensionManager($this->getContainer()); + return new ExtensionManager($this->container); } protected function getEntityManager() { - return $this->getContainer()->get('entityManager'); + return $this->container->get('entityManager'); } protected function out(string $string) diff --git a/application/Espo/Core/Console/Commands/Import.php b/application/Espo/Core/Console/Commands/Import.php index d607b6ee7d..4e5d7ffa62 100644 --- a/application/Espo/Core/Console/Commands/Import.php +++ b/application/Espo/Core/Console/Commands/Import.php @@ -29,15 +29,24 @@ namespace Espo\Core\Console\Commands; -class Import extends Base +use Espo\Core\ServiceFactory; + +class Import implements Command { - public function run($options, $flagList, $argumentList) + protected $serviceFactory; + + public function __construct(ServiceFactory $serviceFactory) + { + $this->serviceFactory = $serviceFactory; + } + + public function run(array $options, array $flagList) { $id = $options['id'] ?? null; $filePath = $options['file'] ?? null; $paramsId = $options['paramsId'] ?? null; - $service = $this->getContainer()->get('serviceFactory')->create('Import'); + $service = $this->serviceFactory->create('Import'); $forceResume = in_array('resume', $flagList); $revert = in_array('revert', $flagList); diff --git a/application/Espo/Core/Console/Commands/Rebuild.php b/application/Espo/Core/Console/Commands/Rebuild.php index 27f2cb3fcd..8fdb2435e7 100644 --- a/application/Espo/Core/Console/Commands/Rebuild.php +++ b/application/Espo/Core/Console/Commands/Rebuild.php @@ -29,11 +29,20 @@ namespace Espo\Core\Console\Commands; -class Rebuild extends Base +use Espo\Core\DataManager; + +class Rebuild implements Command { + protected $dataManager; + + public function __construct(DataManager $dataManager) + { + $this->dataManager = $dataManager; + } + public function run() { - $this->getContainer()->get('dataManager')->rebuild(); + $this->dataManager->rebuild(); echo "Rebuild has been done.\n"; } } diff --git a/application/Espo/Core/Console/Commands/RunJob.php b/application/Espo/Core/Console/Commands/RunJob.php index 3c404bcf54..d567b4578c 100644 --- a/application/Espo/Core/Console/Commands/RunJob.php +++ b/application/Espo/Core/Console/Commands/RunJob.php @@ -29,9 +29,23 @@ namespace Espo\Core\Console\Commands; -class RunJob extends Base +use Espo\Core\CronManager; +use Espo\Core\Utils\Util; +use Espo\Core\Container; +use Espo\Core\ORM\EntityManager; + +class RunJob implements Command { - public function run($options, $flags, $argumentList) + protected $container; + protected $entityManager; + + public function __construct(Container $container, EntityManager $entityManager) + { + $this->container = $container; + $this->entityManager = $entityManager; + } + + public function run(array $options, array $flags, array $argumentList) { $jobName = $options['job'] ?? null; $targetId = $options['targetId'] ?? null; @@ -43,10 +57,10 @@ class RunJob extends Base if (!$jobName) echo "No job specified.\n"; - $jobName = ucfirst(\Espo\Core\Utils\Util::hyphenToCamelCase($jobName)); + $jobName = ucfirst(Util::hyphenToCamelCase($jobName)); - $container = $this->getContainer(); - $entityManager = $container->get('entityManager'); + $container = $this->container; + $entityManager = $this->entityManager; $job = $entityManager->createEntity('Job', [ 'name' => $jobName, @@ -55,7 +69,7 @@ class RunJob extends Base 'targetId' => $targetId, ]); - $cronManager = new \Espo\Core\CronManager($container); + $cronManager = new CronManager($container); $result = $cronManager->runJob($job); diff --git a/application/Espo/Core/Console/Commands/SetPassword.php b/application/Espo/Core/Console/Commands/SetPassword.php index b019a51a90..e053bc554c 100644 --- a/application/Espo/Core/Console/Commands/SetPassword.php +++ b/application/Espo/Core/Console/Commands/SetPassword.php @@ -29,9 +29,21 @@ namespace Espo\Core\Console\Commands; -class SetPassword extends Base +use Espo\Core\Container; +use Espo\Core\ORM\EntityManager; +use Espo\Core\Utils\PasswordHash; + +class SetPassword implements Command { - public function run($options, $flagList, $argumentList) + protected $entityManager; + + public function __construct(EntityManager $entityManager, PasswordHash $passwordHash) + { + $this->entityManager = $entityManager; + $this->passwordHash = $passwordHash; + } + + public function run(array $options, array $flagList, array $argumentList) { $userName = $argumentList[0] ?? null; @@ -40,7 +52,7 @@ class SetPassword extends Base die; } - $em = $this->getContainer()->get('entityManager'); + $em = $this->entityManager; $user = $em->getRepository('User')->where(['userName' => $userName])->findOne(); @@ -63,7 +75,7 @@ class SetPassword extends Base die; } - $hash = $this->getContainer()->get('passwordHash'); + $hash = $this->passwordHash; $user->set('password', $hash->hash($password)); diff --git a/application/Espo/Core/Console/Commands/Version.php b/application/Espo/Core/Console/Commands/Version.php index 54f81a1f53..05e7ea7500 100644 --- a/application/Espo/Core/Console/Commands/Version.php +++ b/application/Espo/Core/Console/Commands/Version.php @@ -29,11 +29,20 @@ namespace Espo\Core\Console\Commands; -class Version extends Base +use Espo\Core\Utils\Config; + +class Version implements Command { - public function run($options, $flagList, $argumentList) + protected $config; + + public function __construct(Config $config) { - $version = $this->getContainer()->get('config')->get('version'); + $this->config = $config; + } + + public function run() + { + $version = $this->config->get('version'); if (is_null($version)) { return; diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index 4f037c42fc..d06d1818a9 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -53,7 +53,7 @@ class Container } /** - * Obtain a service. + * Obtain a service object. */ public function get(string $name) : ?object { @@ -77,23 +77,25 @@ class Container if (method_exists($this, $loadMethodName)) return true; if ($this->configuration->getLoaderClassName($name)) return true; - if ($this->configuration->getServiceClassName($name)) return true; return false; } - public function set(string $name, object $obj) + /** + * Set a service object. Must be configured as settable. + */ + public function set(string $name, object $object) { if (!$this->configuration->isSettable($name)) { throw new Error("Service '{$name}' is not settable."); } - $this->setForced($name, $obj); + $this->setForced($name, $object); } - protected function setForced(string $name, object $obj) + protected function setForced(string $name, object $object) { - $this->data[$name] = $obj; + $this->data[$name] = $object; } private function load(string $name) @@ -139,9 +141,9 @@ class Container return $this; } - protected function loadSlim() + protected function loadInjectableFactory() { - return new \Espo\Core\Utils\Api\Slim(); + return new InjectableFactory($this); } protected function loadFileStorageManager() @@ -152,37 +154,11 @@ class Container ); } - protected function loadControllerManager() - { - return new \Espo\Core\ControllerManager( - $this->get('injectableFactory'), - $this->get('classFinder'), - $this->get('metadata') // TODO remove - ); - } - protected function loadPreferences() { return $this->get('entityManager')->getEntity('Preferences', $this->get('user')->id); } - protected function loadHookManager() - { - return new \Espo\Core\HookManager( - $this->get('injectableFactory'), - $this->get('fileManager'), - $this->get('metadata'), - $this->get('config') - ); - } - - protected function loadOutput() - { - return new \Espo\Core\Utils\Api\Output( - $this->get('slim') - ); - } - protected function loadDateTime() { return new \Espo\Core\Utils\DateTime( @@ -193,44 +169,6 @@ class Container ); } - protected function loadNumber() - { - return new \Espo\Core\Utils\NumberUtil( - $this->get('config')->get('decimalMark'), - $this->get('config')->get('thousandSeparator') - ); - } - - protected function loadSchema() - { - return new \Espo\Core\Utils\Database\Schema\Schema( - $this->get('config'), - $this->get('metadata'), - $this->get('fileManager'), - $this->get('entityManager'), - $this->get('classParser'), - $this->get('ormMetadata') - ); - } - - protected function loadOrmMetadata() - { - return new \Espo\Core\Utils\Metadata\OrmMetadata( - $this->get('metadata'), - $this->get('fileManager'), - $this->get('config') - ); - } - - protected function loadClassParser() - { - return new \Espo\Core\Utils\File\ClassParser( - $this->get('fileManager'), - $this->get('config'), - $this->get('metadata') - ); - } - protected function loadLanguage() { return new \Espo\Core\Utils\Language( @@ -260,44 +198,4 @@ class Container $this->get('config')->get('useCache') ); } - - protected function loadCrypt() - { - return new \Espo\Core\Utils\Crypt( - $this->get('config') - ); - } - - protected function loadScheduledJob() - { - return new \Espo\Core\Utils\ScheduledJob( - $this - ); - } - - protected function loadDataManager() - { - return new \Espo\Core\DataManager( - $this - ); - } - - protected function loadFieldManager() - { - return new \Espo\Core\Utils\FieldManager( - $this - ); - } - - protected function loadFieldManagerUtil() - { - return new \Espo\Core\Utils\FieldManagerUtil( - $this->get('metadata') - ); - } - - protected function loadInjectableFactory() - { - return new InjectableFactory($this); - } } diff --git a/application/Espo/Core/DataManager.php b/application/Espo/Core/DataManager.php index cad516b815..b4109c96ec 100644 --- a/application/Espo/Core/DataManager.php +++ b/application/Espo/Core/DataManager.php @@ -51,7 +51,7 @@ class DataManager * * @return bool */ - public function rebuild($entityList = null) + public function rebuild($target = null) { $result = $this->clearCache(); @@ -59,7 +59,7 @@ class DataManager $result &= $this->rebuildMetadata(); - $result &= $this->rebuildDatabase($entityList); + $result &= $this->rebuildDatabase($target); $this->rebuildScheduledJobs(); @@ -89,12 +89,12 @@ class DataManager * * @return bool */ - public function rebuildDatabase($entityList = null) + public function rebuildDatabase($target = null) { $schema = $this->getContainer()->get('schema'); try { - $result = $schema->rebuild($entityList); + $result = $schema->rebuild($target); } catch (\Throwable $e) { $result = false; $GLOBALS['log']->error('Fault to rebuild database schema. Details: '. $e->getMessage()); diff --git a/application/Espo/Core/Htmlizer/Factory.php b/application/Espo/Core/Htmlizer/Factory.php index 5429661edd..b630c44ca4 100644 --- a/application/Espo/Core/Htmlizer/Factory.php +++ b/application/Espo/Core/Htmlizer/Factory.php @@ -29,29 +29,23 @@ namespace Espo\Core\Htmlizer; -use Espo\Core\Container; +use Espo\Core\InjectableFactory; class Factory { - protected $container; + protected $injectableFactory; - public function __construct(Container $container) - { - $this->container = $container; + public function __construct(InjectableFactory $injectableFactory) { + $this->injectableFactory = $injectableFactory; } - public function create(bool $skipAcl = false) + public function create(bool $skipAcl = false) : Htmlizer { - return new Htmlizer( - $this->container->get('fileManager'), - $this->container->get('dateTime'), - $this->container->get('number'), - !$skipAcl ? $this->container->get('acl') : null, - $this->container->get('entityManager'), - $this->container->get('metadata'), - $this->container->get('defaultLanguage'), - $this->container->get('config'), - $this->container->get('serviceFactory') - ); + $with = []; + if ($skipAcl) { + $with['acl'] = null; + } + + return $this->injectableFactory->createWith(Htmlizer::class, $with); } } diff --git a/application/Espo/Core/Htmlizer/Htmlizer.php b/application/Espo/Core/Htmlizer/Htmlizer.php index 41758becb7..fe902f02d5 100644 --- a/application/Espo/Core/Htmlizer/Htmlizer.php +++ b/application/Espo/Core/Htmlizer/Htmlizer.php @@ -40,6 +40,7 @@ use Espo\Core\Utils\Language; use Espo\Core\Utils\Metadata; use Espo\ORM\EntityManager; use Espo\Core\ServiceFactory; +use Espo\Core\Acl; use LightnCandy\LightnCandy as LightnCandy; @@ -58,14 +59,13 @@ class Htmlizer FileManager $fileManager, DateTime $dateTime, NumberUtil $number, - $acl = null, + ?Acl $acl = null, ?EntityManager $entityManager = null, ?Metadata $metadata = null, ?Language $language = null, ?Config $config = null, ?ServiceFactory $serviceFactory = null - ) - { + ) { $this->fileManager = $fileManager; $this->dateTime = $dateTime; $this->number = $number; diff --git a/application/Espo/Core/Loaders/AuthenticationFactory.php b/application/Espo/Core/Loaders/AuthenticationFactory.php deleted file mode 100644 index cb9b0d7076..0000000000 --- a/application/Espo/Core/Loaders/AuthenticationFactory.php +++ /dev/null @@ -1,42 +0,0 @@ -getContainer()->get('injectableFactory'), - $this->getContainer()->get('metadata') - ); - return $obj; - } -} diff --git a/application/Espo/Core/Loaders/ClassFinder.php b/application/Espo/Core/Loaders/ClassFinder.php deleted file mode 100644 index 240c74d5cd..0000000000 --- a/application/Espo/Core/Loaders/ClassFinder.php +++ /dev/null @@ -1,40 +0,0 @@ -getContainer()->get('classParser') - ); - } -} diff --git a/application/Espo/Core/Loaders/ConsoleCommandManager.php b/application/Espo/Core/Loaders/ConsoleCommandManager.php index 472880434a..54b467c4ec 100644 --- a/application/Espo/Core/Loaders/ConsoleCommandManager.php +++ b/application/Espo/Core/Loaders/ConsoleCommandManager.php @@ -29,12 +29,25 @@ namespace Espo\Core\Loaders; -class ConsoleCommandManager extends Base +use Espo\Core\{ + InjectableFactory, + Utils\Metadata, + Console\CommandManager, +}; + +class ConsoleCommandManager implements Loader { + protected $injectableFactory; + protected $metadata; + + public function __construct(InjectableFactory $injectableFactory, Metadata $metadata) + { + $this->injectableFactory = $injectableFactory; + $this->metadata = $metadata; + } + public function load() { - return new \Espo\Core\Console\CommandManager( - $this->getContainer() - ); + return new CommandManager($this->injectableFactory, $this->metadata); } } diff --git a/application/Espo/Core/Loaders/Auth2FAFactory.php b/application/Espo/Core/Loaders/DataManager.php similarity index 84% rename from application/Espo/Core/Loaders/Auth2FAFactory.php rename to application/Espo/Core/Loaders/DataManager.php index bfe14cd153..e94da3556d 100644 --- a/application/Espo/Core/Loaders/Auth2FAFactory.php +++ b/application/Espo/Core/Loaders/DataManager.php @@ -29,14 +29,20 @@ namespace Espo\Core\Loaders; -class Auth2FAFactory extends Base +use Espo\Core\{ + Container, + DataManager as DataManagerService, +}; + +class DataManager implements Loader { + public function __construct(Container $container) + { + $this->container = $container; + } + public function load() { - $obj = new \Espo\Core\Utils\Authentication\TwoFA\Utils\Factory( - $this->getContainer()->get('injectableFactory'), - $this->getContainer()->get('metadata') - ); - return $obj; + return new DataManagerService($this->container); } } diff --git a/application/Espo/Core/Loaders/FieldValidatorManager.php b/application/Espo/Core/Loaders/FieldValidatorManager.php deleted file mode 100644 index dc5580fea6..0000000000 --- a/application/Espo/Core/Loaders/FieldValidatorManager.php +++ /dev/null @@ -1,41 +0,0 @@ -getContainer()->get('metadata'), - $this->getContainer()->get('fieldManagerUtil') - ); - } -} diff --git a/application/Espo/Core/Loaders/Hasher.php b/application/Espo/Core/Loaders/Hasher.php deleted file mode 100644 index 25357cc166..0000000000 --- a/application/Espo/Core/Loaders/Hasher.php +++ /dev/null @@ -1,40 +0,0 @@ -getContainer()->get('config') - ); - } -} diff --git a/application/Espo/Core/Loaders/MailSender.php b/application/Espo/Core/Loaders/MailSender.php deleted file mode 100644 index f5b09f65a8..0000000000 --- a/application/Espo/Core/Loaders/MailSender.php +++ /dev/null @@ -1,42 +0,0 @@ -getContainer()->get('config'), - $this->getContainer()->get('entityManager'), - $this->getContainer()->get('serviceFactory') - ); - } -} diff --git a/application/Espo/Core/Loaders/Auth2FAUserFactory.php b/application/Espo/Core/Loaders/NumberUtil.php similarity index 80% rename from application/Espo/Core/Loaders/Auth2FAUserFactory.php rename to application/Espo/Core/Loaders/NumberUtil.php index 12e4cb0b9e..b2f37282b4 100644 --- a/application/Espo/Core/Loaders/Auth2FAUserFactory.php +++ b/application/Espo/Core/Loaders/NumberUtil.php @@ -29,14 +29,22 @@ namespace Espo\Core\Loaders; -class Auth2FAUserFactory extends Base +use Espo\Core\{ + Utils\Config, + Utils\NumberUtil as NumberUtilService, +}; + +class NumberUtil implements Loader { + protected $config; + + public function __construct(Config $config) + { + $this->config = $config; + } + public function load() { - $obj = new \Espo\Core\Utils\Authentication\TwoFA\Utils\UserFactory( - $this->getContainer()->get('injectableFactory'), - $this->getContainer()->get('metadata') - ); - return $obj; + return new NumberUtilService($this->config->get('decimalMark'), $this->config->get('thousandSeparator')); } } diff --git a/application/Espo/Core/Loaders/Totp.php b/application/Espo/Core/Loaders/Totp.php deleted file mode 100644 index 8afed561c4..0000000000 --- a/application/Espo/Core/Loaders/Totp.php +++ /dev/null @@ -1,40 +0,0 @@ -getContainer()->get('config') - ); - } -} diff --git a/application/Espo/Core/Mail/Sender.php b/application/Espo/Core/Mail/Sender.php index 300f1a0bfc..7702ab4bec 100644 --- a/application/Espo/Core/Mail/Sender.php +++ b/application/Espo/Core/Mail/Sender.php @@ -42,6 +42,12 @@ use Laminas\Mail\Transport\Envelope; use Espo\Core\Exceptions\Error; +use Espo\Core\{ + Utils\Config, + ORM\EntityManager, + ServiceFactory, +}; + class Sender { protected $config; @@ -64,7 +70,7 @@ class Sender private $envelope = null; - public function __construct($config, $entityManager, $serviceFactory = null) + public function __construct(Config $config, EntityManager $entityManager, ?ServiceFactory $serviceFactory = null) { $this->config = $config; $this->entityManager = $entityManager; diff --git a/application/Espo/Core/Utils/Api/Output.php b/application/Espo/Core/Utils/Api/Output.php index 6158cdf61b..cbe98d88dd 100644 --- a/application/Espo/Core/Utils/Api/Output.php +++ b/application/Espo/Core/Utils/Api/Output.php @@ -47,10 +47,10 @@ class Output ]; protected $ignorePrintXStatusReasonExceptionClassNameList = [ - 'PDOException' + 'PDOException', ]; - public function __construct(\Espo\Core\Utils\Api\Slim $slim) + public function __construct(Slim $slim) { $this->slim = $slim; } diff --git a/application/Espo/Core/Utils/Authentication/Utils/AuthenticationFactory.php b/application/Espo/Core/Utils/Authentication/Utils/AuthenticationFactory.php index ce5568d991..b61d87d331 100644 --- a/application/Espo/Core/Utils/Authentication/Utils/AuthenticationFactory.php +++ b/application/Espo/Core/Utils/Authentication/Utils/AuthenticationFactory.php @@ -51,9 +51,9 @@ class AuthenticationFactory if (!$className) { $sanitizedName = preg_replace('/[^a-zA-Z0-9]+/', '', $method); - $className = "\\Espo\\Custom\\Core\\Utils\\Authentication\\" . $sanitizedName; + $className = "Espo\\Custom\\Core\\Utils\\Authentication\\" . $sanitizedName; if (!class_exists($className)) { - $className = "\\Espo\\Core\\Utils\\Authentication\\" . $sanitizedName; + $className = "Espo\\Core\\Utils\\Authentication\\" . $sanitizedName; } } diff --git a/application/Espo/Core/Utils/Crypt.php b/application/Espo/Core/Utils/Crypt.php index 2e92e91d7c..44f93efed1 100644 --- a/application/Espo/Core/Utils/Crypt.php +++ b/application/Espo/Core/Utils/Crypt.php @@ -39,7 +39,7 @@ class Crypt private $iv = null; - public function __construct($config) + public function __construct(Config $config) { $this->config = $config; $this->cryptKey = $config->get('cryptKey', ''); @@ -97,6 +97,6 @@ class Crypt public function generateKey() { - return \Espo\Core\Utils\Util::generateSecretKey(); + return Util::generateSecretKey(); } } diff --git a/application/Espo/Core/Utils/Database/Schema/Schema.php b/application/Espo/Core/Utils/Database/Schema/Schema.php index 822b8373dd..96fb96235e 100644 --- a/application/Espo/Core/Utils/Database/Schema/Schema.php +++ b/application/Espo/Core/Utils/Database/Schema/Schema.php @@ -29,8 +29,15 @@ namespace Espo\Core\Utils\Database\Schema; -use Doctrine\DBAL\Types\Type, - Espo\Core\Utils\Util; +use Doctrine\DBAL\Types\Type; + +use Espo\Core\Utils\Config; +use Espo\Core\Utils\Metadata; +use Espo\Core\Utils\File\Manager as FileManager; +use Espo\Core\ORM\EntityManager; +use Espo\Core\Utils\File\ClassParser; +use Espo\Core\Utils\Metadata\OrmMetadata; +use Espo\Core\Utils\Util; class Schema { @@ -75,10 +82,13 @@ class Schema protected $rebuildActionClasses = null; public function __construct( - \Espo\Core\Utils\Config $config, \Espo\Core\Utils\Metadata $metadata, - \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\ORM\EntityManager $entityManager, - \Espo\Core\Utils\File\ClassParser $classParser, \Espo\Core\Utils\Metadata\OrmMetadata $ormMetadata) - { + Config $config, + Metadata $metadata, + FileManager $fileManager, + EntityManager $entityManager, + ClassParser $classParser, + OrmMetadata $ormMetadata + ) { $this->config = $config; $this->metadata = $metadata; $this->fileManager = $fileManager; diff --git a/application/Espo/Core/Utils/Hasher.php b/application/Espo/Core/Utils/Hasher.php index 0319d123f6..79ea9e6991 100644 --- a/application/Espo/Core/Utils/Hasher.php +++ b/application/Espo/Core/Utils/Hasher.php @@ -29,13 +29,18 @@ namespace Espo\Core\Utils; +use Espo\Core\Utils\Config; + +/** + * Hash a string. E.g. hash an email address to use it in opt-out URL to recognize a recipient who clicked opt-out. + */ class Hasher { protected $config; protected $secretKeyParam = 'hashSecretKey'; - public function __construct(\Espo\Core\Utils\Config $config) + public function __construct(Config $config) { $this->config = $config; } diff --git a/application/Espo/Core/Utils/PasswordHash.php b/application/Espo/Core/Utils/PasswordHash.php index afa9bdd3db..ded3ab8e26 100644 --- a/application/Espo/Core/Utils/PasswordHash.php +++ b/application/Espo/Core/Utils/PasswordHash.php @@ -28,6 +28,7 @@ ************************************************************************/ namespace Espo\Core\Utils; + use Espo\Core\Exceptions\Error; class PasswordHash @@ -52,12 +53,9 @@ class PasswordHash } /** - * Get hash of a pawword - * - * @param string $password - * @return string + * Hash a password. */ - public function hash($password, $useMd5 = true) + public function hash(string $password, bool $useMd5 = true) : string { $salt = $this->getSalt(); @@ -72,7 +70,7 @@ class PasswordHash } /** - * Get a salt from config and normalize it + * Get a salt from config and normalize it. * * @return string */ @@ -89,7 +87,7 @@ class PasswordHash } /** - * Convert salt in format in accordance to $saltFormat + * Convert salt in format in accordance to $saltFormat. * * @param string $salt * @return string @@ -100,12 +98,10 @@ class PasswordHash } /** - * Generate a new salt - * - * @return string + * Generate a new salt. */ - public function generateSalt() + public function generateSalt() : string { return substr(md5(uniqid()), 0, 16); } -} \ No newline at end of file +} diff --git a/application/Espo/Core/Utils/ScheduledJob.php b/application/Espo/Core/Utils/ScheduledJob.php index 421c638e1e..45d62a8d23 100644 --- a/application/Espo/Core/Utils/ScheduledJob.php +++ b/application/Espo/Core/Utils/ScheduledJob.php @@ -31,10 +31,16 @@ namespace Espo\Core\Utils; use Espo\Core\Exceptions\NotFound; +use Espo\Core\Container; +use Espo\Core\{ + Utils\ClassFinder, + Utils\Language, + Utils\System, + ORM\EntityManager, +}; + class ScheduledJob { - private $container; - private $systemUtil; protected $cronFile = 'cron.php'; @@ -53,10 +59,17 @@ class ScheduledJob 'default' => '* * * * * cd {DOCUMENT_ROOT}; {PHP-BIN-DIR} -f {CRON-FILE} > /dev/null 2>&1', ]; - public function __construct(\Espo\Core\Container $container) + protected $classFinder; + protected $language; + protected $entityManager; + + public function __construct(ClassFinder $classFinder, EntityManager $entityManager, Language $language) { - $this->container = $container; - $this->systemUtil = new \Espo\Core\Utils\System(); + $this->classFinder = $classFinder; + $this->entityManager = $entityManager; + $this->language = $language; + + $this->systemUtil = new System(); } protected function getContainer() @@ -64,11 +77,6 @@ class ScheduledJob return $this->container; } - protected function getEntityManager() - { - return $this->container->get('entityManager'); - } - protected function getSystemUtil() { return $this->systemUtil; @@ -76,7 +84,7 @@ class ScheduledJob public function getAvailableList() { - $map = $this->getContainer()->get('classFinder')->getMap('Jobs'); + $map = $this->classFinder->getMap('Jobs'); $list = array_keys($map); return $list; } @@ -84,13 +92,13 @@ class ScheduledJob public function getJobClassName(string $name) : ?string { $name = ucfirst($name); - $className = $this->getContainer()->get('classFinder')->find('Jobs', $name); + $className = $this->classFinder->find('Jobs', $name); return $className; } public function getSetupMessage() { - $language = $this->getContainer()->get('language'); + $language = $this->language; $OS = $this->getSystemUtil()->getOS(); $desc = $language->translate('cronSetup', 'options', 'ScheduledJob'); @@ -143,12 +151,12 @@ class ScheduledJob [ ['executeTime>=' => $r1From->format($format)], ['executeTime<='=> $r1To->format($format)], - 'scheduledJob.job' => 'Dummy' + 'scheduledJob.job' => 'Dummy', ] ] ] ]; - return !!$this->getEntityManager()->getRepository('Job')->findOne($selectParams); + return !!$this->entityManager->getRepository('Job')->findOne($selectParams); } } diff --git a/application/Espo/Core/WebSocket/Submission.php b/application/Espo/Core/WebSocket/Submission.php index 21610ee0c9..062886bca9 100644 --- a/application/Espo/Core/WebSocket/Submission.php +++ b/application/Espo/Core/WebSocket/Submission.php @@ -29,11 +29,13 @@ namespace Espo\Core\WebSocket; +use Espo\Core\Utils\Config; + class Submission { protected $config; - public function __construct(\Espo\Core\Utils\Config $config) + public function __construct(Config $config) { $this->config = $config; } diff --git a/application/Espo/Core/Webhook/Manager.php b/application/Espo/Core/Webhook/Manager.php index 44219396d6..3251eb3fb6 100644 --- a/application/Espo/Core/Webhook/Manager.php +++ b/application/Espo/Core/Webhook/Manager.php @@ -29,7 +29,13 @@ namespace Espo\Core\Webhook; -use Espo\ORM\Entity; +use Espo\Core\{ + Utils\Config, + Utils\File\Manager as FileManager, + Utils\FieldManagerUtil, + ORM\EntityManager, + ORM\Entity, +}; class Manager { @@ -45,10 +51,10 @@ class Manager private $data = null; public function __construct( - \Espo\Core\Utils\Config $config, - \Espo\Core\Utils\File\Manager $fileManager, - \Espo\ORM\EntityManager $entityManager, - \Espo\Core\Utils\FieldManagerUtil $fieldManager + Config $config, + FileManager $fileManager, + EntityManager $entityManager, + FieldManagerUtil $fieldManager ) { $this->config = $config; $this->fileManager = $fileManager; diff --git a/application/Espo/Resources/metadata/app/containerServices.json b/application/Espo/Resources/metadata/app/containerServices.json index 02b594de89..a501e1705c 100644 --- a/application/Espo/Resources/metadata/app/containerServices.json +++ b/application/Espo/Resources/metadata/app/containerServices.json @@ -1,4 +1,37 @@ { + "slim": { + "className": "Espo\\Core\\Utils\\Api\\Slim" + }, + "output": { + "className": "Espo\\Core\\Utils\\Api\\Output" + }, + "schema": { + "className": "Espo\\Core\\Utils\\Database\\Schema\\Schema" + }, + "ormMetadata": { + "className": "Espo\\Core\\Utils\\Metadata\\OrmMetadata" + }, + "classParser": { + "className": "Espo\\Core\\Utils\\File\\ClassParser" + }, + "classFinder": { + "className": "Espo\\Core\\Utils\\ClassFinder" + }, + "crypt": { + "className": "Espo\\Core\\Utils\\Crypt" + }, + "number": { + "loaderClassName": "Espo\\Core\\Loaders\\NumberUtil" + }, + "controllerManager": { + "className": "Espo\\Core\\ControllerManager" + }, + "scheduledJob": { + "className": "Espo\\Core\\Utils\\ScheduledJob" + }, + "hookManager": { + "className": "Espo\\Core\\HookManager" + }, "notificatorFactory": { "className": "Espo\\Core\\NotificatorFactory" }, @@ -11,6 +44,36 @@ "themeManager": { "className": "Espo\\Core\\Utils\\ThemeManager" }, + "fieldManager": { + "className": "Espo\\Core\\Utils\\FieldManager" + }, + "fieldManagerUtil": { + "className": "Espo\\Core\\Utils\\FieldManagerUtil" + }, + "mailSender": { + "className": "Espo\\Core\\Mail\\Sender" + }, + "htmlizerFactory": { + "className": "Espo\\Core\\Htmlizer\\Factory" + }, + "fieldValidatorManager": { + "className": "Espo\\Core\\Utils\\FieldValidatorManager" + }, + "hasher": { + "className": "Espo\\Core\\Utils\\Hasher" + }, + "authenticationFactory": { + "className": "Espo\\Core\\Utils\\Authentication\\Utils\\AuthenticationFactory" + }, + "auth2FAFactory": { + "className": "Espo\\Core\\Utils\\Authentication\\TwoFA\\Utils\\Factory" + }, + "auth2FAUserFactory": { + "className": "Espo\\Core\\Utils\\Authentication\\TwoFA\\Utils\\UserFactory" + }, + "totp": { + "className": "Espo\\Core\\Utils\\Authentication\\TwoFA\\Utils\\Totp" + }, "user": { "settable": true }