/** * Resolve request parameter * * @param ActionInterface $action * @param CallableInterface $callable * @param array $inputArguments * @param \ReflectionParameter $parameter * * @return object * * @throws \Exception */ private function resolveRequest(ActionInterface $action, CallableInterface $callable, array $inputArguments, \ReflectionParameter $parameter) { $class = $parameter->getClass(); if ($class->isInterface()) { throw new \RuntimeException(sprintf('Could not create instance via interface for parameter "%s" in method "%s". ' . 'You must set the class for type hinting.', $parameter->getName(), Reflection::getCalledMethod($callable->getReflection()))); } if ($class->isAbstract()) { throw new \RuntimeException(sprintf('Could not create instance via abstract class for parameter "%s" in method "%s". ' . 'You must set the real class for type hinting.', $parameter->getName(), Reflection::getCalledMethod($callable->getReflection()))); } /** @var RequestInterface $request */ $request = $class->newInstance(); // Map arguments $this->objectMapper->map($request, $inputArguments, ObjectMetadata::DEFAULT_GROUP); // Validate process if ($this->validator) { if ($this->validator instanceof VarTagValidatorInterface) { $violationList = $this->validator->validateObjectByVarTags($request); if (count($violationList)) { throw ViolationListException::create($violationList); } } $violationList = $this->validator->validate($request); if (count($violationList)) { throw ViolationListException::create($violationList); } } return $request; }
/** * {@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; } }
/** * On pre dispatch event * * @param ActionDispatchEvent $event */ public function onPreDispatch(ActionDispatchEvent $event) { $message = sprintf('Match callable "%s" for action "%s".', Reflection::getCalledMethod($event->getCallable()->getReflection()), $event->getAction()->getName()); $this->logger->debug($message); }