|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MichaelRubel\Formatters; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use MichaelRubel\Formatters\Commands\MakeFormatterCommand; |
|
10
|
|
|
use Spatie\LaravelPackageTools\Package; |
|
11
|
|
|
use Spatie\LaravelPackageTools\PackageServiceProvider; |
|
12
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
13
|
|
|
|
|
14
|
|
|
class FormatterServiceProvider extends PackageServiceProvider |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Configure the package. |
|
18
|
|
|
* |
|
19
|
|
|
* @param Package $package |
|
20
|
|
|
* |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
81 |
|
public function configurePackage(Package $package): void |
|
24
|
|
|
{ |
|
25
|
|
|
$package |
|
26
|
81 |
|
->name('laravel-formatters') |
|
27
|
81 |
|
->hasConfigFile() |
|
28
|
81 |
|
->hasCommand(MakeFormatterCommand::class); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Register any package services. |
|
33
|
|
|
* |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
81 |
|
public function packageRegistered(): void |
|
37
|
|
|
{ |
|
38
|
|
|
/** @var string $app_folder */ |
|
39
|
81 |
|
$app_folder = config('formatters.folder'); |
|
40
|
|
|
|
|
41
|
81 |
|
if (empty($app_folder)) { |
|
42
|
1 |
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** @var string $bindings_case */ |
|
46
|
81 |
|
$bindings_case = config('formatters.bindings_case', 'kebab'); |
|
47
|
|
|
|
|
48
|
81 |
|
$filesystem = app('files'); |
|
49
|
|
|
|
|
50
|
81 |
|
$appFormatters = $filesystem->isDirectory(base_path($app_folder)) |
|
51
|
68 |
|
? collect($filesystem->allFiles(base_path($app_folder))) |
|
|
|
|
|
|
52
|
18 |
|
: new Collection; |
|
53
|
|
|
|
|
54
|
81 |
|
$packageFormatters = collect( |
|
55
|
81 |
|
$filesystem->allFiles($this->getPackageDirectory()) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$packageFormatters |
|
59
|
81 |
|
->merge($appFormatters) |
|
60
|
81 |
|
->each(function ($file) use ($app_folder, $bindings_case) { |
|
61
|
81 |
|
$filename = $file->getFilenameWithoutExtension(); |
|
62
|
81 |
|
$name = $this->getFormatterName($bindings_case, $filename); |
|
63
|
81 |
|
$class = $this->getFormatterClass($file, $filename, $app_folder); |
|
64
|
|
|
|
|
65
|
81 |
|
$this->app->bind($name, $class); |
|
66
|
|
|
}); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the package directory path. |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
81 |
|
private function getPackageDirectory(): string |
|
75
|
|
|
{ |
|
76
|
81 |
|
return $this->getPackageBaseDir() . DIRECTORY_SEPARATOR . FormatterService::PACKAGE_FOLDER; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns formatter name for string binding. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $bindings_case |
|
83
|
|
|
* @param string $filename |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
81 |
|
private function getFormatterName(string $bindings_case, string $filename): string |
|
88
|
|
|
{ |
|
89
|
81 |
|
$name = str_replace('Formatter', '', $filename); |
|
90
|
|
|
|
|
91
|
81 |
|
return Str::{$bindings_case}($name . FormatterService::BINDING_POSTFIX); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Determines the formatter class namespace. |
|
96
|
|
|
* |
|
97
|
|
|
* @param SplFileInfo $file |
|
98
|
|
|
* @param string $filename |
|
99
|
|
|
* @param string $app_folder |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
81 |
|
private function getFormatterClass(SplFileInfo $file, string $filename, string $app_folder): string |
|
104
|
|
|
{ |
|
105
|
81 |
|
$path = str_contains($file->getPathName(), $app_folder) |
|
106
|
66 |
|
? Str::ucfirst(str_replace(DIRECTORY_SEPARATOR, FormatterService::CLASS_SEPARATOR, $app_folder)) |
|
107
|
|
|
. FormatterService::CLASS_SEPARATOR |
|
108
|
81 |
|
: (new \ReflectionClass(static::class))->getNamespaceName() |
|
109
|
|
|
. FormatterService::CLASS_SEPARATOR |
|
110
|
|
|
. FormatterService::PACKAGE_FOLDER |
|
111
|
|
|
. FormatterService::CLASS_SEPARATOR; |
|
112
|
|
|
|
|
113
|
81 |
|
return sprintf('%s%s', $path, $filename); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|