1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2015 Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tebru\Retrofit\Generation\Handler; |
8
|
|
|
|
9
|
|
|
use LogicException; |
10
|
|
|
use Tebru\Retrofit\Exception\RetrofitException; |
11
|
|
|
use Tebru\Retrofit\Generation\Handler; |
12
|
|
|
use Tebru\Retrofit\Generation\HandlerContext; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ReturnHandler |
16
|
|
|
* |
17
|
|
|
* @author Nate Brunette <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class ReturnHandler implements Handler |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Create request object and handle send/response |
23
|
|
|
* |
24
|
|
|
* @param HandlerContext $context |
25
|
|
|
* @return null |
26
|
|
|
* @throws LogicException |
27
|
|
|
* @throws RetrofitException |
28
|
|
|
*/ |
29
|
|
|
public function __invoke(HandlerContext $context) |
30
|
|
|
{ |
31
|
|
|
$callback = $context->annotations()->getCallback(); |
32
|
|
|
|
33
|
|
View Code Duplication |
if ($callback !== null) { |
34
|
|
|
if ($context->annotations()->isCallbackOptional()) { |
35
|
|
|
$context->body()->add('if (%s !== null) {', $callback); |
36
|
|
|
$context->body()->add('$returnEvent = new \Tebru\Retrofit\Event\ReturnEvent(null);'); |
37
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);'); |
38
|
|
|
$context->body()->add('return $returnEvent->getReturn();'); |
39
|
|
|
$context->body()->add('}'); |
40
|
|
|
} else { |
41
|
|
|
$context->body()->add('$returnEvent = new \Tebru\Retrofit\Event\ReturnEvent(null);'); |
42
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);'); |
43
|
|
|
$context->body()->add('return $returnEvent->getReturn();'); |
44
|
|
|
|
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$returnType = 'array'; |
50
|
|
|
if (null !== $context->annotations()->getReturnType()) { |
51
|
|
|
$returnType = $context->annotations()->getReturnType(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$responseReturn = (null !== $context->annotations()->getResponseType()); |
55
|
|
|
if ($responseReturn) { |
56
|
|
|
$returnType = $context->annotations()->getResponseType(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$deserializationContext = (null !== $context->annotations()->getDeserializationContext()) |
60
|
|
|
? $context->annotations()->getDeserializationContext() |
61
|
|
|
: []; |
62
|
|
|
|
63
|
|
|
if ('Response' === $returnType && false === $responseReturn) { |
64
|
|
|
throw new RetrofitException('A method return a Response must include a @ResponseType annotation.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$context->body()->add( |
68
|
|
|
'$retrofitResponse = new \Tebru\Retrofit\Http\Response($response, "%s", $this->serializer, %s);', |
69
|
|
|
$returnType, |
70
|
|
|
$context->printer()->printArray($deserializationContext) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
if ($responseReturn) { |
74
|
|
|
$context->body()->add('$return = $retrofitResponse;'); |
75
|
|
|
} else { |
76
|
|
|
$context->body()->add('$return = $retrofitResponse->body();'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$context->body()->add('$returnEvent = new \Tebru\Retrofit\Event\ReturnEvent($return);'); |
80
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);'); |
81
|
|
|
$context->body()->add('return $returnEvent->getReturn();'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|