|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MichaelRubel\AutoBinder\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
10
|
|
|
|
|
11
|
|
|
trait AutoBinds |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private string $namespace; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return void |
|
20
|
|
|
*/ |
|
21
|
2 |
|
private function prepareNamespace(): void |
|
22
|
|
|
{ |
|
23
|
2 |
|
$this->namespace = Str::ucfirst( |
|
24
|
2 |
|
strtr(config('auto-binder.start_namespace', self::DEFAULT_NAMESPACE), '/', '\\') |
|
|
|
|
|
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $folder |
|
30
|
|
|
* |
|
31
|
|
|
* @return Collection |
|
32
|
|
|
*/ |
|
33
|
2 |
|
private function getFolderFiles(string $folder): Collection |
|
34
|
|
|
{ |
|
35
|
2 |
|
$path = base_path(config('auto-binder.start_folder', self::DEFAULT_FOLDER)) |
|
|
|
|
|
|
36
|
|
|
. DIRECTORY_SEPARATOR |
|
37
|
|
|
. $folder; |
|
38
|
|
|
|
|
39
|
2 |
|
$filesystem = app('files'); |
|
40
|
|
|
|
|
41
|
2 |
|
return collect($filesystem->isDirectory($path) ? $filesystem->allFiles($path) : []) |
|
|
|
|
|
|
42
|
2 |
|
->reject(fn (SplFileInfo $file) => collect(config('auto-binder.exclude_from_scan', self::DEFAULT_SCAN_EXCLUDES)) |
|
|
|
|
|
|
43
|
2 |
|
->map(fn (string $folder) => str_contains($file->getRelativePath(), $folder)) |
|
44
|
2 |
|
->contains(true)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $filename |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
2 |
|
private function cleanupFilename(string $filename): string |
|
53
|
|
|
{ |
|
54
|
2 |
|
return strtr( |
|
55
|
2 |
|
substr( |
|
56
|
|
|
$filename, |
|
57
|
|
|
0, |
|
58
|
2 |
|
(int) strrpos($filename, '.') |
|
59
|
|
|
), |
|
60
|
|
|
'/', |
|
61
|
|
|
'\\' |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $folder |
|
67
|
|
|
* @param string $filenameWithoutExtension |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
2 |
|
private function getInterface(string $folder, string $filenameWithoutExtension): string |
|
72
|
|
|
{ |
|
73
|
2 |
|
return $this->namespace |
|
74
|
|
|
. self::CLASS_SEPARATOR |
|
|
|
|
|
|
75
|
|
|
. $folder |
|
76
|
|
|
. self::CLASS_SEPARATOR |
|
77
|
2 |
|
. (config('auto-binder.interface_folder', self::DEFAULT_INTERFACE_FOLDER)) |
|
|
|
|
|
|
78
|
|
|
. self::CLASS_SEPARATOR |
|
79
|
|
|
. $filenameWithoutExtension |
|
80
|
2 |
|
. (config('auto-binder.interface_postfix', self::DEFAULT_INTERFACE_POSTFIX)); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $folder |
|
85
|
|
|
* @param string $filenameWithRelativePath |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
2 |
|
private function getImplementation(string $folder, string $filenameWithRelativePath): string |
|
90
|
|
|
{ |
|
91
|
2 |
|
return $this->namespace |
|
92
|
|
|
. self::CLASS_SEPARATOR |
|
|
|
|
|
|
93
|
|
|
. $folder |
|
94
|
|
|
. self::CLASS_SEPARATOR |
|
95
|
|
|
. $filenameWithRelativePath; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $interface |
|
100
|
|
|
* @param string $implementation |
|
101
|
|
|
* |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
2 |
|
private function bind(string $interface, string $implementation): void |
|
105
|
|
|
{ |
|
106
|
2 |
|
app()->{config('auto-binder.binding_type', self::DEFAULT_BINDING_TYPE)}($interface, $implementation); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|