Completed
Pull Request — master (#2473)
by jxlwqq
02:37
created

InstallCommand::getStub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Console;
4
5
use Encore\Admin\Auth\Database\Administrator;
6
use Illuminate\Console\Command;
7
8
class InstallCommand extends Command
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $name = 'admin:install';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Install the admin package';
23
24
    /**
25
     * Install directory.
26
     *
27
     * @var string
28
     */
29
    protected $directory = '';
30
31
    /**
32
     * Execute the console command.
33
     *
34
     * @return void
35
     */
36
    public function handle()
37
    {
38
        $this->initResource();
39
40
        $this->initDatabase();
41
42
        $this->initAdminDirectory();
43
    }
44
45
    /**
46
     * Publish assets, configuration, language and migration files.
47
     *
48
     * @return void
49
     */
50
    public function initResource()
51
    {
52
        $this->call('vendor:publish', ['--provider' => 'Encore\Admin\AdminServiceProvider']);
53
    }
54
55
56
    /**
57
     * Create tables and seed it.
58
     *
59
     * @return void
60
     */
61
    public function initDatabase()
62
    {
63
        $this->call('migrate');
64
65
        if (Administrator::count() == 0) {
66
            $this->call('db:seed', ['--class' => \Encore\Admin\Auth\Database\AdminTablesSeeder::class]);
67
        }
68
    }
69
70
    /**
71
     * Initialize the admAin directory.
72
     *
73
     * @return void
74
     */
75
    protected function initAdminDirectory()
76
    {
77
        $this->directory = config('admin.directory');
78
79
        if (is_dir($this->directory)) {
80
            $this->line("<error>{$this->directory} directory already exists !</error> ");
81
82
            return;
83
        }
84
85
        $this->makeDir('/');
86
        $this->line('<info>Admin directory was created:</info> '.str_replace(base_path(), '', $this->directory));
87
88
        $this->makeDir('Controllers');
89
90
        $this->createHomeController();
91
        $this->createExampleController();
92
93
        $this->createBootstrapFile();
94
        $this->createRoutesFile();
95
    }
96
97
    /**
98
     * Create HomeController.
99
     *
100
     * @return void
101
     */
102 View Code Duplication
    public function createHomeController()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
103
    {
104
        $homeController = $this->directory.'/Controllers/HomeController.php';
105
        $contents = $this->getStub('HomeController');
106
107
        $this->laravel['files']->put(
108
            $homeController,
109
            str_replace('DummyNamespace', config('admin.route.namespace'), $contents)
110
        );
111
        $this->line('<info>HomeController file was created:</info> '.str_replace(base_path(), '', $homeController));
112
    }
113
114
    /**
115
     * Create HomeController.
116
     *
117
     * @return void
118
     */
119 View Code Duplication
    public function createExampleController()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
120
    {
121
        $exampleController = $this->directory.'/Controllers/ExampleController.php';
122
        $contents = $this->getStub('ExampleController');
123
124
        $this->laravel['files']->put(
125
            $exampleController,
126
            str_replace('DummyNamespace', config('admin.route.namespace'), $contents)
127
        );
128
        $this->line('<info>ExampleController file was created:</info> '.str_replace(base_path(), '', $exampleController));
129
    }
130
131
    /**
132
     * Create routes file.
133
     *
134
     * @return void
135
     */
136
    protected function createBootstrapFile()
137
    {
138
        $file = $this->directory.'/bootstrap.php';
139
140
        $contents = $this->getStub('bootstrap');
141
        $this->laravel['files']->put($file, $contents);
142
        $this->line('<info>Bootstrap file was created:</info> '.str_replace(base_path(), '', $file));
143
    }
144
145
    /**
146
     * Create routes file.
147
     *
148
     * @return void
149
     */
150 View Code Duplication
    protected function createRoutesFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
151
    {
152
        $file = $this->directory.'/routes.php';
153
154
        $contents = $this->getStub('routes');
155
        $this->laravel['files']->put($file, str_replace('DummyNamespace', config('admin.route.namespace'), $contents));
156
        $this->line('<info>Routes file was created:</info> '.str_replace(base_path(), '', $file));
157
    }
158
159
    /**
160
     * Get stub contents.
161
     *
162
     * @param $name
163
     *
164
     * @return string
165
     */
166
    protected function getStub($name)
167
    {
168
        return $this->laravel['files']->get(__DIR__."/stubs/$name.stub");
169
    }
170
171
    /**
172
     * Make new directory.
173
     *
174
     * @param string $path
175
     */
176
    protected function makeDir($path = '')
177
    {
178
        $this->laravel['files']->makeDirectory("{$this->directory}/$path", 0755, true, true);
179
    }
180
}
181