/**
  * @param ListenerInterface|callable $listener
  *
  * @throws \BadMethodCallException
  *
  * @return ListenerInterface
  */
 private function ensureListener($listener)
 {
     if ($listener instanceof ListenerInterface) {
         return $listener;
     }
     if (is_callable($listener)) {
         return CallbackListener::fromCallable($listener);
     }
     throw new \BadMethodCallException('Fetched listener neither implements ListenerInterface nor is a callable');
 }
示例#2
0
 public function testResponseReady()
 {
     $emitter = new Emitter();
     $callback = function ($event, $parms) {
         $this->assertInstanceOf(Response::class, $parms['response']);
     };
     $emitter->addListener('response_ready', CallbackListener::fromCallable($callback));
     $api = new Server($emitter);
     $api->registerServiceLoader(new DirectoryServiceLoader(__DIR__ . '/services', '\\TestApi'));
     $request = Request::create('/v1/test/123', 'GET');
     $response = $api->run($request);
 }
示例#3
0
 public function testExceptionsInListenersPropagateOutToApplication()
 {
     $this->expectException(\Exception::class);
     $this->expectExceptionMessage('kaboom!');
     $object = new StatefulObject();
     $object->setCurrentState(FlyweightState::named('WHITES_TURN'));
     $originalException = new \Exception('kaboom!');
     $statemachine = new Statemachine(new ChessMatchTransitionTable());
     $statemachine->before(FlyweightInput::named('WHITE_MOVES'), FlyweightState::named('WHITES_TURN'), CallbackListener::fromCallable(function (Event $event) use($originalException) {
         throw $originalException;
     }));
     $statemachine->trigger(FlyweightInput::named('WHITE_MOVES'), $object);
 }
 /**
  * Ensure the input is a listener.
  *
  * @param ListenerInterface|callable $listener
  *
  * @throws InvalidArgumentException
  *
  * @return ListenerInterface
  */
 protected function ensureListener($listener)
 {
     if ($listener instanceof ListenerInterface) {
         return $listener;
     }
     if (is_callable($listener)) {
         return CallbackListener::fromCallable($listener);
     }
     throw new InvalidArgumentException('Listeners should be ListenerInterface, Closure or callable. Received type: ' . gettype($listener));
 }
示例#5
0
文件: index.php 项目: sforsman/rest
$log = new Logger('API');
$log->pushHandler(new StreamHandler('/tmp/api_log.txt', Logger::WARNING));
// We are interested in some events generated by the server
$emitter = new Emitter();
// This will be emitted right before control is dispatched to the actual service
$callback = function (AbstractEvent $event, $param = null) use($log) {
    // In the real world, you would (for an example) validate OAuth2 headers here
    $log->addNotice(serialize($param));
};
$emitter->addListener('dispatch', CallbackListener::fromCallable($callback));
// This will be emitted when an exception is going to be processed and "converted" into JSON
$callback = function ($event, $param) use($log) {
    $log->addError($param['exception']->getMessage());
};
$emitter->addListener('exception', CallbackListener::fromCallable($callback));
// This will be emitted when an PHP error (warning, notice, fatal) has happened and the processing
$callback = function ($event, $errorStr) use($log) {
    $log->addWarning($errorStr);
};
$emitter->addListener('error', CallbackListener::fromCallable($callback));
// Create the actual REST server
$api = new Server($emitter);
// Use the built-in error handlers that prevent any default PHP behavior and ensure all errors are
// cleanly returned as JSON. For logging purposes you should use the event listeners
$api->registerErrorHandlers();
// Register a REST endpoint/service called page (by default it will be placed in the v1 namespace)
// e.g. https://api.myapp.com/v1/page
$api->register('page', PageService::class);
// Process the request
$response = $api->run();
$response->send();