|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MichaelRubel\EnhancedContainer\Core; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use MichaelRubel\EnhancedContainer\Traits\HelpsProxies; |
|
8
|
|
|
|
|
9
|
|
|
class MethodForwarder |
|
10
|
|
|
{ |
|
11
|
|
|
use HelpsProxies; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @const CLASS_SEPARATOR |
|
15
|
|
|
*/ |
|
16
|
|
|
public const CLASS_SEPARATOR = '\\'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param object|string $class |
|
20
|
|
|
* @param array $dependencies |
|
21
|
|
|
*/ |
|
22
|
58 |
|
public function __construct( |
|
23
|
|
|
public object | string $class, |
|
24
|
|
|
public array $dependencies |
|
25
|
|
|
) { |
|
26
|
58 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Forward the method. |
|
30
|
|
|
* |
|
31
|
|
|
* @return object|null |
|
32
|
|
|
*/ |
|
33
|
58 |
|
public function getClass(): ?object |
|
34
|
|
|
{ |
|
35
|
58 |
|
$forwardsTo = $this->forwardsTo(); |
|
36
|
|
|
|
|
37
|
58 |
|
return class_exists($forwardsTo) || interface_exists($forwardsTo) |
|
38
|
30 |
|
? $this->resolvePassedClass($forwardsTo, $this->dependencies) |
|
39
|
58 |
|
: null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Parse the class where to forward the call. |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
58 |
|
public function forwardsTo(): string |
|
48
|
|
|
{ |
|
49
|
|
|
/** @var string */ |
|
50
|
58 |
|
$naming_from = config('enhanced-container.from.naming') ?? 'pluralStudly'; |
|
51
|
|
|
|
|
52
|
|
|
/** @var string */ |
|
53
|
58 |
|
$naming_to = config('enhanced-container.to.naming') ?? 'pluralStudly'; |
|
54
|
|
|
|
|
55
|
|
|
/** @var string */ |
|
56
|
58 |
|
$layer_from = config('enhanced-container.from.layer') ?? 'Service'; |
|
57
|
|
|
|
|
58
|
|
|
/** @var string */ |
|
59
|
58 |
|
$layer_to = config('enhanced-container.to.layer') ?? 'Repository'; |
|
60
|
|
|
|
|
61
|
58 |
|
return collect( |
|
62
|
58 |
|
$this->convertToNamespace($this->class) |
|
63
|
58 |
|
)->pipe( |
|
64
|
58 |
|
fn (Collection $class): Collection => collect( |
|
65
|
58 |
|
explode(self::CLASS_SEPARATOR, $class->first()) |
|
66
|
|
|
) |
|
67
|
58 |
|
)->pipe( |
|
68
|
58 |
|
fn (Collection $delimited): Collection => $delimited->map( |
|
69
|
58 |
|
fn ($item) => str_replace( |
|
70
|
58 |
|
Str::{$naming_from}($layer_from), |
|
71
|
58 |
|
Str::{$naming_to}($layer_to), |
|
72
|
|
|
$item |
|
73
|
|
|
) |
|
74
|
58 |
|
) |
|
75
|
58 |
|
)->pipe( |
|
76
|
58 |
|
fn (Collection $structure): string => implode( |
|
77
|
58 |
|
self::CLASS_SEPARATOR, |
|
78
|
58 |
|
$structure->put( |
|
79
|
58 |
|
$structure->keys()->last(), |
|
80
|
58 |
|
str_replace( |
|
81
|
58 |
|
$layer_from, |
|
82
|
|
|
$layer_to, |
|
83
|
58 |
|
$structure->last() ?? '' |
|
84
|
|
|
) |
|
85
|
58 |
|
)->all() |
|
86
|
|
|
) |
|
87
|
58 |
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get the instance's property. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $name |
|
94
|
|
|
* |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __get(string $name): mixed |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->{$name}; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set the instance's property. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $name |
|
106
|
|
|
* @param mixed $value |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function __set(string $name, mixed $value): void |
|
111
|
|
|
{ |
|
112
|
|
|
$this->{$name} = $value; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|