|
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; |
|
10
|
|
|
|
|
11
|
|
|
use LogicException; |
|
12
|
|
|
use Tebru\Retrofit\Finder\ServiceResolver; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Retrofit |
|
16
|
|
|
* |
|
17
|
|
|
* @author Nate Brunette <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Retrofit |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Registered services |
|
23
|
|
|
* |
|
24
|
|
|
* @var array $services |
|
25
|
|
|
*/ |
|
26
|
|
|
private $services = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Finds all services in a given source directory |
|
30
|
|
|
* |
|
31
|
|
|
* @var ServiceResolver $serviceResolver |
|
32
|
|
|
*/ |
|
33
|
|
|
private $serviceResolver; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* An array of proxy factories |
|
37
|
|
|
* |
|
38
|
|
|
* @var ProxyFactory[] |
|
39
|
|
|
*/ |
|
40
|
|
|
private $proxyFactories; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructor |
|
44
|
|
|
* |
|
45
|
|
|
* @param ServiceResolver $serviceResolver Finds service classes |
|
46
|
|
|
* @param ProxyFactory[] $proxyFactories |
|
47
|
|
|
*/ |
|
48
|
19 |
|
public function __construct(ServiceResolver $serviceResolver, array $proxyFactories) |
|
49
|
|
|
{ |
|
50
|
19 |
|
$this->serviceResolver = $serviceResolver; |
|
51
|
19 |
|
$this->proxyFactories = $proxyFactories; |
|
52
|
19 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Create a new builder |
|
56
|
|
|
* |
|
57
|
|
|
* @return RetrofitBuilder |
|
58
|
|
|
*/ |
|
59
|
22 |
|
public static function builder(): RetrofitBuilder |
|
60
|
|
|
{ |
|
61
|
22 |
|
return new RetrofitBuilder(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Register an array of classes |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $services |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function registerServices(array $services): void |
|
71
|
|
|
{ |
|
72
|
1 |
|
foreach ($services as $service) { |
|
73
|
1 |
|
$this->registerService($service); |
|
74
|
|
|
} |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Register a single class |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $service |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function registerService(string $service): void |
|
84
|
|
|
{ |
|
85
|
1 |
|
$this->services[] = $service; |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Use the service resolver to find all the services dynamically |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $srcDir |
|
92
|
|
|
* @return int Number of services cached |
|
93
|
|
|
* @throws \RuntimeException |
|
94
|
|
|
* @throws \BadMethodCallException |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function createAll(string $srcDir): int |
|
97
|
|
|
{ |
|
98
|
1 |
|
$this->services = $this->serviceResolver->findServices($srcDir); |
|
99
|
|
|
|
|
100
|
1 |
|
return $this->createServices(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Creates cache files based on registered services |
|
105
|
|
|
* |
|
106
|
|
|
* @return int Number of services cached |
|
107
|
|
|
* @throws \RuntimeException |
|
108
|
|
|
* @throws \BadMethodCallException |
|
109
|
|
|
*/ |
|
110
|
2 |
|
public function createServices(): int |
|
111
|
|
|
{ |
|
112
|
2 |
|
foreach ($this->services as $service) { |
|
113
|
2 |
|
$this->create($service); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
return \count($this->services); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Create a new service proxy given an interface name |
|
121
|
|
|
* |
|
122
|
|
|
* The returned proxy object should be used as if it's an |
|
123
|
|
|
* instance of the service provided. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $service |
|
126
|
|
|
* @return Proxy |
|
127
|
|
|
*/ |
|
128
|
19 |
|
public function create(string $service): Proxy |
|
129
|
|
|
{ |
|
130
|
19 |
|
foreach ($this->proxyFactories as $proxyFactory) { |
|
131
|
18 |
|
$object = $proxyFactory->create($service); |
|
132
|
18 |
|
if ($object !== null) { |
|
133
|
18 |
|
return $object; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
|
138
|
1 |
|
throw new LogicException(sprintf('Retrofit: Could not find a proxy factory for %s', $service)); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|