1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shamaseen\Repository\Generator\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RepositoryGenerator |
10
|
|
|
* @package Shamaseen\Repository\Generator\Commands |
11
|
|
|
*/ |
12
|
|
|
class Generator extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The name and signature of the console command. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'make:repository |
20
|
|
|
{name : Class (singular) for example User}'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Create repository generator'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The repository name. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $repoName; |
35
|
|
|
/** |
36
|
|
|
* The repository name space. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $repoNamespace; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new command instance. |
44
|
|
|
* |
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
parent::__construct(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Execute the console command. |
53
|
|
|
* |
54
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
public function handle() |
57
|
|
|
{ |
58
|
|
|
$file = preg_split( " (/|\\\\) ", (string)$this->argument('name')); |
59
|
|
|
|
60
|
|
|
$this->repoName = $file[count($file) - 1]; |
|
|
|
|
61
|
|
|
unset($file[count($file) - 1]); |
62
|
|
|
$path = implode("/", $file); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$model= str_plural(\Config::get('repository.model')); |
65
|
|
|
$interface= str_plural(\Config::get('repository.interface')); |
66
|
|
|
$repository= str_plural(\Config::get('repository.repository')); |
67
|
|
|
|
68
|
|
|
$this->generate($path, 'Http\Controllers', 'Controller'); |
69
|
|
|
$this->generate($path, $model, 'Entity'); |
70
|
|
|
$this->generate($path, 'Http\Requests', 'Request'); |
71
|
|
|
$this->generate($path, $interface, 'Interface'); |
72
|
|
|
$this->generate($path, $repository, 'Repository'); |
73
|
|
|
|
74
|
|
|
File::append(\Config::get('repository.route_path') . '/web.php', "\n" . 'Route::resource(\'' . strtolower(str_plural($this->repoName)) . "', '{$path}\{$this->repoName}Controller');"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get stub content to generate needed files |
79
|
|
|
* |
80
|
|
|
* @param string $type determine which stub should choose to get content |
81
|
|
|
* @return false|string |
82
|
|
|
*/ |
83
|
|
|
protected function getStub($type) |
84
|
|
|
{ |
85
|
|
|
return file_get_contents(\Config::get('repository.resources_path') . "/$type.stub"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $path Class path |
90
|
|
|
* @param string $folder default path to generate in |
91
|
|
|
* @param string $type define which kind of files should generate |
92
|
|
|
*/ |
93
|
|
|
protected function generate($path, $folder, $type) |
94
|
|
|
{ |
95
|
|
|
$template = str_replace( |
96
|
|
|
[ |
97
|
|
|
'{{modelName}}', |
98
|
|
|
"{{folder}}", |
99
|
|
|
"{{path}}", |
100
|
|
|
"{{modelBaseFolderName}}", |
101
|
|
|
"{{interfaceBaseFolderName}}", |
102
|
|
|
], |
103
|
|
|
[ |
104
|
|
|
$this->repoName, |
105
|
|
|
str_plural($folder), |
106
|
|
|
$path, |
107
|
|
|
str_plural(\Config::get('repository.model','Entity')), |
108
|
|
|
str_plural(\Config::get('repository.interface','Interface')), |
109
|
|
|
|
110
|
|
|
], |
111
|
|
|
$this->getStub($type) |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$folder = str_replace('\\','/',$folder); |
115
|
|
|
$filePath = $this->getFolderOrCreate(\Config::get('repository.app_path') . "/{$folder}/{$path}/"); |
116
|
|
|
|
117
|
|
|
if($type == 'Entity') |
118
|
|
|
{ |
119
|
|
|
file_put_contents($filePath . "{$this->repoName}.php", $template); |
120
|
|
|
}else |
121
|
|
|
{ |
122
|
|
|
file_put_contents($filePath . "{$this->repoName}{$type}.php", $template); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Check if folder exist |
129
|
|
|
* @param string $path class path |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getFolderOrCreate($path) |
133
|
|
|
{ |
134
|
|
|
if (!file_exists($path)) { |
135
|
|
|
mkdir($path, 0777, true); |
136
|
|
|
} |
137
|
|
|
return $path; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|