Ejemplo n.º 1
0
 public static function setLocale($locale)
 {
     $full_locale = ($locale ?: 'en_US') . '.UTF-8';
     \Gini\Logger::of('core')->debug('locale = {locale}', ['locale' => $full_locale]);
     putenv('LANGUAGE=' . $full_locale);
     putenv('LANG=' . $full_locale);
     setlocale(LC_MESSAGES, $full_locale);
 }
Ejemplo n.º 2
0
Archivo: Cron.php Proyecto: iamfat/gini
 public function actionSchedule()
 {
     // read cron cache
     $cron_cache_file = sys_get_temp_dir() . '/cron_cache_' . sha1(APP_PATH);
     $fh = fopen($cron_cache_file, 'c+');
     if ($fh) {
         if (flock($fh, LOCK_EX | LOCK_NB)) {
             $cron_cache = @json_decode(fread($fh), true) ?: [];
             $cron_config = (array) \Gini\Config::get('cron');
             foreach ($cron_config as $name => $job) {
                 $schedule = $job['schedule'] ?: $job['interval'];
                 $cron = \Cron\CronExpression::factory($schedule);
                 $cache =& $cron_cache[$name];
                 if (isset($cache)) {
                     $next = date_create($cache['next']);
                     $now = date_create('now');
                     if ($next <= $now) {
                         // we have to run it
                         $cache['last_run_at'] = $now->format('c');
                         \Gini\Logger::of('cron')->info('cron run {command}', ['command' => $job['command']]);
                         $pid = pcntl_fork();
                         if ($pid == -1) {
                             continue;
                         } elseif ($pid == 0) {
                             $command_args = \Gini\Util::parseArgs($job['command']);
                             \Gini\CLI::dispatch($command_args);
                             exit;
                         }
                     }
                 }
                 $cache['next'] = $cron->getNextRunDate()->format('c');
             }
             while (pcntl_wait($status) > 0) {
             }
             ftruncate($fh, 0);
             fwrite($fh, J($cron_cache));
             flock($fh, LOCK_UN);
         }
         fclose($fh);
     }
 }
Ejemplo n.º 3
0
 public function post($post_data, $timeout = 5)
 {
     $cookie_file = $this->_cookie->file;
     $ch = curl_init();
     $header = $this->_header;
     $header['Content-Type'] = 'application/json';
     curl_setopt_array($ch, [CURLOPT_COOKIEJAR => $cookie_file, CURLOPT_COOKIEFILE => $cookie_file, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_URL => $this->_url, CURLOPT_AUTOREFERER => false, CURLOPT_FOLLOWLOCATION => false, CURLOPT_CONNECTTIMEOUT => $timeout, CURLOPT_TIMEOUT => $timeout, CURLOPT_RETURNTRANSFER => true, CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'] ?: 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', CURLOPT_HTTPHEADER => $header]);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
     \Gini\Logger::of('core')->debug('RPC => {url}: {data}', ['url' => $this->_url, 'data' => $post_data]);
     $data = curl_exec($ch);
     $errno = curl_errno($ch);
     if ($errno) {
         $message = curl_error($ch);
         curl_close($ch);
         \Gini\Logger::of('core')->error('RPC cURL error: {message}', ['message' => $message]);
         throw IoC::construct('\\Gini\\RPC\\Exception', "transport error: {$message}", -32300);
     }
     curl_close($ch);
     return $data;
 }
Ejemplo n.º 4
0
 /**
  * Bind some events with specified callback with/without weight.
  *
  * @param string $names
  * @param string $return
  * @param string $weight
  */
 public static function bind($names, $return, $weight = 0)
 {
     $names = static::_normalizeNames($names);
     \Gini\Logger::of('core')->debug('{name} <= {return} [{weight}]', ['name' => J($names), 'return' => J($return), 'weight' => $weight]);
     foreach ($names as $name) {
         static::get($name, true)->addHandler($return, $weight);
     }
 }
Ejemplo n.º 5
0
 /**
  * executes an SQL statement in a single function call
  * returning the number of rows affected by the statement.
  *
  * @param string $SQL
  *
  * @return int Number of rows affected
  *
  * @author Jia Huang
  */
 public function exec($SQL)
 {
     \Gini\Logger::of('core')->debug('Database exec = {SQL}', ['SQL' => preg_replace('/\\s+/', ' ', $SQL)]);
     return $this->_driver->exec($SQL);
 }
 public static function getRPC($type = 'lab-inventory')
 {
     $confs = \Gini\Config::get('app.rpc');
     $conf = $confs[$type] ?: [];
     if (!self::$_RPCs[$type]) {
         $rpc = \Gini\IoC::construct('\\Gini\\RPC', $conf['url']);
         self::$_RPCs[$type] = $rpc;
         if ($type == 'lab-inventory') {
             $token = $rpc->mall->authorize($conf['client_id'], $conf['client_secret']);
             if (!$token) {
                 \Gini\Logger::of('lab-orders')->error('Mall\\RObject getRPC: authorization failed with {client_id}/{client_secret} !', ['client_id' => $conf['client_id'], 'client_secret' => $conf['client_secret']]);
             }
         }
     }
     return self::$_RPCs[$type];
 }