1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bhavingajjar\LaravelApiGenerator\Commands; |
4
|
|
|
|
5
|
|
|
use Bhavingajjar\LaravelApiGenerator\LaravelApiGenerator; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
|
8
|
|
|
class GenerateApi extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'api:generate {--model=}'; |
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'REST Api Generator With API Resources'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a new command instance. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
parent::__construct(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Execute the console command. |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function handle() |
37
|
|
|
{ |
38
|
|
|
if (empty($this->option('model'))) { |
39
|
|
|
$this->error('Model Name Argument not found!'); |
40
|
|
|
|
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (! file_exists(base_path(config('laravel-api-generator.model_directory_path').'/'.$this->option('model').'.php'))) { |
45
|
|
|
$this->error('Model does not exist!'); |
46
|
|
|
|
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$api = new LaravelApiGenerator($this->option('model')); |
51
|
|
|
$controller = $api->generateController(); |
52
|
|
|
if ($controller) { |
53
|
|
|
$this->info('Controller Generated SuccessFully!'); |
54
|
|
|
} else { |
55
|
|
|
$this->error('Controller Already Exists!'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$resource = $api->generateResource(); |
59
|
|
|
if ($resource) { |
60
|
|
|
$this->info('Resource Generated SuccessFully!'); |
61
|
|
|
} else { |
62
|
|
|
$this->error('Resource Already Exists!'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$collection = $api->generateCollection(); |
66
|
|
|
if ($collection) { |
67
|
|
|
$this->info('Collection Generated SuccessFully!'); |
68
|
|
|
} else { |
69
|
|
|
$this->error('Collection Already Exists!'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$route = $api->generateRoute(); |
73
|
|
|
if ($route) { |
74
|
|
|
$this->info('Route Generated SuccessFully!'); |
75
|
|
|
} else { |
76
|
|
|
$this->error('Route Already Exists!'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->info('Api Created SuccessFully!'); |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|