function __construct() { $viewsPath = Settings::getViewsPath(); $module = Module::get(); $templatesDir = sprintf('%s/%s', $viewsPath, $module); $this->smarty = new Smarty(); $this->smarty->compile_dir = Settings::getTemplateCompilePath(); $this->smarty->cache_dir = Settings::getTemplateCachePath(); $this->smarty->setTemplateDir($templatesDir); $this->smarty->addPluginsDir(dirname(__FILE__) . '/Plugin/'); }
/** * Starts the framework * * @param string|null $settings Path to settings file * @return int If all is well then it returns zero * @throws \Exception */ public static function run(string $settings = null) : int { $settingsFile = $settings ? $settings : realpath(dirname(__FILE__) . '/../../../' . 'settings.yml'); # load settings if (!Settings::load($settingsFile)) { return false; } if (Settings::getProduction()) { ini_set('display_errors', 'Off'); ini_set('error_log', '/tmp/php-error.log'); ini_set('log_errors', 1); } # set timezone Timezone::setTimezone(Settings::getTimezone()); # set locale language I18n::setLocale(I18n::getLanguage()); # generate routes if (Settings::getAutoroute()) { Routing::generateRoutes(); } # load routes $routesFile = Settings::getRoutesFile(); if (file_exists($routesFile)) { self::$routes = YAML::load($routesFile); } else { printf('[Error] Routes file %s does not exists', $routesFile); return false; } $dbSettings = Settings::getDBSettings(); $db = new DB($dbSettings['dbm'], $dbSettings['host'], $dbSettings['name'], $dbSettings['user'], $dbSettings['password']); DB::setGlobal($db); # load actions Actions::autoloader(Settings::getActionsPath()); Session::start(); $request = isset($_GET[Settings::SETTINGS_ROUTE_PARAM_VALUE]) ? $_GET[Settings::SETTINGS_ROUTE_PARAM_VALUE] : ''; if (Routing::parseRoute($request, self::$routes) == -1) { Response::response(Response::HTTP_FORBIDDEN); } return 0; }
/** * @param string $model Model file name * @return Model Returns this instance * @throws \Exception */ private function createModel(string $model) : Model { $rootPath = Settings::getRootPath(); $modelsPath = Settings::getModelsPath(); $modelFile = sprintf('%s/%s.yml', $modelsPath, $model); $modelYaml = YAML::load($modelFile); $this->name = $modelYaml['model']; foreach ($modelYaml['fields'] as $field) { $name = $field['name']; if (isset($field['type'])) { $type = strtolower(trim($field['type'])); if (in_array($type, self::FIELD_TYPES)) { $class = sprintf('\\ORM\\Field\\%sField', ucfirst($type)); $fieldClass = new $class(); if (isset($field['size']) and method_exists($fieldClass, 'setSize')) { $fieldClass->setSize($field['size']); } if (isset($field['default']) and method_exists($fieldClass, 'setDefault')) { $fieldClass->setDefault($field['default']); } $this->addField($name, $fieldClass); } elseif ($type == 'model') { if (isset($field['name'])) { $model = new Model($field['name']); $modelField = new ModelField(); $modelField->setValue($model); $this->addField($field['name'], $modelField); } } } if (isset($field['primary'])) { if (!$this->fields[$name]->setPK(true)) { throw new \Exception('Failed to set primary key'); } } if (isset($field['auto'])) { if (!$this->fields[$name]->setAuto(true)) { throw new \Exception('Failed to set auto increment'); } } if (isset($field['void'])) { $this->fields[$name]->setNull(boolval($field['void'])); } else { $this->fields[$name]->setNull(true); } } return $this; }
/** * Generates an URL for specific routing names * * @param string $routesFile Path to the routes file * @param array $names Array containing the routing names * @return string Returns the URL * @throws \Exception */ public function getRoute(string $routesFile, array $names) : string { $result = ''; $routes = YAML::load($routesFile); $name = array_pop($names); foreach ($routes as $route) { if (isset($route['name'])) { if ($route['name'] == $name) { $result .= $route['route']; if (isset($route['module'])) { $moduleRoutesFile = sprintf('%s/%s_routes.yml', Settings::getRoutesPath(), $route['module']); $result .= $this->getRoute($moduleRoutesFile, $names); } return $result; } } } return $result; }