Пример #1
0
 public function testEventWithSender()
 {
     $obj_1 = new \stdClass();
     $obj_1->data = "A";
     $obj_2 = new \stdClass();
     $obj_2->data = "B";
     $obj_3 = new \stdClass();
     $obj_3->data = "C";
     Event::connect('\\photon\\tests\\eventTest\\EventTest::testEventWithSender', '\\photon\\tests\\eventTest\\StaticAction::inc', $obj_1);
     Event::connect('\\photon\\tests\\eventTest\\EventTest::testEventWithSender', '\\photon\\tests\\eventTest\\StaticAction::inc', $obj_2);
     $i = 0;
     $j = 0;
     Event::send('\\photon\\tests\\eventTest\\EventTest::testEventWithSender', $obj_1, $i);
     $this->assertequals($i, 1);
     $this->assertequals($j, 0);
     Event::send('\\photon\\tests\\eventTest\\EventTest::testEventWithSender', $obj_2, $j);
     $this->assertequals($i, 1);
     $this->assertequals($j, 1);
     Event::send('\\photon\\tests\\eventTest\\EventTest::testEventWithSender', null, $i);
     Event::send('\\photon\\tests\\eventTest\\EventTest::testEventWithSender', $obj_3, $j);
     $this->assertequals($i, 1);
     $this->assertequals($j, 1);
 }
Пример #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')));
 }
Пример #3
0
 /**
  * Dispatch a Photon request object and returns the request
  * object and the response object.
  *
  * @param $req Photon request object.
  * @return array(Photon request, Photon response)
  */
 public static function dispatch($req)
 {
     Timer::start('photon.dispatch');
     // FUTUREOPT: One can generate the lists at the initialisation
     // of the server to avoid the repetitive calls to
     // method_exists.
     $middleware = array();
     foreach (Conf::f('middleware_classes', array()) as $mw) {
         $middleware[] = new $mw();
     }
     $response = false;
     try {
         foreach ($middleware as $mw) {
             if (method_exists($mw, 'process_request')) {
                 $response = $mw->process_request($req);
                 if ($response !== false) {
                     // $response is a response, the middleware has
                     // preempted the request and the possible
                     // corresponding view will not called.
                     break;
                 }
             }
         }
         if ($response === false) {
             $response = self::match($req);
         }
         $middleware = array_reverse($middleware);
         foreach ($middleware as $mw) {
             if (method_exists($mw, 'process_response')) {
                 $response = $mw->process_response($req, $response);
             }
         }
     } catch (\Exception $e) {
         Event::send('\\photon\\core\\Dispatcher::dispatchException', null, $e);
         if (true !== Conf::f('debug', false)) {
             $response = new \photon\http\response\ServerError($e, $req);
         } else {
             $response = new \photon\http\response\ServerErrorDebug($e->getMessage());
             $response->setContent($e, $req);
         }
     }
     $view_name = isset($req->view[0]['name']) ? $req->view[0]['name'] : 'not_defined';
     Log::perf(array('photon.dispatch', $req->uuid, Timer::stop('photon.dispatch'), $view_name, array($req->method, $req->path)));
     return array($req, $response);
 }
Пример #4
0
 function start($eventName, $params = array())
 {
     $sender = null;
     $request = $this->context->get('request');
     if ('' !== $request) {
         $sender = isset($request->view[0]['name']) ? $request->view[0]['name'] : null;
         $params = array_merge($params, array('request' => $request));
     }
     \photon\event\Event::send($eventName, $sender, $params);
 }
Пример #5
0
 public function testConfigureCompilerFromEvent()
 {
     Event::connect('\\photon\\template\\compiler\\Compiler::construct_load_tags', '\\photon\\tests\\template\\rendererTest\\LocalCompiler::setupTags');
     Event::connect('\\photon\\template\\compiler\\Compiler::construct_load_modifiers', '\\photon\\tests\\template\\rendererTest\\LocalCompiler::setupModifiers');
     $renderer = new template\Renderer('data-template-custom-tag.html', array(__DIR__));
     $this->assertequals("E=mc²\n", $renderer->render());
     $renderer = new template\Renderer('data-template-custom-modifier.html', array(__DIR__));
     $this->assertequals("deadbeaf\n", $renderer->render(new template\Context(array('value' => 0xdeadbeaf))));
 }
Пример #6
0
 /**
  * Construct the compiler.
  *
  * @param $template_file string Basename of the template file
  * @param $folders array Source folders of the templates
  * @param $options array
  */
 function __construct($template_file, $folders, $options = array())
 {
     $this->_sourceFile = $template_file;
     $this->templateFolders = $folders;
     $options = array_merge(array('load' => true, 'tags' => array(), 'modifiers' => array()), $options);
     $this->_allowedTags = array_merge($this->_allowedTags, $options['tags'], Conf::f('template_tags', array()));
     Event::send('\\photon\\template\\compiler\\Compiler::construct_load_tags', null, $this->_allowedTags);
     $this->_modifier = array_merge($this->_modifier, $options['modifiers'], Conf::f('template_modifiers', array()));
     Event::send('\\photon\\template\\compiler\\Compiler::construct_load_modifiers', null, $this->_modifier);
     foreach ($this->_allowedTags as $name => $model) {
         $this->_extraTags[$name] = new $model();
     }
     $this->_allowedInVar = array_merge($this->_vartype, $this->_op);
     $this->_allowedInExpr = array_merge($this->_vartype, $this->_op);
     $this->_allowedAssign = array_merge($this->_vartype, $this->_assignOp, $this->_op);
     if ($options['load']) {
         $this->sourceFiles[] = $this->loadTemplateFile($this->_sourceFile);
     }
 }