|
7 | 7 | use Illuminate\Support\ServiceProvider; |
8 | 8 | use Laravel\Lumen\Application as LumenApplication; |
9 | 9 | use Spatie\Fractal\Console\Commands\TransformerMakeCommand; |
| 10 | +use Spatie\LaravelPackageTools\Package; |
| 11 | +use Spatie\LaravelPackageTools\PackageServiceProvider; |
10 | 12 |
|
11 | | -class FractalServiceProvider extends ServiceProvider |
| 13 | +class FractalServiceProvider extends PackageServiceProvider |
12 | 14 | { |
13 | | - /** |
14 | | - * Bootstrap the application services. |
15 | | - */ |
16 | | - public function boot() |
| 15 | + public function configurePackage(Package $package): void |
17 | 16 | { |
18 | | - $this->setupConfig(); |
19 | | - |
20 | | - if ($this->app->runningInConsole()) { |
21 | | - $this->commands([ |
22 | | - TransformerMakeCommand::class, |
23 | | - ]); |
24 | | - } |
| 17 | + $package |
| 18 | + ->name('laravel-fractal') |
| 19 | + ->hasConfigFile() |
| 20 | + ->hasCommand(TransformerMakeCommand::class); |
| 21 | + } |
25 | 22 |
|
26 | | - $this->setupMacro(); |
| 23 | + public function packageBooted() |
| 24 | + { |
| 25 | + Collection::macro('transformWith', function ($transformer) { |
| 26 | + return fractal($this, $transformer); |
| 27 | + }); |
27 | 28 | } |
28 | 29 |
|
29 | | - /** |
30 | | - * Register the application services. |
31 | | - */ |
32 | | - public function register() |
| 30 | + public function packageRegistered() |
33 | 31 | { |
34 | 32 | $this->app->singleton('fractal', function ($app, $arguments) { |
35 | 33 | return fractal(...$arguments); |
36 | 34 | }); |
37 | 35 |
|
38 | 36 | $this->app->alias('fractal', Fractal::class); |
39 | 37 | } |
40 | | - |
41 | | - protected function setupConfig() |
42 | | - { |
43 | | - $source = realpath(__DIR__.'/../config/fractal.php'); |
44 | | - |
45 | | - if ($this->app instanceof LaravelApplication) { |
46 | | - $this->publishes([$source => config_path('fractal.php')]); |
47 | | - } elseif ($this->app instanceof LumenApplication) { |
48 | | - $this->app->configure('fractal'); |
49 | | - } |
50 | | - |
51 | | - $this->mergeConfigFrom($source, 'fractal'); |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * Add a 'transformWith' macro to Laravel's collection. |
56 | | - */ |
57 | | - protected function setupMacro() |
58 | | - { |
59 | | - Collection::macro('transformWith', function ($transformer) { |
60 | | - return fractal($this, $transformer); |
61 | | - }); |
62 | | - } |
63 | 38 | } |
0 commit comments