1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
use MichaelRubel\EnhancedContainer\Call; |
6
|
|
|
use MichaelRubel\EnhancedContainer\Core\BindingBuilder; |
7
|
|
|
|
8
|
|
|
if (! function_exists('call')) { |
9
|
|
|
/** |
10
|
|
|
* @param string|object $class |
11
|
|
|
* @param array $parameters |
12
|
|
|
* |
13
|
|
|
* @return mixed |
14
|
|
|
*/ |
15
|
|
|
function call(string|object $class, array $parameters = []): mixed |
16
|
|
|
{ |
17
|
|
|
return app(Call::class, [$class, $parameters]); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if (! function_exists('bind')) { |
22
|
|
|
/** |
23
|
|
|
* @param string|object $abstract |
24
|
|
|
* |
25
|
|
|
* @return BindingBuilder |
26
|
|
|
*/ |
27
|
|
|
function bind(string|object $abstract): BindingBuilder |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
return new BindingBuilder($abstract); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (! function_exists('singleton')) { |
34
|
|
|
/** |
35
|
|
|
* @param string $abstract |
36
|
|
|
* @param Closure|string|null $concrete |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
function singleton(string $abstract, \Closure|string $concrete = null): void |
41
|
|
|
{ |
42
|
|
|
app()->singleton($abstract, $concrete); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (! function_exists('scoped')) { |
47
|
|
|
/** |
48
|
|
|
* @param string $abstract |
49
|
|
|
* @param Closure|string|null $concrete |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
function scoped(string $abstract, \Closure|string $concrete = null): void |
54
|
|
|
{ |
55
|
|
|
app()->scoped($abstract, $concrete); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (! function_exists('extend')) { |
60
|
|
|
/** |
61
|
|
|
* @param string $abstract |
62
|
|
|
* @param Closure $closure |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
function extend(string $abstract, \Closure $closure): void |
67
|
|
|
{ |
68
|
|
|
app()->extend($abstract, $closure); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (! function_exists('enableMethodForwarding')) { |
73
|
|
|
/** |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
function enableMethodForwarding(): void |
77
|
|
|
{ |
78
|
|
|
config(['enhanced-container.forwarding_enabled' => true]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (! function_exists('disableMethodForwarding')) { |
83
|
|
|
/** |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
function disableMethodForwarding(): void |
87
|
|
|
{ |
88
|
|
|
config(['enhanced-container.forwarding_enabled' => false]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (! function_exists('runWithoutForwarding')) { |
93
|
|
|
/** |
94
|
|
|
* @param Closure $closure |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
function runWithoutForwarding(\Closure $closure): mixed |
99
|
|
|
{ |
100
|
|
|
disableMethodForwarding(); |
101
|
|
|
|
102
|
|
|
$callback = $closure(); |
103
|
|
|
|
104
|
|
|
enableMethodForwarding(); |
105
|
|
|
|
106
|
|
|
return $callback; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (! function_exists('runWithForwarding')) { |
111
|
|
|
/** |
112
|
|
|
* @param Closure $closure |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
function runWithForwarding(\Closure $closure): mixed |
117
|
|
|
{ |
118
|
|
|
enableMethodForwarding(); |
119
|
|
|
|
120
|
|
|
$callback = $closure(); |
121
|
|
|
|
122
|
|
|
disableMethodForwarding(); |
123
|
|
|
|
124
|
|
|
return $callback; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.