|
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 AutoBindsToContainer |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Run the folders scanning & bind the results. |
|
15
|
|
|
* |
|
16
|
|
|
* @return void |
|
17
|
|
|
*/ |
|
18
|
6 |
|
protected function run(): void |
|
19
|
|
|
{ |
|
20
|
6 |
|
collect($this->getFolderFiles()) |
|
|
|
|
|
|
21
|
6 |
|
->each(function (SplFileInfo $file) { |
|
22
|
6 |
|
$relativePath = $file->getRelativePathname(); |
|
23
|
6 |
|
$filenameWithoutExtension = $file->getFilenameWithoutExtension(); |
|
24
|
6 |
|
$filenameWithRelativePath = $this->prepareFilename($relativePath); |
|
25
|
|
|
|
|
26
|
6 |
|
$interface = $this->interfaceFrom($filenameWithoutExtension); |
|
27
|
6 |
|
$concrete = $this->concreteFrom($filenameWithRelativePath); |
|
28
|
|
|
|
|
29
|
6 |
|
app()->{$this->bindingType}($interface, $concrete); |
|
30
|
|
|
}); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get the folder files. |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
6 |
|
protected function getFolderFiles(): array |
|
39
|
|
|
{ |
|
40
|
6 |
|
$filesystem = app('files'); |
|
41
|
|
|
|
|
42
|
6 |
|
$folder = base_path($this->basePath . DIRECTORY_SEPARATOR . $this->classFolder); |
|
43
|
|
|
|
|
44
|
6 |
|
return $filesystem->isDirectory($folder) |
|
45
|
6 |
|
? $filesystem->allFiles($folder) |
|
46
|
6 |
|
: []; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Prepare the filename. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $filename |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
6 |
|
protected function prepareFilename(string $filename): string |
|
57
|
|
|
{ |
|
58
|
6 |
|
return (string) Str::of($filename) |
|
59
|
6 |
|
->replace('/', '\\') |
|
60
|
6 |
|
->substr(0, (int) strrpos($filename, '.')); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the namespace from a given path. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $path |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
6 |
|
protected function namespaceFrom(string $path): string |
|
71
|
|
|
{ |
|
72
|
6 |
|
return (string) Str::of($path) |
|
73
|
6 |
|
->replace('/', '\\') |
|
74
|
6 |
|
->ucfirst(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the interface from filename. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $filenameWithoutExtension |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
6 |
|
protected function interfaceFrom(string $filenameWithoutExtension): string |
|
85
|
|
|
{ |
|
86
|
6 |
|
$interface = $this->guessInterfaceWith($filenameWithoutExtension); |
|
87
|
|
|
|
|
88
|
6 |
|
if (is_null($interface)) { |
|
89
|
4 |
|
return $this->interfaceNamespace |
|
90
|
|
|
. '\\' |
|
91
|
|
|
. $filenameWithoutExtension |
|
92
|
4 |
|
. ($this->interfacePostfix); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
return $interface; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Guess the interface with a given filename. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $filenameWithoutExtension |
|
102
|
|
|
* |
|
103
|
|
|
* @return string|null |
|
104
|
|
|
*/ |
|
105
|
6 |
|
protected function guessInterfaceWith(string $filenameWithoutExtension): ?string |
|
106
|
|
|
{ |
|
107
|
6 |
|
if (! Str::contains($this->interfaceNamespace, '\\')) { |
|
108
|
2 |
|
return $this->classNamespace |
|
109
|
|
|
. '\\' |
|
110
|
2 |
|
. $this->classFolder |
|
111
|
|
|
. '\\' |
|
112
|
2 |
|
. $this->interfaceNamespace |
|
113
|
|
|
. '\\' |
|
114
|
|
|
. $filenameWithoutExtension |
|
115
|
2 |
|
. ($this->interfacePostfix); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
4 |
|
return null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get the concrete from filename. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $filenameWithRelativePath |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
6 |
|
protected function concreteFrom(string $filenameWithRelativePath): string |
|
129
|
|
|
{ |
|
130
|
6 |
|
return $this->classNamespace . '\\' . $this->classFolder . '\\' . $filenameWithRelativePath; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|