private static function _getTagData($gid = 0) { $node = \Gini\Config::get('app.node'); $tag = "labmai-{$node}/{$gid}"; $rpc = self::_getTagRPC(); return (array) $rpc->tagdb->data->get($tag); }
public static function setup() { $session_conf = (array) \Gini\Config::get('system.session'); $cookie_params = (array) $session_conf['cookie']; $session_name = $session_conf['name'] ?: 'gini-session'; $host_hash = sha1($cookie_params['domain'] ?: $_SERVER['HTTP_HOST']); ini_set('session.name', $session_name . '_' . $host_hash); if ($session_conf['save_handler']) { self::$_handlerName = $session_conf['save_handler']; // save_handler = internal/files if (0 == strncmp(self::$_handlerName, 'internal/', 9)) { ini_set('session.save_handler', substr(self::$_handlerName, 9)); } else { // save_handler = Database $class = '\\Gini\\Session\\' . self::$_handlerName; if (class_exists($class)) { self::$_handler = \Gini\IoC::construct($class); session_set_save_handler(self::$_handler, false); } } } if ($session_conf['save_path']) { session_save_path($session_conf['save_path']); } if ($session_conf['gc_maxlifetime']) { ini_set('session.gc_maxlifetime', $session_conf['gc_maxlifetime']); } if (isset($_POST['gini-session'])) { session_id($_POST['gini-session']); } elseif (isset($_SERVER['HTTP_X_GINI_SESSION'])) { session_id($_SERVER['HTTP_X_GINI_SESSION']); } session_set_cookie_params($cookie_params['lifetime'], $cookie_params['path'], $cookie_params['domain']); self::open(); }
public function __toString() { if ($this->_ob_cache !== null) { return $this->_ob_cache; } $path = $this->_path; $locale = \Gini\Config::get('system.locale'); $localeSpecificPath = "@{$locale}/{$path}"; $engines = \Gini\Config::get('view.engines'); if (isset($GLOBALS['gini.view_map'][$localeSpecificPath])) { $realPath = $GLOBALS['gini.view_map'][$localeSpecificPath]; $engine = $engines[pathinfo($realPath, PATHINFO_EXTENSION)]; } elseif (isset($GLOBALS['gini.view_map'][$path])) { $realPath = $GLOBALS['gini.view_map'][$path]; $engine = $engines[pathinfo($realPath, PATHINFO_EXTENSION)]; } else { foreach ($engines as $ext => $engine) { $realPath = \Gini\Core::locatePharFile(VIEW_DIR, "{$localeSpecificPath}.{$ext}"); if (!$realPath) { $realPath = \Gini\Core::locatePharFile(VIEW_DIR, "{$path}.{$ext}"); } if ($realPath) { break; } } } if ($engine && $realPath) { $class = '\\Gini\\View\\' . $engine; $output = \Gini\IoC::construct($class, $realPath, $this->_vars); } return $this->_ob_cache = (string) $output; }
public function __construct($name) { // 查询一下看看是不是复数 $name = \Gini\Config::get('orm.plurals')[$name] ?: $name; $this->name = $name; $this->table_name = str_replace('/', '_', $name); $this->db = a($name)->db(); }
public function __construct($name) { $name = \Gini\Config::get('orm.plurals')[$name] ?: $name; $this->name = $name; $this->table_name = str_replace('/', '_', $name); $this->db = a($name)->db(); $this->_table = 't' . $this->uniqid(); }
public static function of($name) { $conf = (array) (\Gini\Config::get("cache.{$name}") ?: \Gini\Config::get("cache.default")); $_driver = $conf['driver']; if (!isset(self::$_CACHE[$_driver])) { self::$_CACHE[$_driver] = \Gini\IoC::construct('\\Gini\\Cache', $name, $conf['driver'] ?: 'none', (array) $conf['options']); } return self::$_CACHE[$_driver]; }
public static function of($name, $cookie = null, $header = []) { if (!self::$_RPCs[$name]) { $conf = \Gini\Config::get('app.rpc'); $rpc = IoC::construct('\\Gini\\RPC', $conf[$name]['url'], null, $cookie, $header); self::$_RPCs[$name] = $rpc; } return self::$_RPCs[$name]; }
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 static function setup() { if (!defined('I18N_PATH')) { define('I18N_PATH', APP_PATH . '/i18n'); } bindtextdomain(APP_ID, I18N_PATH); textdomain(APP_ID); bind_textdomain_codeset(APP_ID, 'UTF-8'); self::setLocale(\Gini\Config::get('system.locale') ?: 'en_US'); }
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 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 function actionCheckRemoteExists() { $dtstart = date('Y-m-d', strtotime("-3 days")); $dtend = date('Y-m-d'); $start = 0; $limit = 20; $conf = \Gini\Config::get('app.rpc')['chemdb']; $rpc1 = \Gini\IoC::construct('\\Gini\\RPC', $conf['url']); $rpc2 = self::getRPC('lab-inventory'); while (true) { $orders = Those('order')->whose('customized')->is(false)->andWhose('ctime')->isGreaterThan($dtstart)->andWhose('ctime')->isLessThan($dtend)->limit($start, $limit); if (!count($orders)) { break; } $start += $limit; $sync_inv = true; foreach ($orders as $order) { $group = $order->group; $items = $order->items; foreach ($items as $item) { $pid = $item['id']; $product = a('product', $pid); $cas_no = $product->cas_no; $type = $product->type; if ($type == 'chem_reagent' && $cas_no) { $results = $rpc1->chemDB->getChemicalTypes($cas_no); if (isset($results[$cas_no])) { $result = $results[$cas_no]; /* $result : '100-19-2'=>[hazardous, highly_toxic] */ $hazs = ['drug_precursor', 'highly_toxic', 'hazardous', 'explosive', 'psychotropic', 'narcotic']; if (count(array_intersect($hazs, $result))) { $criteria = []; $criteria['order_voucher'] = $order->voucher; $criteria['product'] = $pid; $data = $rpc2->mall->inventory->getInventory($criteria); if (!isset($data['id'])) { echo "no data\n"; // 危化品没有生成对应的存货记录, 重新生成 $product = a('product', $pid); $data = ['name' => $product->name, 'manufacturer' => $product->manufacturer, 'brand' => $product->brand, 'catalog_no' => $product->catalog_no, 'package' => $product->package, 'price' => $item['unit_price'], 'quantity' => $item['quantity'], 'user' => $order->requester->id, 'group' => $order->group->id, 'product' => $pid, 'order_voucher' => $order->voucher, 'vendor_id' => $order->vendor->id, 'vendor_name' => $order->vendor->name, 'location' => '--']; $rpc2->mall->inventory->createItem($data); } else { echo "has data\n"; } } } } } } } }
/** * @brief 一卡通密码验证 * * @param $username * @param $password * * @return boolean */ protected function verify($username, $password) { try { $config = (array) \Gini\Config::get('app.rpc'); $config = $config['nankai_gateway']; $api = $config['url']; $rpc = \Gini\IoC::construct('\\Gini\\RPC', $api); return !!$rpc->nankai->auth->verify($username, $password); } catch (\Exception $e) { } return false; }
public static function setup() { $session_conf = (array) \Gini\Config::get('system.session'); $cookie_params = (array) $session_conf['cookie']; $session_name = $session_conf['name'] ?: 'gini-session'; $host_hash = sha1($cookie_params['domain'] ?: $_SERVER['HTTP_HOST']); ini_set('session.name', $session_name . '_' . $host_hash); if ($session_conf['save_handler']) { $handler_name = $session_conf['save_handler']; // save_handler = internal/files if (0 == strncmp($handler_name, 'internal/', 9)) { ini_set('session.save_handler', substr($handler_name, 9)); } else { // save_handler = Database $class = '\\Gini\\Session\\' . $handler_name; self::$_handler = \Gini\IoC::construct($class); session_set_save_handler(self::$_handler, false); } } if ($session_conf['save_path']) { session_save_path($session_conf['save_path']); } if (PHP_SAPI == 'cli') { ini_set('session.use_cookies', 0); // TODO: find a better way to save and load session id $idPath = self::_idPath(); if (file_exists($idPath)) { session_id(file_get_contents($idPath)); } } session_set_cookie_params($cookie_params['lifetime'], $cookie_params['path'], $cookie_params['domain']); if (isset($_POST['gini-session'])) { session_id($_POST['gini-session']); } elseif (isset($_SERVER['HTTP_X_GINI_SESSION'])) { session_id($_SERVER['HTTP_X_GINI_SESSION']); } set_error_handler(function () { }, E_ALL ^ E_NOTICE); session_start(); restore_error_handler(); if (!ini_get('session.use_cookies')) { // close session immediately to avoid deadlock session_write_close(); } $now = time(); foreach ((array) $_SESSION['@TIMEOUT'] as $token => $timeout) { if ($now > $timeout) { unset($_SESSION[$token]); unset($_SESSION['@TIMEOUT'][$token]); } } }
public function __index() { //获取系统维护模块的状态信息 $config = \Gini\Config::get('maintenance'); if ($config['status'] && $config['status'] == 'on') { $view_vars = $config['view_vars']; //维护模块开启时跳转到维护页面 return \Gini\IoC::construct('\\Gini\\CGI\\Response\\HTML', V('maintenance', array('maintain_end_date' => $view_vars['maintain_end_date']))); } else { //维护模块关闭时跳转到首页 return $this->redirect('/'); } }
public function actionRun($name = null) { $job = \Gini\Config::get('cron')[$name]; if (!$job) { return false; } $command_args = \Gini\Util::parseArgs($job['command']); ob_start(); \Gini\CLI::dispatch($command_args); $output = ob_get_contents(); ob_end_clean(); return $output; }
public static function setup() { //获取系统维护模块的状态信息 $config = \Gini\Config::get('maintenance'); $route = \Gini\CGI::route(); $path = 'maintain'; //维护模块开启 if ($config['status'] && $config['status'] == 'on') { //判断$route是防止后续的模块不给他机会直接跳转,判断$route != $path是防止重定向跳转 if ($route && $route != $path) { \Gini\CGI::redirect($path); } } }
/** * Instantiate Logger object by name. * * @param string $name Logger name */ public function __construct($name) { $this->_name = $name; $config = \Gini\Config::get("logger.{$this->_name}") ?: \Gini\Config::get('logger.default'); foreach ($config as $handlerName => $options) { if (!is_array($options)) { continue; } // ignore when "disabled" or invalid value passed. $level = isset($options['level']) ? $options['level'] : Logger\Level::DEBUG; $handlerClass = "\\Gini\\Logger\\{$handlerName}"; $handler = \Gini\IoC::construct($handlerClass, $this->_name, $level, $options); $this->_handlers[] = $handler; } }
public function actionExport($args) { $opt = \Gini\Util::getOpt($args, 'h', ['help', 'json', 'yaml']); if (isset($opt['h']) || isset($opt['help'])) { echo "Usage: gini config export [-h|--help] [--json|--yaml]\n"; return; } \Gini\Config::setup(); $items = \Gini\Config::export(); if (isset($opt['json'])) { echo J($items, JSON_PRETTY_PRINT) . "\n"; } else { echo yaml_emit($items, YAML_UTF8_ENCODING); } }
public function actionAuthorize($clientID, $clientSecret) { $confs = \Gini\Config::get('mall.rpc'); $conf = $confs['node']; try { $rpc = \Gini\IoC::construct('\\Gini\\RPC', $conf['url']); $bool = $rpc->mall->authorize($clientID, $clientSecret); } catch (\Exception $e) { throw new \Gini\API\Exception('网络故障', 503); } if ($bool) { $this->setCurrentApp($clientID); return session_id(); } throw new \Gini\API\Exception('非法的APP', 404); }
/** * Get a database object by name. * * @param string|null $name Name of the database configured in database.yml * * @return object **/ public static function db($name = null) { $name = $name ?: 'default'; if (!isset(self::$DB[$name])) { $opt = \Gini\Config::get('database.' . $name); if (is_string($opt)) { // 是一个别名 $db = static::db($opt); } else { if (!is_array($opt)) { throw new Database\Exception('database "' . $name . '" was not configured correctly!'); } $db = \Gini\IoC::construct('\\Gini\\Database', $opt['dsn'], $opt['username'], $opt['password'], $opt['options']); } static::$DB[$name] = $db; } return static::$DB[$name]; }
public function actionExport($args) { $gini_bin = $_SERVER['_']; $opt = \Gini\Util::getOpt($args, 'hu:', ['help', 'user:'******'prefix:', 'suffix:']); if (isset($opt['h']) || isset($opt['help'])) { echo "Usage: gini cron export [-h|--help] [-u|--user=USER] [--prefix=PREFIX] [--suffix=SUFFIX]\n"; return; } $prefix = $opt['prefix'] ?: ''; $suffix = $opt['suffix'] ?: ''; $user = $opt['u'] ?: $opt['user'] ?: ''; foreach ((array) \Gini\Config::get('cron') as $cron) { if ($cron['comment']) { printf("# %s\n", $cron['comment']); } printf("%s%s\t%s%s @%s %s%s\n\n", $cron['interval'], $user ? "\t{$user}" : '', $prefix, $gini_bin, APP_ID, $cron['command'], $suffix); } }
private static function _rurl_mod($url, $type) { // $info = \Gini\Core::moduleInfo(APP_ID); $config = (array) \Gini\Config::get('system.rurl_mod'); if ($type) { $query = $config[$type]['query']; $query = $query ? strtr($query, ['$(TIMESTAMP)' => time(), '$(VERSION)' => APP_HASH]) : null; } return empty($query) ? $url : self::url($url, $query); }
/** * Return localized string by property name. * * @param string $name * @param string $locale */ public function L($name, $locale = null) { // 如果之前没有触发数据库查询, 在这里触发一下 $this->fetch(); // if \Gini\Config::get('system.locale') == 'zh_CN', $object->L('name') will access $object->_extra['i18n'][zh_CN]['name'] if (!isset($locale)) { $locale = \Gini\Config::get('system.locale'); } if (isset($this->_extra['@i18n'][$locale][$name])) { return $this->_extra['@i18n'][$locale][$name]; } return $this->{$name}; }
/** * Load event hooks according config. */ public static function setup() { foreach ((array) \Gini\Config::get('hooks') as $event => $event_hooks) { foreach ((array) $event_hooks as $key => $hook) { if (!is_string($key)) { $key = null; } // $config['xxx'] = array('return'=>'callback:xxx_func', ); if (is_array($hook) && isset($hook['return'])) { $return = $hook['return']; $weight = $hook['weight']; } else { $return = $hook; $weight = 0; } static::bind($event, $return, $weight, $key); } } }
private static function _cacheConfig($env) { $plurals = self::_getORMPlurals(); printf("%s\n", 'Updating config cache...'); $config_items = \Gini\Config::fetch(); // update orm plurals $c = (array) $config_items['orm']['plurals']; $c += array_filter($plurals, function ($v) use($c) { return in_array($v, $c); }); $config_items['orm']['plurals'] = $c; $config_file = APP_PATH . '/cache/config.json'; \Gini\File::ensureDir(APP_PATH . '/cache'); file_put_contents($config_file, J($config_items)); \Gini\Config::setup(); echo " [32mdone.[0m\n"; }
protected function __preAction($action, &$params) { parent::__preAction($action, $params); $this->view = V(static::$layout_name); $this->view->title = \Gini\Config::get('layout.title'); }
private static function _getOPTableName() { return \Gini\Config::get('hazardous-control-orders.table') ?: '_hazardous_control_order_product'; }
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(); }
public static function diagnose($items = null) { $errors = []; if (!$items || in_array('dependencies', $items)) { echo "Checking module dependencies...\n"; // check gini dependencies foreach (\Gini\Core::$MODULE_INFO as $name => $info) { if (!$info->error) { continue; } $errors['dependencies'][] = "{$name}: {$info->error}"; } if ($errors['dependencies']) { static::_outputErrors($errors['dependencies']); } else { echo " [32mdone.[0m\n"; } echo "\n"; } // check composer requires if (!$items || in_array('composer', $items)) { echo "Checking composer dependencies...\n"; foreach (\Gini\Core::$MODULE_INFO as $name => $info) { if ($info->composer) { if (!file_exists(APP_PATH . '/vendor')) { $errors['composer'][] = $name . ': composer packages missing!'; } break; } } if ($errors['composer']) { static::_outputErrors($errors['composer']); } else { echo " [32mdone.[0m\n"; } echo "\n"; } if (!$items || in_array('file', $items)) { echo "Checking file/directory modes...\n"; // check if /tmp/gini-session is writable $path_gini_session = sys_get_temp_dir() . '/gini-session'; if (is_dir($path_gini_session) && !is_writable($path_gini_session)) { $errors['file'][] = "{$path_gini_session} is not writable!"; } if ($errors['file']) { static::_outputErrors($errors['file']); } else { echo " [32mdone.[0m\n"; } echo "\n"; } if (!$items || in_array('web', $items)) { echo "Checking web dependencies...\n"; if (!file_exists(APP_PATH . '/web')) { $errors['web'][] = "Please run [1m\"gini web update\"[0m to generate web directory!"; } if ($errors['web']) { static::_outputErrors($errors['web']); } else { echo " [32mdone.[0m\n"; } echo "\n"; } // if (!$items || in_array('database', $items)) { $conf = \Gini\Config::get('database'); if (!empty($conf)) { echo "Checking Database...\n"; foreach ((array) $conf as $key => $opts) { $db = \Gini\Database::db($key); $database_errors = $db->diagnose(); if ($database_errors) { static::_outputErrors($module_errors); } else { echo " [32mdone.[0m\n"; } echo "\n"; } } } if (defined('I18N_PATH') && (!$items || in_array('i18n', $items))) { echo "Checking Locale...\n"; $locale = \Gini\Config::get('system.locale') ?: 'en_US'; $lodir = I18N_PATH . '/' . $locale . '/LC_MESSAGES'; $mofile = $lodir . '/' . APP_ID . '.mo'; $pofile = $lodir . '/' . APP_ID . '.po'; if (!file_exists($mofile) || !file_exists($pofile)) { static::_outputErrors(['Please run: gini i18n format ' . $locale]); } else { echo " [32mdone.[0m\n"; } echo "\n"; } // enumerate all doctor extensions if ((!$items || in_array('dependencies', $items) && in_array('module_spec', $items)) && !isset($errors['dependencies'])) { // check gini dependencies foreach (\Gini\Core::$MODULE_INFO as $name => $info) { $class = '\\Gini\\Module\\' . strtr($name, ['-' => '', '_' => '', '/' => '']); $diag_func = "{$class}::diagnose"; if (is_callable($diag_func)) { echo "Checking Module[{$name}]...\n"; $module_errors = call_user_func($diag_func); if ($module_errors) { static::_outputErrors($module_errors); $errors['dependencies'][] = "Module[{$name}] found some error"; } else { echo " [32mdone.[0m\n"; } echo "\n"; } } } return $errors; }