1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MichaelRubel\AutoBinder; |
6
|
|
|
|
7
|
|
|
use MichaelRubel\AutoBinder\Traits\AutoBindsToContainer; |
8
|
|
|
|
9
|
|
|
class AutoBinder |
10
|
|
|
{ |
11
|
|
|
use AutoBindsToContainer; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base class namespace. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public string $classNamespace = 'App'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Target class folder. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public string $classFolder = 'Services'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Interface namespace (can be fully qualified). |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public string $interfaceNamespace = 'Interfaces'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Postfix convention for interfaces. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public string $interfacePostfix = 'Interface'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Base class folder. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public string $basePath = 'app'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The type of bindings. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public string $bindingType = 'singleton'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Assign a new class folder. |
57
|
|
|
* |
58
|
|
|
* @param string $classFolder |
59
|
|
|
*/ |
60
|
2 |
|
final public function __construct(string $classFolder) |
61
|
|
|
{ |
62
|
2 |
|
$this->classFolder = $classFolder; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create the object with target folder assigned. |
67
|
|
|
* |
68
|
|
|
* @param string $folder |
69
|
|
|
* |
70
|
|
|
* @return static |
71
|
|
|
*/ |
72
|
2 |
|
public static function from(string $folder): static |
73
|
|
|
{ |
74
|
2 |
|
return new static($folder); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the class base path. |
79
|
|
|
* |
80
|
|
|
* @param string $basePath |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
2 |
|
public function basePath(string $basePath): static |
85
|
|
|
{ |
86
|
2 |
|
$this->basePath = $basePath; |
87
|
|
|
|
88
|
2 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Define the class namespace. |
93
|
|
|
* |
94
|
|
|
* @param string $path |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
2 |
|
public function classNamespace(string $path): static |
99
|
|
|
{ |
100
|
2 |
|
$this->classNamespace = $this->namespaceFrom($path); |
101
|
|
|
|
102
|
2 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Define the interface namespace. |
107
|
|
|
* |
108
|
|
|
* @param string $path |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
1 |
|
public function interfaceNamespace(string $path): static |
113
|
|
|
{ |
114
|
1 |
|
$this->interfaceNamespace = $this->namespaceFrom($path); |
115
|
|
|
|
116
|
1 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Bind the result as a specific type of binding. |
121
|
|
|
* |
122
|
|
|
* @param string $bindingType |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
2 |
|
public function as(string $bindingType): static |
127
|
|
|
{ |
128
|
2 |
|
$this->bindingType = $bindingType; |
129
|
|
|
|
130
|
2 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Perform the bindings. |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
2 |
|
public function bind(): void |
139
|
|
|
{ |
140
|
2 |
|
$this->run(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|