diff --git a/application/Espo/Core/Loaders/BaseLanguage.php b/application/Espo/Core/Loaders/BaseLanguage.php index fbc7023f45..75c7cd259b 100644 --- a/application/Espo/Core/Loaders/BaseLanguage.php +++ b/application/Espo/Core/Loaders/BaseLanguage.php @@ -31,39 +31,33 @@ namespace Espo\Core\Loaders; use Espo\Core\{ Container\Loader, - Utils\Metadata, Utils\Config, - Utils\File\Manager as FileManager, Utils\Language as LanguageService, - Utils\DataCache, + InjectableFactory, }; class BaseLanguage implements Loader { - protected $fileManager; + private $injectableFactory; protected $config; - protected $metadata; - - protected $dataCache; - - public function __construct(FileManager $fileManager, Config $config, Metadata $metadata, DataCache $dataCache) + public function __construct(InjectableFactory $injectableFactory, Config $config) { - $this->fileManager = $fileManager; + $this->injectableFactory = $injectableFactory; $this->config = $config; - $this->metadata = $metadata; - $this->dataCache = $dataCache; } public function load(): LanguageService { - return new LanguageService( - 'en_US', - $this->fileManager, - $this->metadata, - $this->dataCache, - $this->config->get('useCache') ?? false - ); + return $this->injectableFactory->createWith(LanguageService::class, [ + 'language' => $this->getLanguage(), + 'useCache' => $this->config->get('useCache') ?? false, + ]); + } + + protected function getLanguage(): string + { + return 'en_US'; } } diff --git a/application/Espo/Core/Loaders/DefaultLanguage.php b/application/Espo/Core/Loaders/DefaultLanguage.php index fedf4f9c5a..bc773f1123 100644 --- a/application/Espo/Core/Loaders/DefaultLanguage.php +++ b/application/Espo/Core/Loaders/DefaultLanguage.php @@ -29,20 +29,12 @@ namespace Espo\Core\Loaders; -use Espo\Core\{ - Utils\Language as LanguageService, -}; +use Espo\Core\Utils\Language as LanguageService; class DefaultLanguage extends BaseLanguage { - public function load(): LanguageService + protected function getLanguage(): string { - return new LanguageService( - LanguageService::detectLanguage($this->config), - $this->fileManager, - $this->metadata, - $this->dataCache, - $this->config->get('useCache') ?? false - ); + return LanguageService::detectLanguage($this->config); } } diff --git a/application/Espo/Core/Loaders/Language.php b/application/Espo/Core/Loaders/Language.php index 46c8b39778..d6ab928f16 100644 --- a/application/Espo/Core/Loaders/Language.php +++ b/application/Espo/Core/Loaders/Language.php @@ -31,49 +31,36 @@ namespace Espo\Core\Loaders; use Espo\Core\{ Container\Loader, - Utils\Metadata, Utils\Config, - Utils\File\Manager as FileManager, Utils\Language as LanguageService, - Utils\DataCache, + InjectableFactory, }; use Espo\Entities\Preferences; class Language implements Loader { - protected $fileManager; + private $injectableFactory; - protected $config; + private $config; - protected $metadata; - - protected $dataCache; - - protected $preferences; + private $preferences; public function __construct( - FileManager $fileManager, + InjectableFactory $injectableFactory, Config $config, - Metadata $metadata, - DataCache $dataCache, Preferences $preferences ) { - $this->fileManager = $fileManager; + $this->injectableFactory = $injectableFactory; $this->config = $config; - $this->metadata = $metadata; - $this->dataCache = $dataCache; $this->preferences = $preferences; } public function load(): LanguageService { - return new LanguageService( - LanguageService::detectLanguage($this->config, $this->preferences), - $this->fileManager, - $this->metadata, - $this->dataCache, - $this->config->get('useCache') ?? false - ); + return $this->injectableFactory->createWith(LanguageService::class, [ + 'language' => LanguageService::detectLanguage($this->config, $this->preferences), + 'useCache' => $this->config->get('useCache') ?? false, + ]); } } diff --git a/application/Espo/Core/Loaders/Metadata.php b/application/Espo/Core/Loaders/Metadata.php index dbc39a664d..4d0968e0e3 100644 --- a/application/Espo/Core/Loaders/Metadata.php +++ b/application/Espo/Core/Loaders/Metadata.php @@ -32,30 +32,26 @@ namespace Espo\Core\Loaders; use Espo\Core\{ Container\Loader, Utils\Metadata as MetadataService, - Utils\File\Manager as FileManager, - Utils\DataCache, Utils\Config, + InjectableFactory, }; class Metadata implements Loader { - private $fileManager; - - private $dataCache; + private $injectableFactory; private $config; - public function __construct(FileManager $fileManager, DataCache $dataCache, Config $config) + public function __construct(InjectableFactory $injectableFactory, Config $config) { - $this->fileManager = $fileManager; - $this->dataCache = $dataCache; + $this->injectableFactory = $injectableFactory; $this->config = $config; } public function load(): MetadataService { - $useCache = $this->config->get('useCache') ?? false; - - return new MetadataService($this->fileManager, $this->dataCache, $useCache); + return $this->injectableFactory->createWith(MetadataService::class, [ + 'useCache' => $this->config->get('useCache') ?? false, + ]); } } diff --git a/application/Espo/Core/Portal/Loaders/Language.php b/application/Espo/Core/Portal/Loaders/Language.php index 130f145c14..ea7696b065 100644 --- a/application/Espo/Core/Portal/Loaders/Language.php +++ b/application/Espo/Core/Portal/Loaders/Language.php @@ -31,11 +31,9 @@ namespace Espo\Core\Portal\Loaders; use Espo\Core\{ Container\Loader, - Utils\Metadata, Utils\Config, - Utils\File\Manager as FileManager, Portal\Utils\Language as LanguageService, - Utils\DataCache, + InjectableFactory, }; use Espo\Entities\{ @@ -45,43 +43,32 @@ use Espo\Entities\{ class Language implements Loader { - private $fileManager; + private $injectableFactory; private $config; - private $metadata; - - private $dataCache; - private $preferences; private $portal; public function __construct( - FileManager $fileManager, + InjectableFactory $injectableFactory, Config $config, - Metadata $metadata, - DataCache $dataCache, Preferences $preferences, Portal $portal ) { - $this->fileManager = $fileManager; + $this->injectableFactory = $injectableFactory; $this->config = $config; - $this->metadata = $metadata; - $this->dataCache = $dataCache; $this->preferences = $preferences; $this->portal = $portal; } public function load(): LanguageService { - $language = new LanguageService( - LanguageService::detectLanguage($this->config, $this->preferences), - $this->fileManager, - $this->metadata, - $this->dataCache, - $this->config->get('useCache') - ); + $language = $this->injectableFactory->createWith(LanguageService::class, [ + 'language' => LanguageService::detectLanguage($this->config, $this->preferences), + 'useCache' => $this->config->get('useCache') ?? false, + ]); $language->setPortal($this->portal); diff --git a/application/Espo/Core/Utils/Language.php b/application/Espo/Core/Utils/Language.php index a6828214de..a141cd13f8 100644 --- a/application/Espo/Core/Utils/Language.php +++ b/application/Espo/Core/Utils/Language.php @@ -75,29 +75,20 @@ class Language ?string $language, FileManager $fileManager, Metadata $metadata, - DataCache $dataCache = null, + FileUnifier $unifier, + DataCache $dataCache, bool $useCache = false, bool $noCustom = false ) { - if ($language) { - $this->currentLanguage = $language; - } - else { - $this->currentLanguage = $this->defaultLanguage; - } + $this->currentLanguage = $language ?? $this->defaultLanguage; $this->fileManager = $fileManager; $this->metadata = $metadata; + $this->unifier = $unifier; $this->dataCache = $dataCache; $this->useCache = $useCache; $this->noCustom = $noCustom; - - if (!$this->dataCache) { - $this->useCache = false; - } - - $this->unifier = new FileUnifier($this->fileManager, $this->metadata); } public function getLanguage(): string diff --git a/tests/unit/Espo/Core/Utils/LanguageTest.php b/tests/unit/Espo/Core/Utils/LanguageTest.php index 0540b94620..d369acd62e 100644 --- a/tests/unit/Espo/Core/Utils/LanguageTest.php +++ b/tests/unit/Espo/Core/Utils/LanguageTest.php @@ -35,6 +35,7 @@ use Espo\Core\Utils\Metadata; use Espo\Core\Utils\Language; use Espo\Core\Utils\DataCache; use Espo\Core\Utils\File\Manager as FileManager; +use Espo\Core\Utils\File\Unifier; class LanguageTest extends \PHPUnit\Framework\TestCase { @@ -50,7 +51,7 @@ class LanguageTest extends \PHPUnit\Framework\TestCase 'customPath' => 'tests/unit/testData/Utils/I18n/Espo/Custom/Resources/i18n/{language}', ]; - protected function setUp() : void + protected function setUp(): void { $this->fileManager = new FileManager(); @@ -66,7 +67,16 @@ class LanguageTest extends \PHPUnit\Framework\TestCase ] )); - $this->object = new Language(null, $this->fileManager, $this->metadata, $this->dataCache, false); + $this->unifier = new Unifier($this->fileManager, $this->metadata); + + $this->object = new Language( + null, + $this->fileManager, + $this->metadata, + $this->unifier, + $this->dataCache, + false + ); $this->reflection = new ReflectionHelper($this->object); @@ -74,7 +84,7 @@ class LanguageTest extends \PHPUnit\Framework\TestCase $this->reflection->setProperty('currentLanguage', 'en_US'); } - protected function tearDown() : void + protected function tearDown(): void { $this->object = NULL; }