/** * Create view instance. * If no events manager provided - events would not be attached. * * @param DIBehaviour $di DI. * @param Config $config Configuration. * @param string|null $viewsDirectory Views directory location. * @param Manager|null $em Events manager. * * @return View */ public static function factory($di, $config, $viewsDirectory = null, $em = null) { $view = new View(); $volt = new Volt($view, $di); $volt->setOptions(["compiledPath" => $config->application->view->compiledPath, "compiledExtension" => $config->application->view->compiledExtension, 'compiledSeparator' => $config->application->view->compiledSeparator, 'compileAlways' => $config->application->debug && $config->application->view->compileAlways]); $compiler = $volt->getCompiler(); $compiler->addExtension(new Extension()); $view->registerEngines([".volt" => $volt])->setRenderLevel(View::LEVEL_ACTION_VIEW)->restoreViewDir(); if (!$viewsDirectory) { $view->setViewsDir($viewsDirectory); } // Attach a listener for type "view". if ($em) { $em->attach("view", function ($event, $view) use($di, $config) { if ($config->application->profiler && $di->has('profiler')) { if ($event->getType() == 'beforeRender') { $di->get('profiler')->start(); } if ($event->getType() == 'afterRender') { $di->get('profiler')->stop($view->getActiveRenderPath(), 'view'); } } if ($event->getType() == 'notFoundView') { throw new Exception('View not found - "' . $view->getActiveRenderPath() . '"'); } }); $view->setEventsManager($em); } return $view; }
/** * Parse import data. * Import translations and language if needed. * * @param DIBehaviour $di Dependency injection. * @param array $data Data to parse. * * @throws Exception * @return Language */ public static function parseImportData($di, $data) { if (empty($data['language']) || empty($data['locale']) || empty($data['locale'])) { throw new Exception($di->getI18n()->_('Language translations package must contains fields (not empty): name, language, locale...')); } /** * Get related language. */ $language = Language::findFirst(["language = '{$data['language']}' AND locale = '{$data['locale']}'"]); if (!$language) { $language = new Language(); $language->assign($data); if (!$language->save()) { throw new Exception(implode(', ', $language->getMessages())); } } /** * Import into database. */ $table = LanguageTranslation::getTableName(); $sql = "INSERT IGNORE INTO `{$table}` (language_id, scope, original, translated) VALUES "; $sqlValues = []; $counter = 0; $totals = []; foreach ($data['content'] as $scope => $translations) { $totals[$scope] = 0; foreach ($translations as $original => $translated) { $sqlValues[] = PHP_EOL . sprintf('(%d, "%s", "%s", "%s")', $language->getId(), mysql_real_escape_string($scope), mysql_real_escape_string($original), mysql_real_escape_string($translated)); $counter++; $totals[$scope]++; if ($counter == self::LANGUAGE_IMPORT_BATCH_SIZE) { $counter = 0; $sqlValues = ''; $di->getModelsManager()->execute($sql . implode(',', $sqlValues)); } } } if (!empty($sqlValues)) { $di->getDb()->execute($sql . implode(',', $sqlValues)); } return [$language, $totals]; }
/** * Init engine. * * @param DIBehaviour|DI $di Dependency Injection. * * @return void */ protected function _initEngine($di) { foreach ($di->get('registry')->modules as $module) { // Initialize module api. $di->setShared(strtolower($module), function () use($module, $di) { return new ApiInjector($module, $di); }); } $di->setShared('transactions', function () { return new TxManager(); }); $di->setShared('assets', new AssetsManager($di)); $di->setShared('widgets', new Catalog()); }