public printer ( ) : |
||
리턴 |
/** * Handle request url code generation * * @param HandlerContext $context * @return null * @throws LogicException */ public function __invoke(HandlerContext $context) { $baseUrl = $context->annotations()->getBaseUrl() ?: '$this->baseUrl'; $queryMap = $context->annotations()->getQueryMap(); $queries = $context->annotations()->getQueries(); $uri = $context->annotations()->getRequestUri(); // if there aren't queries or a query map, just set request url if (null === $queryMap && null === $queries) { $context->body()->add('$requestUrl = %s . "%s";', $baseUrl, $uri); return null; } // if there's a query map, check to see if there are also queries if (null !== $queryMap) { // if we have regular queries, add them to the query builder if (null !== $queries) { $queryArray = $context->printer()->printArray($queries); $context->body()->add('$queryArray = \\Tebru\\Retrofit\\Generation\\Manipulator\\QueryManipulator::boolToString(%s + %s);', $queryArray, $queryMap); $context->body()->add('$queryString = http_build_query($queryArray);'); } else { $context->body()->add('$queryArray = \\Tebru\\Retrofit\\Generation\\Manipulator\\QueryManipulator::boolToString(%s);', $queryMap); $context->body()->add('$queryString = http_build_query($queryArray);'); } $context->body()->add('$requestUrl = %s . "%s?" . $queryString;', $baseUrl, $uri); return null; } // add queries to request url $queryArray = $context->printer()->printArray($queries); $context->body()->add('$queryArray = \\Tebru\\Retrofit\\Generation\\Manipulator\\QueryManipulator::boolToString(%s);', $queryArray); $context->body()->add('$queryString = http_build_query($queryArray);'); $context->body()->add('$requestUrl = %s . "%s" . "?" . $queryString;', $baseUrl, $uri); }
public function testGetters() { $annotations = Mockery::mock(AnnotationProvider::class); $body = Mockery::mock(Body::class); $printer = Mockery::mock(ArrayPrinter::class); $context = new HandlerContext($annotations, $body, $printer); $this->assertSame($annotations, $context->annotations()); $this->assertSame($body, $context->body()); $this->assertSame($printer, $context->printer()); }
/** * Create request object and handle send/response * * @param HandlerContext $context * @return null * @throws LogicException * @throws RetrofitException */ public function __invoke(HandlerContext $context) { $callback = $context->annotations()->getCallback(); if ($callback !== null) { if ($context->annotations()->isCallbackOptional()) { $context->body()->add('if (%s !== null) {', $callback); $context->body()->add('$returnEvent = new \\Tebru\\Retrofit\\Event\\ReturnEvent(null);'); $context->body()->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);'); $context->body()->add('return $returnEvent->getReturn();'); $context->body()->add('}'); } else { $context->body()->add('$returnEvent = new \\Tebru\\Retrofit\\Event\\ReturnEvent(null);'); $context->body()->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);'); $context->body()->add('return $returnEvent->getReturn();'); return; } } $returnType = 'array'; if (null !== $context->annotations()->getReturnType()) { $returnType = $context->annotations()->getReturnType(); } $responseReturn = null !== $context->annotations()->getResponseType(); if ($responseReturn) { $returnType = $context->annotations()->getResponseType(); } $deserializationContext = null !== $context->annotations()->getDeserializationContext() ? $context->annotations()->getDeserializationContext() : []; if ('Response' === $returnType && false === $responseReturn) { throw new RetrofitException('A method return a Response must include a @ResponseType annotation.'); } $context->body()->add('$retrofitResponse = new \\Tebru\\Retrofit\\Http\\Response($response, "%s", $this->deserializerAdapter, %s);', $returnType, $context->printer()->printArray($deserializationContext)); if ($responseReturn) { $context->body()->add('$return = $retrofitResponse;'); } else { $context->body()->add('$return = $retrofitResponse->body();'); } $context->body()->add('$returnEvent = new \\Tebru\\Retrofit\\Event\\ReturnEvent($return);'); $context->body()->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);'); $context->body()->add('return $returnEvent->getReturn();'); }
/** * Create serialization context * * @param HandlerContext $context * @param string $contextVar * @return null */ private function createContext(HandlerContext $context, $contextVar) { $serializationContext = $context->annotations()->getSerializationContext(); $context->body()->add('%s = \\JMS\\Serializer\\SerializationContext::create();', $contextVar); if (0 === count($serializationContext)) { return null; } if (!empty($serializationContext['groups'])) { $context->body()->add('%s->setGroups(%s);', $contextVar, $context->printer()->printArray($serializationContext['groups'])); } if (!empty($serializationContext['version'])) { $context->body()->add('%s->setVersion(%d);', $contextVar, (int) $serializationContext['version']); } if (!empty($serializationContext['serializeNull'])) { $serializeNull = QueryManipulator::boolToString([(bool) $serializationContext['serializeNull']])[0]; $context->body()->add('%s->setSerializeNull(%s);', $contextVar, $serializeNull); } if (!empty($serializationContext['enableMaxDepthChecks'])) { $context->body()->add('%s->enableMaxDepthChecks();', $contextVar); } if (!empty($serializationContext['attributes'])) { foreach ($serializationContext['attributes'] as $key => $value) { $context->body()->add('%s->setAttribute("%s", "%s");', $contextVar, $key, $value); } } }
/** * Create serialization context * * @param HandlerContext $context * @param string $contextVar * @return void */ private function createContext(HandlerContext $context, $contextVar) { $serializationContext = $context->annotations()->getSerializationContext(); if (null === $serializationContext) { $serializationContext = []; } $context->body()->add('%s = %s;', $contextVar, $context->printer()->printArray($serializationContext)); }