Skip to content

Commit f8a1bcb

Browse files
committedFeb 24, 2023
Edit command, Add config file
1 parent 0283454 commit f8a1bcb

4 files changed

+57
-4
lines changed
 

‎README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Laravel API Resource Generator Package
2+
3+
This package will help you to generate API resources for your Laravel project.
4+
5+
## Installation
6+
7+
You can install the package via composer:
8+
9+
```bash
10+
composer require alibori/laravel-api-resource-generator
11+
```
12+
13+
## Usage
14+
15+
``` bash
16+
php artisan alibori:api-resource <model-name>
17+
```
18+
19+
## Publish config file
20+
21+
``` bash
22+
php artisan vendor:publish --provider="Alibori\LaravelApiResourceGenerator\LaravelApiResourceGeneratorServiceProvider" --tag="config"
23+
```

‎config/config.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is the "config" file of the Laravel Api Resource Generator package.
5+
*/
6+
7+
return [
8+
'resources' => [
9+
'dir' => 'app/Http/Resources',
10+
'namespace' => 'App\\Http\\Resources',
11+
],
12+
'models' => [
13+
'dir' => 'app/Models',
14+
'namespace' => 'App\\Models',
15+
],
16+
];

‎src/Console/GenerateApiResourceCommand.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Illuminate\Database\Eloquent\Model;
1212
use Illuminate\Filesystem\Filesystem;
1313
use Illuminate\Support\Str;
14-
use ReflectionClass;
1514
use Symfony\Component\Console\Input\InputArgument;
1615

1716
class GenerateApiResourceCommand extends Command
@@ -66,7 +65,7 @@ public function __construct(Filesystem $files)
6665
public function handle(): void
6766
{
6867
$this->dir = $this->defaultResourcesDir();
69-
$this->namespace = 'App\Http\Resources';
68+
$this->namespace = config('laravelapiresourcegeneratorpackage.resources.namespace');
7069

7170
$model = $this->loadModel($this->argument('model'));
7271

@@ -84,7 +83,7 @@ protected function getArguments(): array
8483

8584
protected function defaultResourcesDir(): string
8685
{
87-
return 'app/Http/Resources';
86+
return config('laravelapiresourcegeneratorpackage.resources.dir');
8887
}
8988

9089
/**
@@ -93,7 +92,7 @@ protected function defaultResourcesDir(): string
9392
protected function loadModel(string $model): Model
9493
{
9594

96-
return $this->laravel->make('App\\Models\\' . $model);
95+
return $this->laravel->make(config('laravelapiresourcegeneratorpackage.models.namespace') . '\\' . $model);
9796
}
9897

9998
/**

‎src/LaravelApiResourceGeneratorServiceProvider.php

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010
class LaravelApiResourceGeneratorServiceProvider extends ServiceProvider
1111
{
12+
/**
13+
* Register the application services.
14+
*
15+
* @return void
16+
*/
17+
public function register(): void
18+
{
19+
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravelapiresourcegeneratorpackage');
20+
}
21+
1222
/**
1323
* Bootstrap the application services.
1424
*
@@ -21,5 +31,10 @@ public function boot(): void
2131
}
2232

2333
$this->commands([GenerateApiResourceCommand::class]);
34+
35+
// Publish config file
36+
$this->publishes([
37+
__DIR__.'/../config/config.php' => config_path('laravelapiresourcegeneratorpackage.php'),
38+
], 'config');
2439
}
2540
}

0 commit comments

Comments
 (0)