public static function setup() { date_default_timezone_set(\Gini\Config::get('system.timezone') ?: 'Asia/Shanghai'); class_exists('\\Gini\\Those'); class_exists('\\Gini\\ThoseIndexed'); isset($_GET['locale']) and $_SESSION['locale'] = $_GET['locale']; isset($_SESSION['locale']) and \Gini\Config::set('system.locale', $_SESSION['locale']); \Gini\I18N::setup(); setlocale(LC_MONETARY, (\Gini\Config::get('system.locale') ?: 'zh_CN') . '.UTF-8'); }
public function testPlurals() { $plurals = \Gini\Config::get('orm.plurals'); $plurals['users'] = 'user'; \Gini\Config::set('orm.plurals', $plurals); // e.g. those('users') \Gini\Those::reset(); $those = those('users')->whose('name')->beginsWith('COOL'); $those->makeSQL(); $this->assertAttributeEquals('SELECT DISTINCT "t0"."id" FROM "user" AS "t0" WHERE "t0"."name" LIKE \'COOL%\'', 'SQL', $those); }
public static function request($route, array $env = array()) { $args = array_map('rawurldecode', explode('/', $route)); $path = ''; $candidates = array('/index' => $args) + Util::pathAndArgs($args); $class = null; foreach (array_reverse($candidates) as $path => $params) { $path = strtr(ltrim($path, '/'), ['-' => '', '_' => '']); $basename = basename($path); $dirname = dirname($path); $class_namespace = '\\Gini\\Controller\\CGI\\'; if ($dirname != '.') { $class_namespace .= strtr($dirname, ['/' => '\\']) . '\\'; } $class = $class_namespace . $basename . '\\Index'; if (class_exists($class)) { break; } $class = $class_namespace . $basename; if (class_exists($class)) { break; } $class = $class_namespace . 'Controller' . $basename; if (class_exists($class)) { break; } if ($basename != 'index') { $class = $class_namespace . 'Index'; if (class_exists($class)) { array_unshift($params, $basename); break; } } } if (!$class || !class_exists($class, false)) { self::redirect('error/404'); } \Gini\Config::set('runtime.controller_path', $path); \Gini\Config::set('runtime.controller_class', $class); $controller = \Gini\IoC::construct($class); $action = strtr($params[0], ['-' => '', '_' => '']); if ($action && $action[0] != '_' && method_exists($controller, 'action' . $action)) { $action = 'action' . $action; array_shift($params); } elseif (method_exists($controller, '__index')) { $action = '__index'; } else { self::redirect('error/404'); } $controller->action = $action; $controller->params = $params; $controller->env = $env; return $controller; }
public static function dispatch(array $argv) { if (!isset($GLOBALS['gini.class_map'])) { echo "[33mNOTICE: You are currently executing commands without cache![0m\n\n"; } $candidates = Util::pathAndArgs($argv, true); $class = null; foreach (array_reverse($candidates) as $path => $params) { $path = strtr(ltrim($path, '/'), ['-' => '', '_' => '']); $basename = basename($path); $dirname = dirname($path); $class_namespace = '\\Gini\\Controller\\CLI\\'; if ($dirname != '.') { $class_namespace .= strtr($dirname, ['/' => '\\']) . '\\'; } $class = $class_namespace . $basename; if (class_exists($class)) { break; } $class = $class_namespace . 'Controller' . $basename; if (class_exists($class)) { break; } } if (!$class || !class_exists($class, false)) { $class = '\\Gini\\Controller\\CLI\\App'; $params = $argv; } \Gini\Config::set('runtime.controller_path', $path); \Gini\Config::set('runtime.controller_class', $class); $controller = \Gini\IoC::construct($class); $action = strtr($params[0], ['-' => '', '_' => '']); if ($action && method_exists($controller, 'action' . $action)) { $action = 'action' . $action; array_shift($params); } elseif (method_exists($controller, '__index')) { $action = '__index'; } else { $action = '__unknown'; } $controller->action = $action; $controller->params = $params; $controller->execute(); }