1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
class InstallCommand extends Command |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The console command name. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $signature = 'admin:install'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Install the admin package'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Install directory. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $directory = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Execute the console command. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function handle() |
36
|
|
|
{ |
37
|
|
|
$this->initDatabase(); |
38
|
|
|
|
39
|
|
|
$this->initAdminDirectory(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create tables and seed it. |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function initDatabase() |
48
|
|
|
{ |
49
|
|
|
$this->call('migrate'); |
50
|
|
|
|
51
|
|
|
$userModel = config('admin.database.users_model'); |
52
|
|
|
|
53
|
|
|
if ($userModel::count() == 0) { |
54
|
|
|
$this->call('db:seed', ['--class' => \Encore\Admin\Auth\Database\AdminTablesSeeder::class]); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Initialize the admAin directory. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
protected function initAdminDirectory() |
64
|
|
|
{ |
65
|
|
|
$this->directory = config('admin.directory'); |
66
|
|
|
|
67
|
|
|
if (is_dir($this->directory)) { |
68
|
|
|
$this->line("<error>{$this->directory} directory already exists !</error> "); |
69
|
|
|
|
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->makeDir('/'); |
74
|
|
|
$this->line('<info>Admin directory was created:</info> '.str_replace(base_path(), '', $this->directory)); |
75
|
|
|
|
76
|
|
|
$this->makeDir('Controllers'); |
77
|
|
|
|
78
|
|
|
$this->createHomeController(); |
79
|
|
|
$this->createAuthController(); |
80
|
|
|
$this->createExampleController(); |
81
|
|
|
|
82
|
|
|
$this->createBootstrapFile(); |
83
|
|
|
$this->createRoutesFile(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create HomeController. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function createHomeController() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$homeController = $this->directory.'/Controllers/HomeController.php'; |
94
|
|
|
$contents = $this->getStub('HomeController'); |
95
|
|
|
|
96
|
|
|
$this->laravel['files']->put( |
97
|
|
|
$homeController, |
98
|
|
|
str_replace('DummyNamespace', config('admin.route.namespace'), $contents) |
99
|
|
|
); |
100
|
|
|
$this->line('<info>HomeController file was created:</info> '.str_replace(base_path(), '', $homeController)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create AuthController. |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function createAuthController() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$authController = $this->directory.'/Controllers/AuthController.php'; |
111
|
|
|
$contents = $this->getStub('AuthController'); |
112
|
|
|
|
113
|
|
|
$this->laravel['files']->put( |
114
|
|
|
$authController, |
115
|
|
|
str_replace('DummyNamespace', config('admin.route.namespace'), $contents) |
116
|
|
|
); |
117
|
|
|
$this->line('<info>AuthController file was created:</info> '.str_replace(base_path(), '', $authController)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Create HomeController. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function createExampleController() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$exampleController = $this->directory.'/Controllers/ExampleController.php'; |
128
|
|
|
$contents = $this->getStub('ExampleController'); |
129
|
|
|
|
130
|
|
|
$this->laravel['files']->put( |
131
|
|
|
$exampleController, |
132
|
|
|
str_replace('DummyNamespace', config('admin.route.namespace'), $contents) |
133
|
|
|
); |
134
|
|
|
$this->line('<info>ExampleController file was created:</info> '.str_replace(base_path(), '', $exampleController)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Create routes file. |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
protected function createBootstrapFile() |
143
|
|
|
{ |
144
|
|
|
$file = $this->directory.'/bootstrap.php'; |
145
|
|
|
|
146
|
|
|
$contents = $this->getStub('bootstrap'); |
147
|
|
|
$this->laravel['files']->put($file, $contents); |
148
|
|
|
$this->line('<info>Bootstrap file was created:</info> '.str_replace(base_path(), '', $file)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Create routes file. |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
protected function createRoutesFile() |
157
|
|
|
{ |
158
|
|
|
$file = $this->directory.'/routes.php'; |
159
|
|
|
|
160
|
|
|
$contents = $this->getStub('routes'); |
161
|
|
|
$this->laravel['files']->put($file, str_replace('DummyNamespace', config('admin.route.namespace'), $contents)); |
162
|
|
|
$this->line('<info>Routes file was created:</info> '.str_replace(base_path(), '', $file)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get stub contents. |
167
|
|
|
* |
168
|
|
|
* @param $name |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
protected function getStub($name) |
173
|
|
|
{ |
174
|
|
|
return $this->laravel['files']->get(__DIR__."/stubs/$name.stub"); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Make new directory. |
179
|
|
|
* |
180
|
|
|
* @param string $path |
181
|
|
|
*/ |
182
|
|
|
protected function makeDir($path = '') |
183
|
|
|
{ |
184
|
|
|
$this->laravel['files']->makeDirectory("{$this->directory}/$path", 0755, true, true); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.