Beispiel #1
0
 function testCalling()
 {
     $manager = new Manager();
     $tid = 1;
     $manager->getConfiguration()->set('Cti\\Direct\\Service', 'list', array('Common\\Api'));
     $response = $manager->call('Cti\\Direct\\Service', 'handle', array('request' => Request::create((object) array('action' => 'Api', 'method' => 'greet', 'tid' => $tid, 'type' => 'type', 'data' => array('Cti')))));
     $this->assertSame($response->getResult(), "Hello, Cti!");
     // test cache by tid
     $responseWithSameTid = $manager->call('Cti\\Direct\\Service', 'handle', array('request' => Request::create((object) array('action' => 'Api', 'method' => 'anotherGreet', 'tid' => $tid, 'type' => 'type', 'data' => array('Cti2')))));
     $this->assertSame($response, $responseWithSameTid);
     // test exception
     $response = $manager->call('Cti\\Direct\\Service', 'handle', array('request' => Request::create((object) array('action' => 'Api', 'method' => 'exception', 'tid' => ++$tid, 'type' => 'type', 'data' => array('message')))));
     $this->assertSame($response->getType(), 'exception');
     $this->assertSame($response->getResult(), 'message');
     $this->assertNotNull($response->getFile());
     $this->assertNotNull($response->getLine());
     // test incorrect action
     $response = $manager->call('Cti\\Direct\\Service', 'handle', array('request' => Request::create((object) array('action' => 'no_action', 'method' => 'no_method', 'tid' => ++$tid, 'type' => 'type', 'data' => array()))));
     $this->assertSame($response->getType(), 'exception');
     $this->assertContains("Action no_action not found", $response->getResult());
     // test incorrect method
     $response = $manager->call('Cti\\Direct\\Service', 'handle', array('request' => Request::create((object) array('action' => 'Api', 'method' => 'no_method', 'tid' => ++$tid, 'type' => 'type', 'data' => array()))));
     $this->assertSame($response->getType(), 'exception');
     $this->assertContains("Method no_method not found", $response->getResult());
 }
Beispiel #2
0
 function post(Manager $manager, Service $service)
 {
     $data = json_decode($GLOBALS['HTTP_RAW_POST_DATA']);
     if (!is_array($data)) {
         $response = $service->handle($manager, Request::create($data));
     } else {
         $response = array();
         foreach ($data as $request) {
             $response[] = $service->handle($manager, Request::create($request));
         }
     }
     header('Content-type: text/javascript');
     echo json_encode($response);
 }
Beispiel #3
0
 /**
  * @param Manager $manager
  * @param Request $request
  * @return Response
  * @throws Exception
  */
 function handle(Manager $manager, Request $request)
 {
     $response = $request->generateResponse();
     if (isset($this->responses[$request->getTid()])) {
         return $this->responses[$request->getTid()];
     }
     try {
         if (!isset($this->classes[$request->getAction()])) {
             throw new Exception(sprintf("Action %s not found", $request->getAction()));
         }
         foreach ($this->actions[$request->getAction()] as $info) {
             if ($request->getMethod() == $info['name']) {
                 $response->setType($request->getType());
                 $response->setResult($manager->call($this->classes[$request->getAction()], $request->getMethod(), $request->getData()));
                 break;
             }
         }
         if (!$response->getType()) {
             throw new Exception(sprintf("Method %s not found", $response->getMethod()));
         }
     } catch (Exception $e) {
         $response->setType('exception');
         $response->setResult($e->getMessage());
         $response->setFile($e->getFile());
         $response->setLine($e->getLine());
     }
     return $this->responses[$response->getTid()] = $response;
 }