1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace Tebru\Retrofit\Command; |
10
|
|
|
|
11
|
|
|
use GuzzleHttp\Psr7\Response; |
12
|
|
|
use Psr\Http\Message\RequestInterface; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Tebru\Retrofit\HttpClient; |
20
|
|
|
use Tebru\Retrofit\Retrofit; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class CompileCommand |
24
|
|
|
* |
25
|
|
|
* @author Nate Brunette <[email protected]> |
26
|
|
|
* @codeCoverageIgnore |
27
|
|
|
*/ |
28
|
|
|
class CompileCommand extends Command |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Configure command |
32
|
|
|
* |
33
|
|
|
* @throws InvalidArgumentException |
34
|
|
|
*/ |
35
|
|
|
protected function configure(): void |
36
|
|
|
{ |
37
|
|
|
$this->setName('compile'); |
38
|
|
|
$this->setDescription('Compiles and caches all services found in the project'); |
39
|
|
|
$this->addArgument('sourceDirectory', InputArgument::REQUIRED, 'Enter the source directory'); |
40
|
|
|
$this->addArgument('cacheDirectory', InputArgument::REQUIRED, 'Enter the cache directory'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Execute command |
45
|
|
|
* |
46
|
|
|
* @param InputInterface $input |
47
|
|
|
* @param OutputInterface $output |
48
|
|
|
* @return int|null|void |
49
|
|
|
*/ |
50
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
51
|
|
|
{ |
52
|
|
|
$srcDir = $input->getArgument('sourceDirectory'); |
53
|
|
|
$cacheDir = $input->getArgument('cacheDirectory'); |
54
|
|
|
|
55
|
|
|
$clientStub = new class implements HttpClient { |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Send a request synchronously and return a PSR-7 [@see ResponseInterface] |
59
|
|
|
* |
60
|
|
|
* @param RequestInterface $request |
61
|
|
|
* @return ResponseInterface |
62
|
|
|
*/ |
63
|
|
|
public function send(RequestInterface $request): ResponseInterface |
64
|
|
|
{ |
65
|
|
|
return new Response(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Send a request asynchronously |
70
|
|
|
* |
71
|
|
|
* The response callback must be called if any response is returned from the request, and the failure |
72
|
|
|
* callback should only be executed if a request was not completed. |
73
|
|
|
* |
74
|
|
|
* The response callback should pass a PSR-7 [@see ResponseInterface] as the one and only argument. The |
75
|
|
|
* failure callback should pass a [@see Throwable] as the one and only argument. |
76
|
|
|
* |
77
|
|
|
* @param RequestInterface $request |
78
|
|
|
* @param callable $onResponse |
79
|
|
|
* @param callable $onFailure |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function sendAsync(RequestInterface $request, callable $onResponse, callable $onFailure): void |
83
|
|
|
{ |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Calling this method should execute any enqueued requests asynchronously |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function wait(): void |
92
|
|
|
{ |
93
|
|
|
} |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
$retrofit = Retrofit::builder() |
97
|
|
|
->setBaseUrl('') |
98
|
|
|
->setHttpClient($clientStub) |
99
|
|
|
->setCacheDir($cacheDir) |
|
|
|
|
100
|
|
|
->enableCache() |
101
|
|
|
->build(); |
102
|
|
|
$count = $retrofit->createAll($srcDir); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$output->writeln(sprintf('<info>Compiled %s %s successfully</info>', $count, ($count === 1) ? 'class' : 'classes')); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.