/** * {@inheritDoc} */ public function loadMetadata($class) { // Try get object annotation from class $objectAnnotation = null; $classAnnotations = Reflection::loadClassAnnotations($this->reader, $class, true); foreach ($classAnnotations as $classAnnotation) { if ($classAnnotation instanceof ObjectAnnotation) { if ($objectAnnotation) { throw new \RuntimeException(sprintf('Many @Transformer\\Object annotation in class "%s".', $class)); } $objectAnnotation = $classAnnotation; } } if (!$objectAnnotation) { throw new TransformAnnotationNotFoundException(sprintf('Not found @Object annotations in class "%s".', $class)); } // Try get properties annotations $properties = []; $classProperties = Reflection::getClassProperties($class, true); foreach ($classProperties as $classProperty) { $propertyAnnotations = $this->reader->getPropertyAnnotations($classProperty); foreach ($propertyAnnotations as $propertyAnnotation) { if ($propertyAnnotation instanceof PropertyAnnotation) { $property = new PropertyMetadata($propertyAnnotation->propertyName ?: $classProperty->getName(), $propertyAnnotation->groups, $propertyAnnotation->shouldTransform, $propertyAnnotation->expressionValue); $properties[$classProperty->getName()] = $property; } } } return new ObjectMetadata($objectAnnotation->transformedClass, $objectAnnotation->evaluateConstructor, $properties); }
/** * {@inheritDoc} */ public function transform($object, ContextInterface $context) { $metadata = $this->metadataFactory->loadMetadata(get_class($object)); // Get properties for transformation if (!$context->getGroups()) { $transformProperties = $metadata->getProperties(); } else { $transformProperties = $metadata->getPropertiesForGroups($context->getGroups()); } // Try create transformed $transformedClass = $metadata->getTransformedClass(); $transformedReflection = Reflection::loadClassReflection($transformedClass); $transformed = $metadata->isEvaluateConstructor() ? $transformedReflection->newInstance() : $transformedReflection->newInstanceWithoutConstructor(); foreach ($transformProperties as $transformPropertyName => $propertyMetadata) { try { $objectPropertyReflection = Reflection::loadPropertyReflection($object, $transformPropertyName); } catch (\ReflectionException $e) { throw new \RuntimeException(sprintf('Error transform property: Not found property "%s" in class "%s".', $transformPropertyName, get_class($object)), 0, $e); } try { $transformedPropertyReflection = $transformedReflection->getProperty($propertyMetadata->getPropertyName()); } catch (\ReflectionException $e) { throw new \RuntimeException(sprintf('Error transform property: Not found property "%s" in class "%s".', $propertyMetadata->getPropertyName(), $transformedReflection->getName())); } if (!$transformedPropertyReflection->isPublic()) { $transformedPropertyReflection->setAccessible(true); } if (!$objectPropertyReflection->isPublic()) { $objectPropertyReflection->setAccessible(true); } $objectPropertyValue = $objectPropertyReflection->getValue($object); $transformedValue = $this->transformValue($object, $objectPropertyValue, $propertyMetadata, $transformedPropertyReflection); $transformedPropertyReflection->setValue($transformed, $transformedValue); } return $transformed; }
/** * {@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; } }
/** * 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; }
/** * 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); }