1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Console\Domain\AllAppModules; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\ClassResolver\Config\ConfigResolver; |
8
|
|
|
use Gacela\Framework\ClassResolver\Factory\FactoryResolver; |
9
|
|
|
use Gacela\Framework\ClassResolver\Provider\ProviderNotFoundException; |
10
|
|
|
use Gacela\Framework\ClassResolver\Provider\ProviderResolver; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
|
13
|
|
|
use function strlen; |
14
|
|
|
|
15
|
|
|
final class AppModuleCreator |
16
|
|
|
{ |
17
|
|
|
public function __construct( |
18
|
|
|
private readonly FactoryResolver $factoryResolver, |
19
|
|
|
private readonly ConfigResolver $configResolver, |
20
|
|
|
private readonly ProviderResolver $providerResolver, |
21
|
|
|
) { |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param class-string $facadeClass |
26
|
|
|
*/ |
27
|
|
|
public function fromClass(string $facadeClass): AppModule |
28
|
|
|
{ |
29
|
|
|
return new AppModule( |
30
|
|
|
$this->fullModuleName($facadeClass), |
31
|
|
|
$this->moduleName($facadeClass), |
32
|
|
|
$facadeClass, |
33
|
|
|
$this->findFactory($facadeClass), |
|
|
|
|
34
|
|
|
$this->findConfig($facadeClass), |
|
|
|
|
35
|
|
|
$this->findProvider($facadeClass), |
|
|
|
|
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param class-string $facadeClass |
41
|
|
|
*/ |
42
|
|
|
private function fullModuleName(string $facadeClass): string |
43
|
|
|
{ |
44
|
|
|
$moduleNameIndex = strrpos($facadeClass, '\\') ?: strlen($facadeClass); |
45
|
|
|
|
46
|
|
|
return substr($facadeClass, 0, $moduleNameIndex); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param class-string $facadeClass |
51
|
|
|
*/ |
52
|
|
|
private function moduleName(string $facadeClass): string |
53
|
|
|
{ |
54
|
|
|
$fullModuleName = $this->fullModuleName($facadeClass); |
55
|
|
|
|
56
|
|
|
$moduleName = strrchr($fullModuleName, '\\') ?: $fullModuleName; |
57
|
|
|
|
58
|
|
|
return ltrim($moduleName, '\\'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param class-string $facadeClass |
63
|
|
|
*/ |
64
|
|
|
private function findFactory(string $facadeClass): ?string |
65
|
|
|
{ |
66
|
|
|
$resolver = $this->factoryResolver->resolve($facadeClass); |
67
|
|
|
|
68
|
|
|
if ((new ReflectionClass($resolver))->isAnonymous()) { |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $resolver::class; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param class-string $facadeClass |
77
|
|
|
*/ |
78
|
|
|
private function findConfig(string $facadeClass): ?string |
79
|
|
|
{ |
80
|
|
|
$resolver = $this->configResolver->resolve($facadeClass); |
81
|
|
|
|
82
|
|
|
if ((new ReflectionClass($resolver))->isAnonymous()) { |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $resolver::class; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param class-string $facadeClass |
91
|
|
|
*/ |
92
|
|
|
private function findProvider(string $facadeClass): ?string |
93
|
|
|
{ |
94
|
|
|
try { |
95
|
|
|
$resolver = $this->providerResolver->resolve($facadeClass); |
96
|
|
|
|
97
|
|
|
if ((new ReflectionClass($resolver))->isAnonymous()) { |
98
|
|
|
throw new ProviderNotFoundException($resolver); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $resolver::class; |
102
|
|
|
} catch (ProviderNotFoundException) { |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.