Exemplo n.º 1
0
 private function incQueries($start)
 {
     $numberOfQueries = container()->getNumberOfQueries();
     $numberOfQueries = is_null($numberOfQueries) ? 0 : $numberOfQueries;
     $numberOfQueries++;
     $totalDuration = container()->getTotalDuration();
     $totalDuration = is_null($totalDuration) ? 0 : $totalDuration;
     $totalDuration += $this->getTime() - $start;
     Utils::set('NbQueries', $numberOfQueries);
     Utils::set('SQLTotalDuration', $totalDuration);
 }
Exemplo n.º 2
0
 private static function defaultRoute()
 {
     $tab = explode('/', substr(static::$_uri, 1));
     if (count($tab) > 1) {
         if (3 != count($tab)) {
             $module = container()->getConfig()->getDefaultModule();
             $module = Inflector::lower($module);
             $controller = Inflector::lower(Arrays::first($tab));
             $action = $tab[1];
         } else {
             list($module, $controller, $action) = $tab;
             $module = Inflector::lower($module);
             $controller = Inflector::lower($controller);
             $action = Inflector::lower($action);
         }
         $action = repl(array('.html', '.php', '.asp', '.jsp', '.cfm', '.py', '.pl'), array('', '', '', '', '', '', ''), $action);
         if (true === container()->getMultiSite()) {
             $moduleDir = APPLICATION_PATH . DS . SITE_NAME . DS . 'modules' . DS . $module;
         } else {
             $moduleDir = APPLICATION_PATH . DS . 'modules' . DS . $module;
         }
         $controllerDir = $moduleDir . DS . 'controllers';
         $controllerFile = $controllerDir . DS . $controller . 'Controller.php';
         if (true === File::exists($controllerFile)) {
             require_once $controllerFile;
             $controllerClass = 'Thin\\' . $controller . 'Controller';
             $controllerInstance = new $controllerClass();
             $actions = get_class_methods($controllerInstance);
             $actionName = $action . 'Action';
             if (Arrays::inArray($actionName, $actions)) {
                 $dispatch = new Container();
                 $dispatch->setModule($module);
                 $dispatch->setController($controller);
                 $dispatch->setAction(Inflector::uncamelize($action, '-'));
                 Utils::set('appDispatch', $dispatch);
                 return true;
             }
         }
     }
     return null;
 }
Exemplo n.º 3
0
 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));
 }
Exemplo n.º 4
0
 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);
         }
     }
 }
Exemplo n.º 5
0
Arquivo: Sql.php Projeto: schpill/thin
 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;
 }
Exemplo n.º 6
0
 protected function _run()
 {
     $viewRedis = container()->getViewRedis();
     $echo = func_get_arg(0);
     $file = $this->_viewFile;
     $isExpired = $this->expired();
     if (false === $this->_compiled) {
         $isExpired = true;
     }
     if (false === $isExpired) {
         $file = $this->compiled();
     } else {
         if (is_numeric($this->_cache)) {
             $cacheInst = new Cache(CACHE_PATH . DS);
             $hash = sha1($this->compiled() . $this->_cache . _serialize((array) $this)) . '.cache';
             $cacheInst->forget($hash);
         }
         $file = $this->compiled($file);
     }
     if (null !== $this->_cache) {
         $isCached = isCached($file, $this->_cache, (array) $this);
         if (false === $isCached) {
             ob_start();
             if (true !== $viewRedis) {
                 include $file;
             }
             $content = ob_get_contents();
             ob_end_clean();
             if (true === $echo) {
                 if (true !== $viewRedis) {
                     echo cache($file, $content, $this->_cache, (array) $this);
                 }
             } else {
                 if (true !== $viewRedis) {
                     return cache($file, $content, $this->_cache, (array) $this);
                 }
             }
         } else {
             if (true !== $viewRedis) {
                 $content = cache($file, null, $this->_cache, (array) $this);
                 if (true === $echo) {
                     echo $content;
                 } else {
                     return $content;
                 }
             }
         }
     } else {
         if (true === $echo) {
             if (true !== $viewRedis) {
                 include $file;
             } else {
                 $this->compile($file);
             }
         } else {
             ob_start();
             if (true !== $viewRedis) {
                 include $file;
             } else {
                 $this->compile($file);
             }
             $content = ob_get_contents();
             ob_end_clean();
             $hash = sha1($this->_viewFile);
             Utils::set($hash, $content);
             return $content;
         }
     }
 }
Exemplo n.º 7
0
 public function noRender()
 {
     $this->view->noCompiled();
     Utils::set('showStats', null);
 }
Exemplo n.º 8
0
 public static function _incQueries($start)
 {
     static::$_numberOfQueries++;
     static::$_totalDuration += static::_getTime() - $start;
     Utils::set('NbQueriesNOSQL', static::$_numberOfQueries);
     Utils::set('SQLTotalDurationNOSQL', static::$_totalDuration);
 }