1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace MichaelRubel\AutoBinder; |
||
6 | |||
7 | use Closure; |
||
8 | use Illuminate\Support\Collection; |
||
9 | use Illuminate\Support\Str; |
||
10 | use InvalidArgumentException; |
||
11 | use MichaelRubel\AutoBinder\Contracts\ShouldCache; |
||
12 | use MichaelRubel\AutoBinder\Traits\BindsToContainer; |
||
13 | use MichaelRubel\AutoBinder\Traits\CachesBindings; |
||
14 | |||
15 | class AutoBinder implements ShouldCache |
||
16 | { |
||
17 | use BindsToContainer, CachesBindings; |
||
18 | |||
19 | /** |
||
20 | * Base class namespace. |
||
21 | */ |
||
22 | public string $classNamespace = 'App'; |
||
23 | |||
24 | /** |
||
25 | * Target class folder. |
||
26 | */ |
||
27 | public string $classFolder = 'Services'; |
||
28 | |||
29 | /** |
||
30 | * Interface namespace (can be fully qualified). |
||
31 | */ |
||
32 | public string $interfaceNamespace = 'Interfaces'; |
||
33 | |||
34 | /** |
||
35 | * Postfix convention for interfaces. |
||
36 | */ |
||
37 | public string $interfaceNaming = 'Interface'; |
||
38 | |||
39 | /** |
||
40 | * Base class folder. |
||
41 | */ |
||
42 | public string $basePath = 'app'; |
||
43 | |||
44 | /** |
||
45 | * The type of bindings. |
||
46 | */ |
||
47 | public string $bindingType = 'bind'; |
||
48 | |||
49 | /** |
||
50 | * When the class name is met, these dependencies are passed to the concrete. |
||
51 | */ |
||
52 | public array $dependencies = []; |
||
53 | |||
54 | /** |
||
55 | * Subdirectories to ignore when scanning. |
||
56 | */ |
||
57 | public array $excludesFolders = []; |
||
58 | |||
59 | /** |
||
60 | * Assign a new class folder. |
||
61 | */ |
||
62 | 26 | final public function __construct(?string $classFolder = null) |
|
63 | { |
||
64 | 26 | if ($classFolder) { |
|
65 | 24 | $this->classFolder = $classFolder; |
|
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Create the object with target folder assigned. |
||
71 | * |
||
72 | * @return static|Collection<int, static> |
||
73 | */ |
||
74 | 23 | public static function from(string|array $folder): static|Collection |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
75 | { |
||
76 | 23 | if (func_num_args() > 1) { |
|
77 | 6 | $folders = is_array($folder) ? $folder : func_get_args(); |
|
78 | |||
79 | 6 | return collect($folders)->map(fn ($folder) => new static($folder)); |
|
80 | } |
||
81 | |||
82 | 17 | $folder = is_string($folder) ? $folder : current($folder); |
|
83 | |||
84 | 17 | return new static($folder); |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Exclude specified subdirectory from scanning. |
||
89 | */ |
||
90 | 3 | public function exclude(string|array $folders): static |
|
91 | { |
||
92 | 3 | $folders = is_array($folders) ? $folders : func_get_args(); |
|
93 | |||
94 | 3 | collect($folders)->each( |
|
95 | 3 | fn ($folder) => $this->excludesFolders[] = $folder |
|
96 | 3 | ); |
|
97 | |||
98 | 3 | return $this; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Set the class base path. |
||
103 | */ |
||
104 | 22 | public function basePath(string $basePath): static |
|
105 | { |
||
106 | 22 | $this->basePath = $basePath; |
|
107 | |||
108 | 22 | return $this; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Define the class namespace. |
||
113 | */ |
||
114 | 21 | public function classNamespace(string $path): static |
|
115 | { |
||
116 | 21 | $this->classNamespace = $this->namespaceFrom($path); |
|
117 | |||
118 | 21 | return $this; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Define the interface namespace. |
||
123 | */ |
||
124 | 9 | public function interfaceNamespace(string $path): static |
|
125 | { |
||
126 | 9 | $this->interfaceNamespace = $this->namespaceFrom($path); |
|
127 | |||
128 | 9 | return $this; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Define the interface postfix. |
||
133 | */ |
||
134 | 1 | public function interfaceNaming(string $name): static |
|
135 | { |
||
136 | 1 | $this->interfaceNaming = Str::ucfirst($name); |
|
137 | |||
138 | 1 | if (! Str::contains($this->interfaceNamespace, '\\')) { |
|
139 | 1 | $this->interfaceNamespace(Str::plural($this->interfaceNaming)); |
|
140 | } |
||
141 | |||
142 | 1 | return $this; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Adds dependencies to the class when the class name is met. |
||
147 | */ |
||
148 | 3 | public function when(string $abstract, Closure $callback): static |
|
149 | { |
||
150 | 3 | $this->dependencies[$abstract] = $callback; |
|
151 | |||
152 | 3 | return $this; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Bind the result as a specific type of binding. |
||
157 | */ |
||
158 | 15 | public function as(string $type): static |
|
159 | { |
||
160 | 15 | if (! Str::is(['bind', 'scoped', 'singleton'], $type)) { |
|
161 | 2 | throw new InvalidArgumentException('Invalid binding type.'); |
|
162 | } |
||
163 | |||
164 | 13 | $this->bindingType = $type; |
|
165 | |||
166 | 13 | return $this; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Perform the scan & binding. |
||
171 | */ |
||
172 | 22 | public function bind(): void |
|
173 | { |
||
174 | 22 | if ($this->hasCache()) { |
|
175 | 2 | $this->usesCache = true; |
|
176 | |||
177 | 2 | $this->fromCache(); |
|
178 | |||
179 | 2 | return; |
|
180 | } |
||
181 | |||
182 | 22 | $this->scan(); |
|
183 | } |
||
184 | } |
||
185 |