/** * Generate a REST client based on an interface * * @param ClassMetaDataProvider $classMetaDataProvider * @param GeneratedClassMetaDataProvider $generatedClassMetaDataProvider * @return string */ public function generate(ClassMetaDataProvider $classMetaDataProvider, GeneratedClassMetaDataProvider $generatedClassMetaDataProvider) { // get interface methods $parsedMethods = $classMetaDataProvider->getInterfaceMethods(); // loop through class annotations $class = new ClassModel(); foreach ($classMetaDataProvider->getClassAnnotations() as $classAnnotation) { if ($classAnnotation instanceof Headers) { $class->addHeaders($classAnnotation->getHeaders()); } } // loop over class methods foreach ($classMetaDataProvider->getReflectionMethods() as $index => $classMethod) { $parsedMethod = array_shift($parsedMethods); $parameters = $classMethod->getParameters(); $method = new Method(); $method->setName($classMetaDataProvider->getMethodName($parsedMethod)); $method->setDeclaration($classMetaDataProvider->getMethodDeclaration($parsedMethod)); // loop through method annotations foreach ($classMetaDataProvider->getMethodAnnotations($classMethod) as $methodAnnotation) { if ($methodAnnotation instanceof HttpRequest) { foreach ($methodAnnotation->getParameters() as $parameter) { $this->assertParameter($parameters, $parameter); } } if ($methodAnnotation instanceof AnnotationToVariableMap) { $this->assertParameter($parameters, $methodAnnotation->getName()); } $handler = $this->annotationHandlerFactory->make($methodAnnotation); $handler->handle($method, $methodAnnotation); } $methodOptions = $method->getOptions(); $methodParts = $method->getParts(); Tebru\assert(null !== $method->getType(), new GenerationException('During Generation, the http method annotation was not found.')); Tebru\assert(empty($methodOptions['body']) || empty($methodParts), new GenerationException(sprintf('During Generation, both a @Body and @Part annotation were found on method "%s"', $method->getName()))); $class->addMethod($method); } // render class as string $template = $this->twig->loadTemplate('service.php.twig'); return $template->render(['uses' => $classMetaDataProvider->getUseStatements(), 'namespace' => $generatedClassMetaDataProvider->getNamespaceFull(), 'className' => $classMetaDataProvider->getInterfaceNameShort(), 'interfaceName' => $classMetaDataProvider->getInterfaceNameFull(), 'methods' => $class->getMethods()]); }
/** * @param Method $method * @param QueryMap $annotation * @return null */ public function handle(Method $method, $annotation) { $method->addQueryMap($annotation->getValue()); }
/** * @param Method $method * @param JsonBody $annotation * @return null */ public function handle(Method $method, $annotation) { $method->setJsonBody(true); }
/** * @param Method $method * @param Query $annotation * @return null */ public function handle(Method $method, $annotation) { $method->addQueries([$annotation->getKey() => $annotation->getValue()]); }
/** * Will set annotation data to $method * * @param Method $method * @param DeserializationContext $annotation * * @return null */ public function handle(Method $method, $annotation) { $method->setDeserializationContext(['depth' => $annotation->getDepth(), 'groups' => $annotation->getGroups(), 'serializeNull' => $annotation->getSerializeNull(), 'version' => $annotation->getVersion(), 'attributes' => $annotation->getAttributes()]); }
/** * @param Method $method * @param Headers $annotation * @return null */ public function handle(Method $method, $annotation) { $method->addHeaders($annotation->getHeaders()); }
/** * @param Method $method * @param HttpRequest $annotation * @return null */ public function handle(Method $method, $annotation) { $method->setType($annotation->getType()); $method->setPath($annotation->getPath()); $method->addQueries($annotation->getQueries()); }
/** * @param Method $method * @param Returns $annotation * @return null */ public function handle(Method $method, $annotation) { $method->setReturn($annotation->getReturn()); }
/** * @param Method $method * @param Url $annotation * @return null */ public function handle(Method $method, $annotation) { $method->setUrl($annotation->getValue()); }