Ejemplo n.º 1
0
 public function testLog()
 {
     Log::setLevel('ALL');
     Conf::set('log_delayed', true);
     Conf::set('log_handlers', array('\\photon\\log\\NullBackend'));
     $message = 'dummy message';
     $i = 0;
     Log::plog($message);
     $i++;
     $this->assertEquals($i, count(Log::$stack));
     Log::debug($message);
     $i++;
     $this->assertEquals($i, count(Log::$stack));
     Log::info($message);
     $i++;
     $this->assertEquals($i, count(Log::$stack));
     Log::perf($message);
     $i++;
     $this->assertEquals($i, count(Log::$stack));
     Log::event($message);
     $i++;
     $this->assertEquals($i, count(Log::$stack));
     Log::warn($message);
     $i++;
     $this->assertEquals($i, count(Log::$stack));
     Log::error($message);
     $i++;
     $this->assertEquals($i, count(Log::$stack));
     Log::fatal($message);
     $i++;
     $this->assertEquals($i, count(Log::$stack));
     FileBackend::$return = true;
     Log::flush();
     $this->assertEquals(0, count(Log::$stack));
     FileBackend::$return = false;
     Log::flush();
     $this->assertEquals(0, count(Log::$stack));
     Log::flush();
     $this->assertEquals(0, count(Log::$stack));
     Conf::set('log_delayed', false);
     Log::info($message);
     $this->assertEquals(0, count(Log::$stack));
 }
Ejemplo n.º 2
0
 /**
  * Process the request available on the socket.
  *
  * The socket is available for reading with recv().
  */
 public function processRequest($conn)
 {
     Timer::start('photon.process_request');
     $uuid = request_uuid();
     $mess = $conn->recv();
     if ($mess->is_disconnect()) {
         // A client disconnect from mongrel2 before a answer was send
         // Use this event to cleanup your context (long polling socket, task queue, ...)
         $event_params = array('conn_id' => $mess->conn_id);
         Event::send('\\photon\\server\\Server\\processRequest::disconnect', null, $event_params);
     } else {
         // This could be converted to use server_id + listener
         // connection id, it will wrap but should provide enough
         // uniqueness to track the effect of a request in the app.
         $req = new \photon\http\Request($mess);
         $req->uuid = $uuid;
         $req->conn = $conn;
         list($req, $response) = \photon\core\Dispatcher::dispatch($req);
         // If the response is false, the view is simply not
         // sending an answer, most likely the work was pushed to
         // another backend. Yes, you do not need to reply after a
         // recv().
         if (false !== $response) {
             if (is_string($response->content)) {
                 $conn->reply($mess, $response->render());
             } else {
                 Log::debug(array('photon.process_request', $uuid, 'SendIterable'));
                 $response->sendIterable($mess, $conn);
             }
         }
     }
     unset($mess);
     // Cleans the memory with the __destruct call.
     Log::perf(array('photon.process_request', $uuid, Timer::stop('photon.process_request')));
 }
Ejemplo n.º 3
0
Archivo: db.php Proyecto: photon/photon
 /**
  * The definition to get a connection is:
  *
  * Required:
  *
  * - server (mapped to host in the connection string);
  * - database (mapped to dbname in the connection string);
  *
  * Optional:
  *
  * - user;
  * - hostaddr;
  * - port;
  * - password;
  * - connect_timeout;
  * - options;
  * - sslmode;
  * - service.
  *
  * You can read more about it here: http://www.php.net/pg_connect
  */
 public static function get($def)
 {
     $user = null;
     $password = null;
     $allowed_cfg = array('host', 'dbname', 'user', 'hostaddr', 'port', 'password', 'connect_timeout', 'options', 'sslmode', 'service');
     $cfgs = array();
     $opts = array();
     foreach ($def as $key => $value) {
         if ('engine' === $key) {
             continue;
         }
         $key = str_replace(array('server', 'database'), array('host', 'dbname'), $key);
         if ('user' == $key) {
             $user = $value;
         } elseif ('password' == $key) {
             $password = $value;
         } elseif (in_array($key, $allowed_cfg)) {
             $cfgs[] = $key . '=' . $value;
         } else {
             $opts[$key] = $value;
         }
     }
     Log::debug(array('photon.db.PostgreSQL.get', $cfgs, $user, $password, $opts));
     return new \PDO('pgsql:' . implode(';', $cfgs), $user, $password, $opts);
 }
Ejemplo n.º 4
0
 /**
  * Call the view found by self::match.
  *
  * The called view can throw an exception. This is fine and
  * normal.
  *
  * @param $req Photon request 
  * @param $ctl The url definition matching the request
  * @param $matches The matches found by preg_match
  * @return mixed Response or None
  */
 public static function send($req, $ctl, $match)
 {
     Log::debug(array('photon.dispatch.send', $req->uuid, array($ctl, $match)));
     $req->view = array($ctl, $match);
     if (is_array($ctl['view'])) {
         list($mn, $mv) = $ctl['view'];
         $m = new $mn();
         if (isset($m->{$mv . '_precond'})) {
             // Preconditions to respects. A precondition must return
             // true or a response object.
             $preconds = $m->{$mv . '_precond'};
             foreach ($preconds as $precond) {
                 $res = call_user_func_array($precond, array(&$req));
                 if ($res !== true) {
                     return $res;
                 }
             }
         }
         if (!isset($ctl['params'])) {
             return $m->{$mv}($req, $match);
         } else {
             return $m->{$mv}($req, $match, $ctl['params']);
         }
     } else {
         // simple callable function
         $v = $ctl['view'];
         if (!isset($ctl['params'])) {
             return $v($req, $match);
         } else {
             return $v($req, $match, $ctl['params']);
         }
     }
 }
Ejemplo n.º 5
0
 public function loop()
 {
     // Execute the back ground task, only one time per seconds
     static $lastRefresh = 0;
     $now = time();
     if ($now == $lastRefresh) {
         return;
     }
     $lastRefresh = $now;
     Log::debug('Connections actives to mongrel2: ' . count($this->connections));
     foreach ($this->connections as $connectionIndex => $connection) {
         Log::debug('#' . $connectionIndex . ' Jobs: ' . count($this->jobs[$connectionIndex]));
         $this->_loop($connectionIndex, $connection, $this->jobs[$connectionIndex]);
     }
 }