public static function run() { Request::$route = $route = Utils::get('appDispatch'); container()->setRoute($route); $render = $route->getRender(); $tplDir = $route->getTemplateDir(); $module = $route->getModule(); $controller = $route->getController(); $action = $route->getAction(); $alert = $route->getAlert(); $page = container()->getPage(); $isCms = !empty($page); if (!empty($render)) { $tplMotor = $route->getTemplateMotor(); $tplDir = empty($tplDir) ? APPLICATION_PATH . DS . SITE_NAME . DS . 'app' . DS . 'views' : $tplDir; $tpl = $tplDir . DS . $render . '.phtml'; if (File::exists($tpl)) { if ('Twig' == $tplMotor) { if (!class_exists('Twig_Autoloader')) { require_once 'Twig/Autoloader.php'; } $tab = explode(DS, $tpl); $file = Arrays::last($tab); $path = repl(DS . $file, '', $tpl); $loader = new \Twig_Loader_Filesystem($path); $view = new \Twig_Environment($loader, array('cache' => CACHE_PATH, 'debug' => false, 'charset' => 'utf-8', 'strict_variables' => false)); container()->setView($view); if ($action instanceof Closure) { $action($view); } $params = null === container()->getViewParams() ? array() : container()->getViewParams(); echo $view->render($file, $params); /* stats */ if (null === container()->getNoShowStats() && null === $route->getNoShowStats()) { echo View::showStats(); } } else { $view = new View($tpl); container()->setView($view); if ($action instanceof Closure) { $action($view); } $view->render(); /* stats */ if (null === container()->getNoShowStats() && null === $route->getNoShowStats()) { echo View::showStats(); } } return; } } $module = Inflector::lower($module); $controller = Inflector::lower($controller); $action = Inflector::lower($action); if (true === container()->getMultiSite()) { $moduleDir = APPLICATION_PATH . DS . SITE_NAME . DS . 'modules' . DS . $module; } else { $moduleDir = APPLICATION_PATH . DS . 'modules' . DS . $module; } if (!is_dir($moduleDir)) { throw new Exception("The module '{$module}' does not exist."); } $controllerDir = $moduleDir . DS . 'controllers'; if (!is_dir($controllerDir)) { throw new Exception("The controller '{$controller}' does not exist."); } $controllerFile = $controllerDir . DS . $controller . 'Controller.php'; if (!File::exists($controllerFile)) { throw new Exception("The controller '{$controllerFile}' does not exist."); } require_once $controllerFile; $controllerClass = 'Thin\\' . $controller . 'Controller'; $controller = new $controllerClass(); $controller->view = new View($route->getView()); if (null !== $alert) { $controller->view->alert($alert); } container()->setController($controller); $actions = get_class_methods($controllerClass); if (true === $isCms) { if (!Arrays::inArray($action, $actions)) { $action = 'page'; } } container()->setAction($action); if (strstr($action, '-')) { $words = explode('-', $action); $newAction = ''; for ($i = 0; $i < count($words); $i++) { $word = trim($words[$i]); if ($i > 0) { $word = ucfirst($word); } $newAction .= $word; } $action = $newAction; } $actionName = $action . 'Action'; if (Arrays::in('init', $actions)) { $controller->init(); } if (Arrays::in('preDispatch', $actions)) { $controller->preDispatch(); } if (!Arrays::in($actionName, $actions)) { throw new Exception("The action '{$actionName}' does not exist."); } $controller->{$actionName}(); $controller->view->render(); if (Arrays::inArray('postDispatch', $actions)) { $controller->postDispatch(); } /* stats */ if (null !== Utils::get("showStats")) { echo View::showStats(); } }
public function __call($func, $argv) { $key = sha1('orm' . $this->_token); $orm = isAke($this->values, $key, false); $key = sha1('model' . $this->_token); $dbjson = isAke($this->values, $key, false); if (substr($func, 0, 4) == 'link' && false !== $orm) { $value = Arrays::first($argv); $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 4))); $var = Inflector::lower($uncamelizeMethod); if (!empty($var)) { $var = setter($var . '_id'); $this->{$var}($value->id); return $this; } } elseif (substr($func, 0, 3) == 'get') { $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3))); $var = Inflector::lower($uncamelizeMethod); if (isset($this->{$var})) { if (isset($this->thin_type)) { $type = $this->thin_type; Data::getModel($type); $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : []; if (Arrays::exists('relationships', $settings)) { if (Arrays::exists($var, $settings['relationships'])) { return Data::getById($var, $this->{$var}); } } } if (Arrays::is($this->{$var}) && count($argv) == 1) { $o = new self(); $getter = getter(Arrays::first($argv)); $o->populate($this->{$var}); return $o->{$getter}(); } if ($this->{$var} instanceof Closure) { if (is_callable($this->{$var}) && count($argv)) { return call_user_func_array($this->{$var}, $argv); } } return count($argv) && is_null($this->{$var}) ? Arrays::first($argv) : $this->{$var}; } else { if (isset($this->db_instance)) { return $this->db_instance->getValue($this, $var); } if (isset($this->thin_type)) { $type = $this->thin_type; Data::getModel($type); $settings = Arrays::exists($type, Data::$_settings) ? Data::$_settings[$type] : []; $relationships = Arrays::exists('relationships', $settings) ? $settings['relationships'] : []; if (Arrays::exists($var, $relationships) && 's' == $var[strlen($var) - 1]) { if (Arrays::exists($var, $relationships)) { $res = dm(substr($var, 0, -1))->where("{$type} = " . $this->id)->get(); $collection = []; if (count($res)) { foreach ($res as $obj) { array_push($collection, $obj); } } return $collection; } } elseif (Arrays::exists('defaultValues', $settings)) { if (Arrays::is($settings['defaultValues'])) { if (Arrays::exists($this->{$var}, $settings['defaultValues'])) { return $settings['defaultValues'][$this->{$var}]; } } } } if (count($argv) == 1) { return Arrays::first($argv); } return null; } } elseif (substr($func, 0, 3) == 'has') { $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3))); $var = Inflector::lower($uncamelizeMethod); if (isset($this->{$var})) { return !empty($this->{$var}); } elseif (isset($this->db_instance)) { return $this->db_instance->hasValue($this, $var); } } elseif (substr($func, 0, 3) == 'set') { $value = Arrays::first($argv); $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3))); $var = Inflector::lower($uncamelizeMethod); if (!empty($var)) { if (isset($this->thin_type)) { Data::getModel($this->thin_type); $fields = Arrays::exists($this->thin_type, Data::$_fields) ? Data::$_fields[$this->thin_type] : []; if (!Arrays::exists($var, $fields)) { throw new Exception($var . ' is not defined in the model => ' . $this->thin_type); } else { $settingsField = $fields[$var]; if (Arrays::exists('checkValue', $settingsField)) { $functionCheck = $settingsField['checkValue']; $value = $functionCheck($value); } if (is_object($value)) { if (isset($value->thin_type)) { if ($value->thin_type == $var) { $value = $value->id; } } } } } $this->{$var} = $value; if (!Arrays::in($var, $this->_fields)) { $this->_fields[] = $var; } if (isset($this->is_thin_object)) { $name = $this->is_thin_object; $objects = Utils::get('thinObjects'); $this->values = null; $objects[$name] = $this; Utils::set('thinObjects', $objects); } if (isset($this->is_app)) { if (true === $this->is_app) { Utils::set('ThinAppContainer', $this); } } } elseif (isset($this->db_instance)) { return $this->db_instance->setValue($this, $var, $value); } return $this; } elseif (substr($func, 0, 3) == 'add') { $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 3))); $var = Inflector::lower($uncamelizeMethod) . 's'; $value = Arrays::first($argv); if (!isset($this->{$var})) { $this->{$var} = []; } if (!Arrays::is($this->{$var})) { $this->{$var} = []; } array_push($this->{$var}, $value); return $this; } elseif (substr($func, 0, 6) == 'remove') { $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($func, 6))); $var = Inflector::lower($uncamelizeMethod) . 's'; $value = Arrays::first($argv); if (isset($this->{$var})) { if (Arrays::is($this->{$var})) { if (count($this->{$var})) { $remove = false; foreach ($this->{$var} as $key => $tmpValue) { $comp = md5(serialize($value)) == md5(serialize($tmpValue)); if (true === $comp) { $remove = true; break; } } if (true === $remove) { unset($this->{$var}[$key]); } } } } return $this; } if (Arrays::in($func, $this->_fields)) { if ($this->{$func} instanceof Closure) { return call_user_func_array($this->{$func}, $argv); } } if (Arrays::exists($func, $this->_closures)) { if ($this->_closures[$func] instanceof Closure) { return call_user_func_array($this->_closures[$func], $argv); } } if (isset($this->_token)) { $id = sha1($func . $this->_token); if (Arrays::is($this->values)) { if (Arrays::exists($id, $this->values)) { return call_user_func_array($this->values[$id], $argv); } } } if (true === hasEvent($func)) { array_push($argv, $this); return fire($func, $argv); } if (!is_callable($func) || substr($func, 0, 6) !== 'array_' || substr($func, 0, 3) !== 'set' || substr($func, 0, 3) !== 'get' || substr($func, 0, 3) !== 'has' || substr($func, 0, 3) !== 'add' || substr($func, 0, 6) !== 'remove') { $callable = strrev(repl('_', '', $func)); if (!is_callable($callable)) { if (method_exists($this, $callable)) { return call_user_func_array(array($this, $callable), $argv); } } else { return call_user_func_array($callable, $argv); } if (isset($this->thin_litedb)) { $closure = isAke($this->thin_litedb->closures, $func); if (!empty($closure) && $closure instanceof Closure) { return $closure($this); } } if (isset($this->db_instance)) { return $this->db_instance->{$func}($this, $var, $value); call_user_func_array(array($this->db_instance, $func), array_merge(array($this), $argv)); } if (false !== $orm) { $db = call_user_func_array($orm, []); $fields = array_keys($db->map['fields']); if (Arrays::in($func, $fields)) { if (!count($argv)) { return $this->{$func}; } else { $setter = setter($func); $this->{$setter}(Arrays::first($argv)); return $this; } } $tab = str_split($func); $many = false; if (Arrays::last($tab) == 's') { array_pop($tab); $table = implode('', $tab); $many = true; } else { $table = $func; } $object = count($argv) == 1 ? Arrays::first($argv) : false; $model = model($table); return true === $many ? $model->where($db->table . '_id = ' . $this->id)->exec($object) : $model->where($db->table . '_id = ' . $this->id)->first($object); } if (false !== $dbjson) { $db = $this->model()->db(); $fields = $db->fields(); $modelMethods = get_class_methods('Dbjson\\Model'); if (Arrays::in($func, $fields)) { if (!count($argv)) { return $this->{$func}; } else { $setter = setter($func); $this->{$setter}(Arrays::first($argv)); return $this; } } if (Arrays::in($func, $modelMethods)) { return call_user_func_array([$this->model(), $func], $argv); } $tab = str_split($func); $many = false; if (Arrays::last($tab) == 's') { array_pop($tab); $table = implode('', $tab); $many = true; } else { $table = $func; } $object = count($argv) == 1 ? Arrays::first($argv) : true; $model = jdb($db->db, $table); return true === $many ? $model->where($db->table . '_id = ' . $this->id)->exec($object) : $model->where($db->table . '_id = ' . $this->id)->first($object); } return null; } return call_user_func_array($func, array_merge(array($this->getArrayCopy()), $argv)); }
function save($key, $value = null) { $saved = Utils::get('ThinSaved'); if (null === $saved) { if (null === $value) { return null; } $saved = array(); $saved[$key] = $value; Utils::set('ThinSaved', $saved); } else { if (null === $value) { if (Arrays::exists($key, $saved)) { return $saved[$key]; } else { return null; } } else { $saved[$key] = $value; Utils::set('ThinSaved', $saved); } } }
public function row(array $data, $recursive = true, $extends = array()) { if (Arrays::isAssoc($data)) { $obj = o(sha1(serialize($data))); $obj->db_instance = $this; if (count($extends)) { foreach ($extends as $name => $instance) { $closure = function ($object) use($name, $instance) { $idx = $object->is_thin_object; $objects = Utils::get('thinObjects'); return $instance->{$name}($objects[$idx]); }; $obj->_closures[$name] = $closure; } } $fields = $this->fields(); foreach ($fields as $field) { $hasFk = $this->hasFk($field); if (false === $hasFk) { $obj->{$field} = $data[$field]; } else { extract($hasFk); $ar = ar($foreignEntity, $foreignTable); $one = contain('toone', Inflector::lower($type)); if ($one && $recursive) { $foreignObj = $ar->findBy($foreignKey, $data[$field], $one); $obj->{$relationKey} = $foreignObj; } } } $hasFk = ake('relationships', $this->_settings); if (true === $hasFk && $recursive) { $rs = $this->_settings['relationships']; if (count($rs)) { foreach ($rs as $field => $infos) { extract($infos); $ar = ar($foreignEntity, $foreignTable); if (!Arrays::in($field, $fields)) { $pk = $this->pk(); $obj->{$field} = $ar->findBy($foreignKey, $obj->{$pk}, false, false); } } } } return $obj; } return null; }
public function close() { $configs = container()->getConfig()->getDb(); $config = isAke($configs, $this->entity); if (empty($config)) { throw new Exception("Database configuration does not exist."); } $username = $config->getUsername(); if (empty($username)) { throw new Exception("Username is mandatory to connect database."); } $adapter = $config->getAdapter(); $password = $config->getPassword(); $dbName = $config->getDatabase(); $host = $config->getHost(); $dsn = $config->getDsn(); if (!empty($dsn)) { $adapter = 'mysql'; } $connexions = Utils::get('SQLConnexions'); if (null === $connexions) { $connexions = array(); } $keyConnexion = sha1(serialize(array("{$adapter}:dbname={$dbName};host={$host}", $username, $password))); if (Arrays::exists($keyConnexion, $connexions)) { $connexions[$keyConnexion] = null; Utils::set('SQLConnexions', $connexions); } $this->_isConnected = false; return $this; }
public static function showStats() { Timer::stop(); $executionTime = Timer::get(); $queries = null === Utils::get('NbQueries') ? 0 : Utils::get('NbQueries'); $valQueries = $queries < 2 ? 'SQL Query executed' : 'SQL Queries executed'; $SQLDuration = null === Utils::get('SQLTotalDuration') ? 0 : Utils::get('SQLTotalDuration'); $queriesNoSQL = \Dbjson\Dbjson::$queries; $valQueriesNoSQL = $queriesNoSQL < 2 ? 'NoSQL Query executed' : 'NoSQL Queries executed'; $SQLDurationNoSQL = number_format(\Dbjson\Dbjson::$duration, 6); $execPHPSQL = $executionTime - $SQLDuration; $execPHPNoSQL = $executionTime - $SQLDurationNoSQL; $execPHP = $executionTime - $SQLDuration - $SQLDurationNoSQL; $PCPhp = round($execPHP / $executionTime * 100, 2); $PCPhpSQL = round($execPHPSQL / $executionTime * 100, 2); $PCPhpNoSQL = round($execPHPNoSQL / $executionTime * 100, 2); $PCSQL = 100 - $PCPhpSQL; $PCNoSQL = 100 - $PCPhpNoSQL; $included = count(get_included_files()); return "\n<!--\n\n\tPage generated in {$executionTime} s. by Thin Framework (C) www.geraldplusquellec.me 1996 - " . date('Y') . "\n\t{$queries} {$valQueries} in {$SQLDuration} s. (" . $PCSQL . " %)\n\t{$queriesNoSQL} {$valQueriesNoSQL} in {$SQLDurationNoSQL} s. (" . $PCNoSQL . " %)\n\tPHP Execution {$execPHP} s. ({$PCPhp} %)\n\n\n\n\t" . $included . " scripts included\n\tUsed Memory : " . convertSize(memory_get_usage()) . "\n\n-->"; }
public static function row($type, array $data, $extends = array()) { if (Arrays::isAssoc($data)) { $obj = o(sha1(serialize($data))); if (count($extends)) { foreach ($extends as $name => $instance) { $closure = function ($object) use($name, $instance) { $idx = $object->is_thin_object; $objects = Utils::get('thinObjects'); return $instance->{$name}($objects[$idx]); }; $obj->_closures[$name] = $closure; } } $settings = Arrays::exists($type, static::$_settings) ? static::$_settings[$type] : array(); if (count($settings)) { $db = Arrays::exists('db', $settings) ? $settings['db'] : null; if (!empty($db)) { $methods = array('save', 'delete'); foreach ($methods as $method) { if (!Arrays::exists($method, $obj->_closures)) { $closure = function () use($type, $method, $obj, $db) { $name = $method . Inflector::camelize($db); return $obj->{$name}($type); }; $obj->_closures[$method] = $closure; } } } } return $obj->populate($data); } return null; }