Beispiel #1
0
 /**
  * {@inheritDoc}
  */
 public function extractAction(ActionInterface $action)
 {
     $callable = $this->callableResolver->resolve($action);
     if ($this->parameterExtractor) {
         $parameters = $this->parameterExtractor->extract($action, $callable);
     } else {
         $parameters = [];
     }
     $reflection = $callable->getReflection();
     $docBlock = new DocBlock($reflection);
     $description = $docBlock->getShortDescription();
     $actionDoc = new Action($action->getName(), $description, $parameters);
     return $actionDoc;
 }
Beispiel #2
0
 /**
  * {@inheritDoc}
  */
 public function handle($method, array $parameters)
 {
     try {
         // Get action and callable
         $action = $this->actionManager->getAction($method);
         $callable = $this->callableResolver->resolve($action);
         // Resolve parameters
         $parameters = $this->parameterResolver->resolve($action, $callable, $parameters);
         // Dispatch "pre dispatch" event
         $event = new ActionDispatchEvent($action, $callable, $parameters);
         $this->eventDispatcher->dispatch(ApiEvents::ACTION_PRE_DISPATCH, $event);
         // Call to API method
         $response = $callable->apply($parameters);
         if (null === $response) {
             throw new \RuntimeException(sprintf('The callable "%s" should be return Response or any values. Can not be empty.', Reflection::getCalledMethod($callable->getReflection())));
         }
         if (!$response instanceof ResponseInterface) {
             // Try transform in listeners.
             $event = new ActionViewEvent($action, $callable, $parameters, $response);
             $this->eventDispatcher->dispatch(ApiEvents::ACTION_VIEW, $event);
             $response = $event->getResponse();
             if (!$response) {
                 throw new \RuntimeException(sprintf('Not found response after dispatch view event in API method "%s". ' . 'You must return response or transform response in view event.', Reflection::getCalledMethod($callable->getReflection())));
             }
             if (!$response instanceof ResponseInterface) {
                 throw new \RuntimeException(sprintf('The response after dispatch view event must be a ResponseInterface instance, but "%s" given ' . 'for method "%s".', is_object($response) ? get_class($response) : gettype($response)));
             }
         }
         $event = new ActionDispatchEvent($action, $callable, $parameters, $response);
         $this->eventDispatcher->dispatch(ApiEvents::ACTION_POST_DISPATCH, $event);
         return $response;
     } catch (\Exception $e) {
         $event = new ActionExceptionEvent(isset($action) ? $action : null, $e);
         $this->eventDispatcher->dispatch(ApiEvents::ACTION_EXCEPTION, $event);
         if ($event->hasResponse()) {
             return $event->getResponse();
         }
         throw $e;
     }
 }