michael-rubel /
laravel-formatters
| 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 | 84 | public function configurePackage(Package $package): void |
|
| 24 | { |
||
| 25 | 84 | $package |
|
| 26 | 84 | ->name('laravel-formatters') |
|
| 27 | 84 | ->hasConfigFile() |
|
| 28 | 84 | ->hasCommand(MakeFormatterCommand::class); |
|
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Register any package services. |
||
| 33 | * |
||
| 34 | * @return void |
||
| 35 | */ |
||
| 36 | 84 | public function packageRegistered(): void |
|
| 37 | { |
||
| 38 | /** @var string $app_folder */ |
||
| 39 | 84 | $app_folder = config('formatters.folder'); |
|
| 40 | |||
| 41 | 84 | if (empty($app_folder)) { |
|
| 42 | 1 | return; |
|
| 43 | } |
||
| 44 | |||
| 45 | /** @var string $bindings_case */ |
||
| 46 | 84 | $bindings_case = config('formatters.bindings_case', 'kebab'); |
|
| 47 | |||
| 48 | 84 | $filesystem = app('files'); |
|
| 49 | |||
| 50 | 84 | $appFormatters = $filesystem->isDirectory(base_path($app_folder)) |
|
| 51 | 82 | ? collect($filesystem->allFiles(base_path($app_folder))) |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 52 | 7 | : new Collection(); |
|
| 53 | |||
| 54 | 84 | $packageFormatters = collect( |
|
| 55 | 84 | $filesystem->allFiles($this->getPackageDirectory()) |
|
| 56 | 84 | ); |
|
| 57 | |||
| 58 | 84 | $packageFormatters |
|
| 59 | 84 | ->merge($appFormatters) |
|
| 60 | 84 | ->each(function ($file) use ($app_folder, $bindings_case) { |
|
| 61 | 84 | $filename = $file->getFilenameWithoutExtension(); |
|
| 62 | 84 | $name = $this->getFormatterName($bindings_case, $filename); |
|
| 63 | 84 | $class = $this->getFormatterClass($file, $filename, $app_folder); |
|
| 64 | |||
| 65 | 84 | $this->app->bind($name, $class); |
|
| 66 | 84 | }); |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get the package directory path. |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | 84 | private function getPackageDirectory(): string |
|
| 75 | { |
||
| 76 | 84 | 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 | 84 | private function getFormatterName(string $bindings_case, string $filename): string |
|
| 88 | { |
||
| 89 | 84 | $name = str_replace('Formatter', '', $filename); |
|
| 90 | |||
| 91 | 84 | 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 | 84 | private function getFormatterClass(SplFileInfo $file, string $filename, string $app_folder): string |
|
| 104 | { |
||
| 105 | 84 | $path = str_contains($file->getPathName(), $app_folder) |
|
| 106 | 33 | ? Str::ucfirst(str_replace(DIRECTORY_SEPARATOR, FormatterService::CLASS_SEPARATOR, $app_folder)) |
|
| 107 | 33 | . FormatterService::CLASS_SEPARATOR |
|
| 108 | 84 | : (new \ReflectionClass(static::class))->getNamespaceName() |
|
| 109 | 84 | . FormatterService::CLASS_SEPARATOR |
|
| 110 | 84 | . FormatterService::PACKAGE_FOLDER |
|
| 111 | 84 | . FormatterService::CLASS_SEPARATOR; |
|
| 112 | |||
| 113 | 84 | return sprintf('%s%s', $path, $filename); |
|
| 114 | } |
||
| 115 | } |
||
| 116 |