Esempio n. 1
0
 /**
  * @param RequestCollection $directRequest
  * @param HttpRequest       $httpRequest
  * @return ResponseCollection
  */
 public function handle(RequestCollection $directRequest, HttpRequest $httpRequest)
 {
     $this->dispatchEvent(RouterEvents::BEGIN_REQUEST, new BeginRequestEvent($directRequest, $httpRequest));
     $invocations = $this->prepareInvocation($directRequest, $httpRequest);
     $responses = array();
     foreach ($invocations as $invocation) {
         /** @var ServiceReference $service */
         /** @var array $arguments */
         list($service, $arguments, $singleRequest) = $invocation;
         try {
             $result = $this->invokeService($service, $arguments, $singleRequest, $httpRequest);
             if ($result instanceof Response) {
                 $responses[] = $result;
             } else {
                 $responses[] = RPCResponse::fromRequest($singleRequest, $result);
             }
         } catch (\Exception $e) {
             /** @var ExceptionEvent $exceptionEvent */
             $exceptionEvent = $this->dispatchEvent(RouterEvents::EXCEPTION, new ExceptionEvent($singleRequest, $httpRequest, $e, ExceptionResponse::fromRequest($singleRequest, $e, $this->debug), isset($service) ? $service : null, isset($arguments) ? $arguments : null));
             $responses[] = $exceptionEvent->getResponse();
         }
     }
     /** @var EndRequestEvent $endRequestEvent */
     $endRequestEvent = $this->dispatchEvent(RouterEvents::END_REQUEST, new EndRequestEvent($directRequest, new ResponseCollection($responses), $httpRequest));
     return $endRequestEvent->getDirectResponse();
 }
Esempio n. 2
0
 public function testCreateFromRequest()
 {
     $request = new Request(2, 'My.Action', 'method', array(1, 2, 3), false, false);
     $response = RPCResponse::fromRequest($request, array(4, 5, 6));
     $this->assertEquals('{"type":"rpc","tid":2,"action":"My.Action","method":"method","result":[4,5,6]}', json_encode($response));
 }