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
|
|
|
* @return void |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
public function __construct() |
48
|
|
|
{ |
49
|
|
|
parent::__construct(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Execute the console command. |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function handle() |
58
|
|
|
{ |
59
|
|
|
|
60
|
|
|
$file = explode("/", (string)$this->argument('name')); |
61
|
|
|
|
62
|
|
|
$this->repoName=$file[count($file) - 1]; |
63
|
|
|
unset($file[count($file) - 1]); |
64
|
|
|
$path = implode("/", $file); |
65
|
|
|
|
66
|
|
|
if(count($file) == 0){ |
|
|
|
|
67
|
|
|
$this->repoNamespace=$this->repoName; |
68
|
|
|
}else{ |
|
|
|
|
69
|
|
|
$name = $file[count($file) - 1]; |
70
|
|
|
$this->repoNamespace=implode("\\", $file); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->generate($path, \Config::get('repository.controllers_path'), 'Controller'); |
75
|
|
|
$this->generate($path, \Config::get('repository.models_path'), 'Entity'); |
76
|
|
|
$this->generate($path, \Config::get('repository.requests_path'), 'Request'); |
77
|
|
|
$this->generate($path, \Config::get('repository.interfaces_path'), 'Interface'); |
78
|
|
|
$this->generate($path, \Config::get('repository.repositories_path'), 'Repository'); |
79
|
|
|
|
80
|
|
|
File::append(\Config::get('repository.route_path').'web.php', "\n" . 'Route::resource(\'' . str_plural($name) . "', '{$name}Controller');"); |
|
|
|
|
81
|
|
|
|
82
|
|
|
} |
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get stub content to generate needed files |
86
|
|
|
* |
87
|
|
|
* @param string $type determine which stub should choose to get content |
88
|
|
|
* @return false|string |
89
|
|
|
*/ |
90
|
|
|
protected function getStub($type) |
91
|
|
|
{ |
92
|
|
|
return file_get_contents(\Config::get('repository.resources_path')."/stubs/$type.stub"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $name Class name |
|
|
|
|
97
|
|
|
* @param string $path Class path |
98
|
|
|
* @param string $folder default path to generate in |
99
|
|
|
* @param string $type define which kind of files should generate |
100
|
|
|
*/ |
101
|
|
|
protected function generate($path, $folder, $type) |
102
|
|
|
{ |
103
|
|
|
|
104
|
|
|
$template = str_replace( |
105
|
|
|
[ |
106
|
|
|
'{{modelName}}', |
107
|
|
|
"{{modelNamePlural}}" |
108
|
|
|
], |
109
|
|
|
[ |
110
|
|
|
$this->repoName, |
111
|
|
|
$this->repoNamespace |
112
|
|
|
], |
113
|
|
|
$this->getStub($type) |
114
|
|
|
); |
115
|
|
|
$path = $this->checkFolder(\Config::get('repository.app_path').'/'.$folder.$path."/"); |
|
|
|
|
116
|
|
|
file_put_contents($path . "{$this->repoName}{$type}.php", $template); |
117
|
|
|
|
118
|
|
|
} |
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Check if folder exist |
122
|
|
|
* @param string $path class path |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function checkFolder($path) |
126
|
|
|
{ |
127
|
|
|
|
128
|
|
|
if (!file_exists($path)) { |
129
|
|
|
mkdir($path, 0777, true); |
130
|
|
|
} |
131
|
|
|
return $path; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.