/** * Load config for application modules * * @return void */ public function load() { $app = \Magelight\App::getInstance(); $loader = \Magelight\Components\Loaders\Config::forge(); $loader->loadConfig($app->getAppDir() . DS . 'etc' . DS . 'config.xml'); $this->config = $loader->getConfig(); $modulesConfigString = $this->getConfigBool('global/app/cache_modules_config', false) ? $this->cache()->get($this->buildCacheKey('modules_config')) : false; /* Loading modules config */ if (!$modulesConfigString) { $loader->setConfig($this->config); foreach (array_reverse($app->getModuleDirectories()) as $modulesDir) { foreach (\Magelight\Components\Modules::getInstance()->getActiveModules() as $module) { $filename = $loader->getModulesConfigFilePath($modulesDir, $module); if ($filename) { $loader->loadConfig($filename); } } } $modulesConfig = $loader->getConfig(); if ($this->getConfigBool('global/app/cache_modules_config', false)) { $this->cache()->set($this->buildCacheKey('modules_config'), $modulesConfig->asXML(), 3600); } $this->config = $modulesConfig; } else { $this->config = simplexml_load_string($modulesConfigString); } unset($loader); }
public function testLoadFromCacheMiss() { $appConfig = '<config><global> <app> <cache_modules_config>1</cache_modules_config> </app> </global></config>'; $expectedConfig = new \SimpleXMLElement('<config><global> <app> <cache_modules_config>0</cache_modules_config> <framework_config_data>framework</framework_config_data> <app_module_config_data>app.domain</app_module_config_data> <bool_data_true>1</bool_data_true> <bool_data_false>0</bool_data_false> <int_data>123</int_data> <float_data>123.456</float_data> <array_data> <node_1>data</node_1> <node_2>data_node_2</node_2> </array_data> <string_data>some string</string_data> <config_with_attribute attribute_1="attribute_value">config_string</config_with_attribute> </app> </global></config>'); $this->appMock->expects($this->once())->method('getAppDir')->will($this->returnValue('/app')); $this->appMock->expects($this->once())->method('getModuleDirectories')->will($this->returnValue(['/app/modules', '/framework/modules'])); $this->configLoaderMock->expects($this->at(0))->method('loadConfig')->with('/app' . DS . 'etc' . DS . 'config.xml'); $this->configLoaderMock->expects($this->at(1))->method('getConfig')->will($this->returnValue(new \SimpleXMLElement($appConfig))); $this->modulesMock->expects($this->any())->method('getActiveModules')->will($this->returnValue([['name' => 'Magelight_Module', 'path' => 'Magelight\\Module'], ['name' => 'App_Module', 'path' => 'App\\Module']])); $this->configLoaderMock->expects($this->any())->method('getModulesConfigFilePath')->will($this->returnValueMap([['/framework/modules', ['name' => 'Magelight_Module', 'path' => 'Magelight\\Module'], '/framework/modules/Magelight\\Module' . DS . 'etc' . DS . 'config.xml'], ['/app/modules', ['name' => 'Magelight_Module', 'path' => 'Magelight\\Module'], null], ['/framework/modules', ['name' => 'App_Module', 'path' => 'App\\Module'], null], ['/app/modules', ['name' => 'App_Module', 'path' => 'App\\Module'], '/app/modules/App\\Module' . DS . 'etc' . DS . 'config.xml']])); $this->configLoaderMock->expects($this->at(3))->method('loadConfig')->with('/framework/modules/Magelight\\Module' . DS . 'etc' . DS . 'config.xml'); $this->configLoaderMock->expects($this->at(7))->method('loadConfig')->with('/app/modules/App\\Module' . DS . 'etc' . DS . 'config.xml'); $this->configLoaderMock->expects($this->at(8))->method('getConfig')->will($this->returnValue($expectedConfig)); $this->cacheAdapterMock->expects($this->once())->method('set')->with($this->config->buildCacheKey('modules_config'), $expectedConfig->asXML(), 3600); $this->config->load(); $this->assertEquals('framework', $this->config->getConfigString('/global/app/framework_config_data')); $this->assertEquals('app.domain', $this->config->getConfigString('/global/app/app_module_config_data')); $this->assertTrue($this->config->getConfigBool('/global/app/bool_data_true')); $this->assertFalse($this->config->getConfigBool('/global/app/bool_data_false')); $this->assertInternalType('int', $this->config->getConfigInt('/global/app/int_data')); $this->assertEquals(123, $this->config->getConfigInt('/global/app/int_data')); $this->assertInternalType('float', $this->config->getConfigFloat('/global/app/float_data')); $this->assertEquals(123.456, $this->config->getConfigFloat('/global/app/float_data')); $this->assertInternalType('array', $this->config->getConfigArray('/global/app/array_data')); $this->assertEquals(['node_1' => 'data', 'node_2' => 'data_node_2'], $this->config->getConfigArray('/global/app/array_data')); $this->assertInternalType('string', $this->config->getConfigString('/global/app/string_data')); $this->assertEquals('some string', $this->config->getConfigString('/global/app/string_data')); $this->assertInternalType('string', $this->config->getConfigAttribute('/global/app/config_with_attribute', 'attribute_1')); $this->assertEquals('attribute_value', $this->config->getConfigAttribute('/global/app/config_with_attribute', 'attribute_1')); $this->assertInternalType('string', $this->config->getConfigString('/global/app/unexistent_node', 'default_value')); $this->assertEquals('default_value', $this->config->getConfigString('/global/app/unexistent_node', 'default_value')); $this->assertInternalType('array', $this->config->getConfigSet('/global/app')); $this->assertInternalType('string', $this->config->getConfigXmlString()); $this->assertEquals($expectedConfig->asXML(), $this->config->getConfigXmlString()); }
public function setUp() { $this->modulesMock = $this->getMock(\Magelight\Components\Modules::class, [], [], '', false); $this->configMock = $this->getMock(\Magelight\Config::class, [], [], '', false); $this->dbMock = $this->getMock(\Magelight\Db\Mysql\Adapter::class, [], [], '', false); $this->appMock = $this->getMockForAbstractClass(App::class, [], '', false, false, true, ['db']); \Magelight\Components\Modules::forgeMock($this->modulesMock); \Magelight\Config::forgeMock($this->configMock); \Magelight\Db\Mysql\Adapter::forgeMock($this->dbMock); \Magelight\App::forgeMock($this->appMock); }
/** * Load application routes * * @return Routes */ public function loadRoutes() { $modules = \Magelight\Components\Modules::getInstance()->getActiveModules(); foreach (array_reverse(\Magelight\App::getInstance()->getModuleDirectories()) as $modulesPath) { foreach ($modules as $module) { $filename = $modulesPath . DS . str_replace('/', DS, $module['path']) . DS . 'etc' . DS . 'routes.xml'; if (file_exists($filename)) { $xml = simplexml_load_file($filename); $this->parseModuleRoutes($xml, $module['path']); } } } $this->routes = array_reverse($this->routes, true); return $this; }
/** * Set up before test */ public function setUp() { $this->app = $this->getMockForAbstractClass(App::class, [], '', false, false, true, []); $this->modulesMock = $this->getMock(\Magelight\Components\Modules::class, [], [], '', false); $this->configMock = $this->getMock(\Magelight\Config::class, [], [], '', false); $this->routerMock = $this->getMock(\Magelight\Components\Router::class, [], [], '', false); $this->sessionMock = $this->getMock(\Magelight\Http\Session::class, [], [], '', false); $this->translatorMock = $this->getMock(\Magelight\I18n\Translator::class, [], [], '', false); $this->eventManagerMock = $this->getMock(\Magelight\Event\Manager::class, [], [], '', false); \Magelight\Components\Modules::forgeMock($this->modulesMock); \Magelight\Config::forgeMock($this->configMock); \Magelight\Components\Router::forgeMock($this->routerMock); \Magelight\Http\Session::forgeMock($this->sessionMock); \Magelight\I18n\Translator::forgeMock($this->translatorMock); \Magelight\Event\Manager::forgeMock($this->eventManagerMock); }
/** * Load translations * * @param string $lang */ public function loadTranslations($lang) { if (!is_string($lang)) { throw new \InvalidArgumentException('Language must be a string'); } foreach (array_reverse(\Magelight\App::getInstance()->getModuleDirectories()) as $modulesDir) { foreach (\Magelight\Components\Modules::getInstance()->getActiveModules() as $module) { $filename = $modulesDir . DS . $module['path'] . DS . 'I18n' . DS . $lang . '.php'; if (file_exists($filename)) { $translations = (require $filename); $this->translations = array_replace_recursive($this->translations, $translations); } } } $filename = __DIR__ . DS . 'Preferences' . DS . $lang . '.php'; if (file_exists($filename)) { $plurals = (require_once $filename); $this->plurals = $plurals; } }
/** * Initialize application * * @return App * @throws \Magelight\Exception */ public function init() { $this->addModulesDir($this->getFrameworkDir() . DS . 'modules'); $this->addModulesDir($this->getFrameworkDir() . DS . 'modules'); $this->initIncludePaths(); \Magelight\Components\Modules::getInstance()->loadModules($this->getAppDir() . DS . 'etc' . DS . 'modules.xml'); \Magelight\Components\Modules::getInstance()->getActiveModules(); \Magelight\Config::getInstance()->load(); $this->setDeveloperMode((string) \Magelight\Config::getInstance()->getConfig('global/app/developer_mode')); if ($this->isInDeveloperMode()) { error_reporting(E_ALL); ini_set('display_errors', '1'); } \Magelight\Http\Session::getInstance()->setLifetime((int) \Magelight\Config::getInstance()->getConfig('global/app/session_lifetime', 1440))->setSessionName(self::SESSION_ID_COOKIE_NAME)->start(); $this->loadPreferences(); $lang = \Magelight\Http\Session::getInstance()->get('lang'); if (empty($lang)) { $lang = (string) \Magelight\Config::getInstance()->getConfig('global/app/default_lang'); } if (empty($lang)) { $lang = self::DEFAULT_LANGUAGE; } $this->setLang($lang); return $this; }
/** * Upgrade modules * * @return Installer * @codeCoverageIgnore */ public function upgrade() { foreach (\Magelight\Components\Modules::getInstance()->getActiveModules() as $module) { $scripts = $this->findInstallScripts($module['path']); foreach ($scripts as $script => $filename) { if (!$this->isSetupScriptExecuted($module['name'], $script)) { $this->executeScript($filename); $this->setSetupScriptExecuted($module['name'], $script); } } } return $this; }
/** * Crawl application for translations */ public function crawlApp() { $files = []; foreach (array_reverse(\Magelight\App::getInstance()->getModuleDirectories()) as $modulesDir) { foreach (\Magelight\Components\Modules::getInstance()->getActiveModules() as $module) { $path = $modulesDir . DS . $module['path']; if (is_readable($path)) { foreach ($this->_getModuleFilesList($path) as $foundFile) { $files[$path][$foundFile] = []; } } } } foreach ($files as $modulePath => $filesList) { foreach ($filesList as $filePath => $translations) { echo 'fetching translations for ' . $filePath . PHP_EOL; $translations = $this->_getTokenizer()->findTranslations($filePath); if (!empty($translations)) { if (!isset($this->_phrases[$modulePath])) { $this->_phrases[$modulePath] = []; } $this->_phrases[$modulePath] = array_merge_recursive($this->_phrases[$modulePath], $translations); } } } $this->_processLanguagePreferences(); }