Example #1
0
 public function log($message, $type, $args = array())
 {
     $errorTypes = array(\Phalcon\Logger::WARNING, \Phalcon\Logger::ERROR, \Phalcon\Logger::ALERT, \Phalcon\Logger::CRITICAL, \Phalcon\Logger::EMERGENCE);
     if (in_array($type, $errorTypes, true)) {
         $this->logger = new \Phalcon\Logger\Adapter\File($this->filepath . ".wf");
     } else {
         $this->logger = new \Phalcon\Logger\Adapter\File($this->filepath);
     }
     $trace = debug_backtrace();
     $depth = count($trace) > 1 ? 1 : 0;
     $current = $trace[$depth];
     $file = basename($current['file']);
     $line = $current['line'];
     $ip = \BullSoft\Utility::getIP();
     unset($trace, $current);
     $message = preg_replace_callback('/%(\\w+)%/', function ($matches) use($file, $line, $ip, $message) {
         return ${$matches[1]};
     }, $this->template);
     if (!empty($args)) {
         if ((bool) getDI()->get('config')->application->debug) {
             $message .= PHP_EOL;
             foreach ($args as $arg) {
                 $message .= $this->logVar($arg);
             }
         } else {
             foreach ($args as $arg) {
                 $message .= ' ||| ';
                 $message .= json_encode($arg);
             }
         }
     }
     $this->logger->log($message, $type);
 }
Example #2
0
function registerModule($module_name)
{
    $module = $GLOBALS['modules'][$module_name];
    require $module["path"];
    $obj = new $module["className"]();
    $obj->registerAutoloaders();
    $obj->registerServices(getDI());
}
Example #3
0
 public function testAction()
 {
     $var = array("foo" => "bar", "bar" => "baz");
     $var1 = "hello";
     $var2 = new \stdClass();
     $var2->hello = "world";
     getDI()->get('logger')->error("Hello World", $var, $var1, $var2);
 }
Example #4
0
 /**
  * Validates the model
  * @return bool
  * @throws ValidationException
  */
 public final function validate()
 {
     $app = getDI();
     $data = $this->getAttributes();
     $rules = $this->rules();
     if ($rules instanceof Assert\Collection) {
         $errors = $app['validator']->validateValue($data, $rules);
         if (count($errors) > 0) {
             throw new ValidationException($errors);
         }
     }
     return true;
 }
Example #5
0
 public static function connect($nodeName)
 {
     $configs = getDI()->get('config');
     $nodes = $configs->database->{$nodeName}->toArray();
     $totalNodes = $configs->database->{$nodeName}->nodes;
     $nodeIds = range(1, intval($totalNodes));
     $nodeId = array_rand($nodeIds);
     $descriptor = array("host" => $nodes['host'][$nodeId], "port" => $nodes['port'][$nodeId], "username" => $nodes['username'][$nodeId], "password" => $nodes['password'][$nodeId], "dbname" => $nodes['dbname'][$nodeId], "options" => array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $nodes['charset'], \PDO::ATTR_TIMEOUT => 3, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
     $connection = null;
     $try = 1;
     RECONNECT:
     try {
         $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($descriptor);
     } catch (\Exception $e) {
         error_log("PHP Fatal error:  BullSoft::Db::connect() failed to connect to MySQL in " . __FILE__ . " on line " . __LINE__ . '. Detail: ' . json_encode($descriptor));
         while (!is_object($connection)) {
             // wait for 100ms
             usleep(100000);
             error_log('PHP Notice: BullSoft::Db::connnect() retry to connect to MySQL for the ' . $try . ' time ... ');
             if ($try++ >= 10) {
                 break;
             }
             goto RECONNECT;
         }
         error_log("PHP Fatal error:  BullSoft::Db::connect() finally failed to connect to MySQL in " . __FILE__ . " on line " . __LINE__);
         throw $e;
     }
     $connection->start = time();
     $waitTimeout = $connection->fetchOne("SHOW VARIABLES LIKE 'wait_timeout'", \Phalcon\Db::FETCH_ASSOC);
     $connection->timeout = intval($waitTimeout['Value']);
     // begin debug mode
     $debug = false;
     $logger = null;
     if ((bool) $configs->application->debug && isset($configs->database->{$nodeName}->logger)) {
         if (!file_exists($configs->database->{$nodeName}->logger)) {
             if (mkdir($configs->database->{$nodeName}->logger, 0777, true)) {
                 $debug = true;
                 $logger = new \Phalcon\Logger\Adapter\File($configs->database->{$nodeName}->logger . date("Ymd"));
             } else {
                 error_log("PHP Notice:  BullSoft::Db::connect() permission denied for creating directory " . $configs->database->{$nodeName}->logger);
             }
         } else {
             $debug = true;
             $logger = new \Phalcon\Logger\Adapter\File($configs->database->{$nodeName}->logger . date("Ymd"));
         }
     }
     // end debug mode
     try {
         // event manager
         $evtManager = new \Phalcon\Events\Manager();
         $evtManager->attach('db', function ($event, $connection) use($logger, $debug) {
             if ($event->getType() == 'beforeQuery') {
                 // check timeout to reconnect
                 $idle = time() - $connection->start;
                 if ($idle >= $connection->timeout) {
                     $connection->connect();
                     $connection->start = time();
                 }
                 $sql = $connection->getSQLStatement();
                 // begin debug mode
                 if ($debug == true) {
                     $variables = $connection->getSqlVariables();
                     if (count($variables)) {
                         $query = preg_replace("/('.*?')/", "", $sql);
                         $query = preg_replace('/(\\?)|(:[0-9a-z_]+)/is', "'%s'", $query);
                         $query = vsprintf($query, $variables);
                         $logger->log($query, \Phalcon\Logger::INFO);
                     } else {
                         $logger->log($sql, \Phalcon\Logger::INFO);
                     }
                 }
                 // end debug mode
             }
         });
         $connection->setEventsManager($evtManager);
     } catch (\Exception $e) {
         // can not throw
         error_log("PHP Notice: BullSoft::Db:connect event attach failed");
     }
     return $connection;
 }
Example #6
0
 public static function destroyCart()
 {
     getDI()->get('session')->remove(self::BULL_CART_KEY);
 }
Example #7
0
 public function insertAction()
 {
     if (!$this->user) {
         $this->flashJson(500, array(), "请先登陆!");
         exit;
     }
     $userId = $this->user->id;
     $name = $this->request->getPost('name');
     $price = $this->request->getPost('price');
     $description = $this->request->getPost('description');
     $from = $this->request->getPost('from');
     $fromUrl = $this->request->getPost('from_url');
     $lImgs = $this->request->getPost('l_imgs');
     $model = new ProductModel();
     $model->name = strval($name);
     $model->image_url = reset($lImgs);
     $model->more_image_urls = json_encode($lImgs);
     $model->description = strval($description);
     $model->price = floatval($price);
     $model->from = strval($from);
     $model->from_url = strval($fromUrl);
     $model->user_id = $userId;
     $model->likeit = 1;
     $model->addtime = $model->modtime = date("Y-m-d H:i:s");
     if ($model->save() == false) {
         $this->flashJson(500, array(), "暂时不能推荐商品!");
         foreach ($model->getMessages() as $message) {
             getDI()->get('logger')->error($message->__toString());
         }
     } else {
         $this->flashJson(200, array('forward' => $this->url->get('goods/detail-') . $model->id . '.html'), "商品推荐成功!");
     }
     exit;
 }