Passed
Push — feature/lg ( 3c06af...b2e7fb )
by Richard
03:07
created

LayoutPublishCommand   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
eloc 83
c 2
b 0
f 0
dl 0
loc 188
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A copyView() 0 17 3
A createDirectories() 0 7 1
A handle() 0 5 1
A getArguments() 0 3 1
A getLocaleViews() 0 14 1
A fillTemplate() 0 15 1
A getOptions() 0 4 1
A publishHomeController() 0 17 3
A updateRoutes() 0 17 3
A getViews() 0 30 2
1
<?php
2
3
namespace PWWEB\Artomator\Commands\Publish;
4
5
use Illuminate\Support\Str;
6
use InfyOm\Generator\Utils\FileUtil;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class LayoutPublishCommand extends PublishBaseCommand
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'artomator.publish:layout';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Publishes auth files';
24
25
    /**
26
     * Execute the command.
27
     *
28
     * @return void
29
     */
30
    public function handle()
31
    {
32
        $this->copyView();
33
        $this->updateRoutes();
34
        $this->publishHomeController();
35
    }
36
37
    private function copyView()
38
    {
39
        $viewsPath = config('infyom.laravel_generator.path.views', resource_path('views/'));
40
        $templateType = config('infyom.laravel_generator.templates', 'adminlte-templates');
41
42
        $this->createDirectories($viewsPath);
43
44
        if ($this->option('localized')) {
45
            $files = $this->getLocaleViews();
46
        } else {
47
            $files = $this->getViews();
48
        }
49
50
        foreach ($files as $stub => $blade) {
51
            $sourceFile = get_template_file_path('scaffold/'.$stub, $templateType);
52
            $destinationFile = $viewsPath.$blade;
53
            $this->publishFile($sourceFile, $destinationFile, $blade);
54
        }
55
    }
56
57
    private function createDirectories($viewsPath)
58
    {
59
        FileUtil::createDirectoryIfNotExist($viewsPath.'layouts');
60
        FileUtil::createDirectoryIfNotExist($viewsPath.'auth');
61
62
        FileUtil::createDirectoryIfNotExist($viewsPath.'auth/passwords');
63
        FileUtil::createDirectoryIfNotExist($viewsPath.'auth/emails');
64
    }
65
66
    private function getViews()
67
    {
68
        $views = [
69
            'layouts/app'               => 'layouts/app.blade.php',
70
            'layouts/sidebar'           => 'layouts/sidebar.blade.php',
71
            'layouts/datatables_css'    => 'layouts/datatables_css.blade.php',
72
            'layouts/datatables_js'     => 'layouts/datatables_js.blade.php',
73
            'layouts/menu'              => 'layouts/menu.blade.php',
74
            'layouts/home'              => 'home.blade.php',
75
            'auth/login'                => 'auth/login.blade.php',
76
            'auth/register'             => 'auth/register.blade.php',
77
            'auth/email'                => 'auth/passwords/email.blade.php',
78
            'auth/reset'                => 'auth/passwords/reset.blade.php',
79
            'emails/password'           => 'auth/emails/password.blade.php',
80
        ];
81
82
        $version = $this->getApplication()->getVersion();
83
        if (Str::contains($version, '6.')) {
84
            $verifyView = [
85
                'auth/verify_6' => 'auth/verify.blade.php',
86
            ];
87
        } else {
88
            $verifyView = [
89
                'auth/verify' => 'auth/verify.blade.php',
90
            ];
91
        }
92
93
        $views = array_merge($views, $verifyView);
94
95
        return $views;
96
    }
97
98
    private function getLocaleViews()
99
    {
100
        return [
101
            'layouts/app_locale'        => 'layouts/app.blade.php',
102
            'layouts/sidebar_locale'    => 'layouts/sidebar.blade.php',
103
            'layouts/datatables_css'    => 'layouts/datatables_css.blade.php',
104
            'layouts/datatables_js'     => 'layouts/datatables_js.blade.php',
105
            'layouts/menu'              => 'layouts/menu.blade.php',
106
            'layouts/home'              => 'home.blade.php',
107
            'auth/login_locale'         => 'auth/login.blade.php',
108
            'auth/register_locale'      => 'auth/register.blade.php',
109
            'auth/email_locale'         => 'auth/passwords/email.blade.php',
110
            'auth/reset_locale'         => 'auth/passwords/reset.blade.php',
111
            'emails/password_locale'    => 'auth/emails/password.blade.php',
112
        ];
113
    }
114
115
    private function updateRoutes()
116
    {
117
        $path = config('infyom.laravel_generator.path.routes', base_path('routes/web.php'));
118
119
        $prompt = 'Existing routes web.php file detected. Should we add standard auth routes? (y|N) :';
120
        if (file_exists($path) && ! $this->confirmOverwrite($path, $prompt)) {
121
            return;
122
        }
123
124
        $routeContents = file_get_contents($path);
125
126
        $routesTemplate = get_template('routes.auth', 'laravel-generator');
127
128
        $routeContents .= "\n\n".$routesTemplate;
129
130
        file_put_contents($path, $routeContents);
131
        $this->comment("\nRoutes added");
132
    }
133
134
    private function publishHomeController()
135
    {
136
        $templateData = get_template('home_controller', 'laravel-generator');
137
138
        $templateData = $this->fillTemplate($templateData);
139
140
        $controllerPath = config('infyom.laravel_generator.path.controller', app_path('Http/Controllers/'));
141
142
        $fileName = 'HomeController.php';
143
144
        if (file_exists($controllerPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
145
            return;
146
        }
147
148
        FileUtil::createFile($controllerPath, $fileName, $templateData);
149
150
        $this->info('HomeController created');
151
    }
152
153
    /**
154
     * Replaces dynamic variables of template.
155
     *
156
     * @param string $templateData
157
     *
158
     * @return string
159
     */
160
    private function fillTemplate($templateData)
161
    {
162
        $templateData = str_replace(
163
            '$NAMESPACE_CONTROLLER$',
164
            config('infyom.laravel_generator.namespace.controller'),
165
            $templateData
166
        );
167
168
        $templateData = str_replace(
169
            '$NAMESPACE_REQUEST$',
170
            config('infyom.laravel_generator.namespace.request'),
171
            $templateData
172
        );
173
174
        return $templateData;
175
    }
176
177
    /**
178
     * Get the console command options.
179
     *
180
     * @return array
181
     */
182
    public function getOptions()
183
    {
184
        return [
185
            ['localized', null, InputOption::VALUE_NONE, 'Localize files.'],
186
        ];
187
    }
188
189
    /**
190
     * Get the console command arguments.
191
     *
192
     * @return array
193
     */
194
    protected function getArguments()
195
    {
196
        return [];
197
    }
198
}
199