/** * {@inheritdoc} */ public function setUp() { $this->appMock = $this->getMock(\Magelight\App::class, [], [], '', false); \Magelight\App::forgeMock($this->appMock); $this->dbMock = $this->getMock(\Magelight\Db\Common\Adapter::class, [], [], '', false); $this->appMock->expects($this->any())->method('db')->with(App::DEFAULT_INDEX)->will($this->returnValue($this->dbMock)); $this->dbMock->expects($this->any())->method('getType')->will($this->returnValue(\Magelight\Db\Common\Adapter::TYPE_MYSQL)); $this->ormMock = $this->getMock(\Magelight\Db\Mysql\Orm::class, [], [], '', false); \Magelight\Db\Mysql\Orm::forgeMock($this->ormMock); $this->ormMock->expects($this->once())->method('create')->with(['arg1' => 'value1'], false); $this->model = Model::forge(['arg1' => 'value1']); }
/** * 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); }
/** * Forgery constructor */ public function __forge() { $this->setGetSetTargetArray($this->config); $this->config = array_merge($this->config, (array) \Magelight\Config::getInstance()->getConfig('global/document/captcha', [])); $this->font_file = \Magelight\App::getInstance()->getRealPathInModules($this->font_file); $this->cleanup(); }
public function testForwardController() { $this->controller->init(); $this->appMock->expects($this->once())->method('getCurrentAction')->will($this->returnValue([['module' => 'Module1', 'controller' => 'index', 'action' => 'index']])); $this->appMock->expects($this->once())->method('dispatchAction')->with(['module' => 'Module1', 'controller' => 'index', 'action' => 'forwarded']); $this->controller->forwardController('index', 'forwarded'); }
public function testSetSetupScriptExecuted() { $this->appMock->expects($this->any())->method('db')->will($this->returnValue($this->dbMock)); $this->dbMock->expects($this->at(0))->method('execute')->with($this->matchesRegularExpression('/CREATE TABLE.*/')); $installer = \Magelight\Installer::forge(); $this->dbMock->expects($this->at(0))->method('execute')->with($this->matches("INSERT INTO `" . $installer->getVersionTable() . "` (module_name, setup_script) VALUES (?, ?)"), ['Module1', 'setup-1.0.0.0.php']); $this->assertInstanceOf(\Magelight\Installer::class, $installer->setSetupScriptExecuted('Module1', 'setup-1.0.0.0.php')); }
/** * Initialize cache * * @return \Magelight\Cache\AdapterAbstract|File */ public function init() { $this->cachePath = isset($this->config->path) ? \Magelight\App::getInstance()->getAppDir() . DS . trim((string) $this->config->path, '\\/') : $this->cachePath; if (!file_exists($this->cachePath)) { @mkdir($this->cachePath, 0777, true); } return $this; }
/** * Forgery * * @param $entity * @param $page */ public function __forge($entity, $page) { $this->setEntity($entity); $this->setPage($page); $this->scaffold = \Magelight\Admin\Models\Scaffold\Scaffold::forge(); $this->scaffold->loadEntities(); $this->sectionReplace('pager', \Magelight\Core\Blocks\Pager::forge($this->getCollection())->setRoute(\Magelight\App::getInstance()->getCurrentAction()['match'], ['entity' => $entity])->addClass('pagination-small')->addClass('pagination-centered')); }
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()); }
/** * Get url by match * * @param string $match * @param array $params * @param string $type * @param bool $addOnlyMaskParams - add to url only params that are present in URL match mask * * @return string * @throws \Magelight\Exception */ public function getUrl($match, $params = [], $type = null, $addOnlyMaskParams = false) { if (!$type) { $type = \Magelight\Config::getInstance()->getConfigString('global/app/default_url_type', self::TYPE_HTTP); } $match = '/' . trim($match, '\\/'); if (!$addOnlyMaskParams) { if (\Magelight\App::getInstance()->isInDeveloperMode() && !$this->checkParamsWithPlaceholderMask($match, $params)) { throw new \Magelight\Exception("Passed url params don`t match mask that is set for parameter or default mask: " . \Magelight\Components\Loaders\Routes::DEFAULT_REGEX . ". Change the mask for parameter or use concatenation for complex URLs", E_USER_NOTICE); } } $params = $this->flatternParams($params); return $this->getBaseUrl($type) . $this->makeRequestUri($match, $params, $addOnlyMaskParams); }
/** * 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; }
/** * Forgery constructor * * @param \SimpleXMLElement $config * @throws \Magelight\Exception */ public function __forge($config) { $this->config = $config; if (isset($config->cache_key_prefix)) { $this->cacheKeyPrefix = (string) $config->cache_key_prefix; } else { $this->cacheKeyPrefix = (string) \Magelight\Config::getInstance()->getConfig('global/base_domain'); if (!$this->cacheKeyPrefix) { $this->cacheKeyPrefix = md5(\Magelight\App::getInstance()->getAppDir()); if (!$this->cacheKeyPrefix) { throw new \Magelight\Exception('Cache key prefix not set, and base domain too. Cache conflicts can appear.'); } } } $this->init(); }
/** * 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; } }
* DISCLAIMER * * This file is a part of a framework. Please, do not modify it unless you discard * further updates. * * @version 1.0 * @author Roman Ganin * @copyright Copyright (c) 2012-2015 rganin (rganin@gmail.com) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ $options = getopt("p:a:"); if (!isset($options['p'])) { echo 'Usage:' . PHP_EOL; echo 'php -f prepare_translations.php -- -p path/to/executed_scripts.json -a path/to/app/root'; die; } require '../../core.php'; \Magelight\App::getForgery()->setPreference(\Magelight\App::class, \Magelight\App\Web::class); \Magelight\App::getInstance()->addModulesDir(realpath($options['a']) . DS . 'modules')->setAppDir($options['a'])->setDeveloperMode(true)->init(); $jsonFile = realpath($options['p']); $scripts = json_decode(file_get_contents($jsonFile), true); $data = []; foreach ($scripts as $moduleName => $executedModuleScripts) { foreach ($executedModuleScripts as $scriptName => $scriptData) { $data[] = ['module_name' => $moduleName, 'script_name' => $scriptName]; } } $installer = \Magelight\Installer::forge(); foreach ($data as $update) { $installer->setSetupScriptExecuted($update['module_name'], $update['script_name']); }
/** * Redirect to url * * @param string $url */ public function redirect($url) { \Magelight\Http\Server::getInstance()->sendHeader("Location: {$url}"); \Magelight\App::getInstance()->shutdown(); }
/** * 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(); }
/** * Find install scripts * * @param $modulePath * @return array * @codeCoverageIgnore */ public function findInstallScripts($modulePath) { $modulePath = str_replace('\\', DS, $modulePath); $scripts = []; $modulesDirs = array_reverse(\Magelight\App::getInstance()->getModuleDirectories()); foreach ($modulesDirs as $modulesDir) { $path = $modulesDir . DS . $modulePath . DS . 'setup'; if (is_readable($path)) { foreach (glob($path . DS . '*[setup|install|upgrade]*.php') as $file) { $basename = basename($file); /** * Finding install scripts in modules sequence and not allowing to override them */ if (!isset($scripts[$basename])) { $scripts[$basename] = $file; } } } } return $scripts; }
/** * Connection setter * * @param string $connectionName * * @return $this */ public function setConnection($connectionName = \Magelight\App::DEFAULT_INDEX) { $this->connectionName = $connectionName; $this->db = \Magelight\App::getInstance()->db($connectionName); return $this; }
<?php /** * Magelight * * NOTICE OF LICENSE * * This file is open source and it`s distribution is based on * Open Software License (OSL 3.0). You can obtain license text at * http://opensource.org/licenses/osl-3.0.php * * For any non license implied issues please contact rganin@gmail.com * * @version 1.0 * @author Roman Ganin * @copyright Copyright (c) 2012-2015 rganin (rganin@gmail.com) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ require __DIR__ . '/../core.php'; \Magelight\App::getForgery()->setPreference(\Magelight\App::class, \Magelight\App\Web::class); \Magelight\App::getInstance()->addModulesDir(__DIR__ . DS . 'modules')->setAppDir(__DIR__)->setDeveloperMode(true)->init()->flushAllCache(); \Magelight\Installer::forge()->upgrade();
<?php /** * Magelight * * NOTICE OF LICENSE * * This file is open source and it`s distribution is based on * Open Software License (OSL 3.0). You can obtain license text at * http://opensource.org/licenses/osl-3.0.php * * For any non license implied issues please contact rganin@gmail.com * * @version 1.0 * @author Roman Ganin * @copyright Copyright (c) 2012-2015 rganin (rganin@gmail.com) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ require __DIR__ . '/../core.php'; \Magelight\App::getForgery()->setPreference(\Magelight\App::class, \Magelight\App\StaticResource::class); \Magelight\App::getInstance()->addModulesDir(__DIR__ . DS . 'modules')->setAppDir(__DIR__)->init()->run();
/** * Check are the required modules loaded * * @param \SimpleXMLElement $moduleConfig * @return bool * @throws \Magelight\Exception */ private function requiredModulesLoaded(\SimpleXMLElement $moduleConfig) { $result = true; $path = str_replace(['\\', '/'], DS, $moduleConfig->path) . DS . 'etc' . DS . 'module.xml'; foreach (\Magelight\App::getInstance()->getModuleDirectories() as $modulesDir) { if (is_readable($modulesDir . DS . $path)) { $moduleXml = simplexml_load_file($modulesDir . DS . $path); foreach ($moduleXml->xpath('require') as $require) { $require = (string) $require; if (!isset($this->loadQueue[$require]) && !isset($this->modules[$require])) { throw new \Magelight\Exception('Module "' . $require . '" required in "' . $moduleXml->getName() . '" is not configured for loading.'); } $result &= isset($this->modules[$require]); } } } return $result; }
/** * Minify document static entry * * @param string $type * @param array $staticEntries * @return array * @throws \Magelight\Exception */ protected function minifyDocumentStatic($type = 'css', $staticEntries = []) { $minifier = $this->getMinifierByType($type); $content = ''; $path = $this->getEntriesStaticPath($staticEntries, $type); $dir = dirname($path); if (!is_writable($dir)) { trigger_error(__("Static cache directory %s is not writable or does not exist!", [$dir])); return $staticEntries; } if ($isAlreadyMinified = $this->cache()->get($this->buildCacheKey($path), false)) { $ok = true; if (\Magelight\Config::getInstance()->getConfigBool('global/minifier/check_readability')) { $ok = is_readable($path); } if ($ok) { return ['path' => $path, 'content' => '', 'url' => \Magelight\Helpers\UrlHelper::getInstance()->getUrl($path), 'inline' => false]; } } foreach ($staticEntries as $entry) { if ($entry['inline']) { $content .= $minifier->minify($entry['content']); } else { $buffer = file_get_contents(\Magelight\App::getInstance()->getRealPathInModules($entry['path'])); if ($buffer === false) { trigger_error(__("File %s for minifier cannot be read", [$entry['path']]), E_USER_WARNING); } if (\Magelight\Config::getInstance()->getConfigBool('global/minifier/compress_' . $type)) { $buffer = $minifier->minify($buffer); } switch ($type) { case 'css': $content .= $this->fixCssUrls($buffer, $entry['path']); break; case 'js': $content .= $buffer; break; default: break; } unset($buffer); } } if (file_put_contents($path, $content)) { $this->cache()->set($this->buildCacheKey($path), 1, \Magelight\Config::getInstance()->getConfigInt('global/minifier/cache_ttl_' . $type)); return ['path' => $path, 'content' => '', 'url' => \Magelight\Helpers\UrlHelper::getInstance()->getUrl($path), 'inline' => false]; } else { return $staticEntries; } }
/** * Get model orm * * @return Db\Common\Orm */ public static function orm() { $db = \Magelight\App::getInstance()->db(static::callStaticLate('getDbIndex')); $ormClass = \Magelight\Db\Common\Orm::getOrmClassByType($db->getType()); $orm = call_user_func_array([$ormClass, 'forge'], [$db]); /* @var $orm \Magelight\Db\Mysql\Orm */ $orm->setIdColumn(static::callStaticLate('getIdField')); $orm->setTableName(static::callStaticLate('getTableName')); $orm->setModelName(static::getClassRedefinition()); return $orm; }