Passed
Push — feature/lg ( dc598a...fe8da4 )
by Richard
04:27
created

PublishUserCommand::copyViews()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace PWWEB\Artomator\Commands\Publish;
4
5
use InfyOm\Generator\Commands\Publish\PublishBaseCommand;
6
use InfyOm\Generator\Utils\FileUtil;
7
8
class PublishUserCommand extends PublishBaseCommand
9
{
10
    /**
11
     * ALL REFERENCES TO (get_template\(\')([a-z_\.]+)(', 'laravel-generator'\))
12
     * REPLACED WITH: get_artomator_template('$2').
13
     */
14
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'artomator.publish:user';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Publishes Users CRUD file';
28
29
    /**
30
     * Execute the command.
31
     *
32
     * @return void
33
     */
34
    public function handle()
35
    {
36
        $this->copyViews();
37
        $this->updateRoutes();
38
        $this->updateMenu();
39
        $this->publishUserController();
40
        if (config('infyom.laravel_generator.options.repository_pattern')) {
41
            $this->publishUserRepository();
42
        }
43
        $this->publishCreateUserRequest();
44
        $this->publishUpdateUserRequest();
45
    }
46
47
    private function copyViews()
48
    {
49
        $viewsPath = config('infyom.laravel_generator.path.views', resource_path('views/'));
50
        $templateType = config('infyom.laravel_generator.templates', 'adminlte-templates');
51
52
        $this->createDirectories($viewsPath.'users');
53
54
        $files = $this->getViews();
55
56
        foreach ($files as $stub => $blade) {
57
            $sourceFile = get_template_file_path('scaffold/'.$stub, $templateType);
58
            $destinationFile = $viewsPath.$blade;
59
            $this->publishFile($sourceFile, $destinationFile, $blade);
60
        }
61
    }
62
63
    private function createDirectories($dir)
64
    {
65
        FileUtil::createDirectoryIfNotExist($dir);
66
    }
67
68
    private function getViews()
69
    {
70
        return [
71
            'users/create'      => 'users/create.blade.php',
72
            'users/edit'        => 'users/edit.blade.php',
73
            'users/fields'      => 'users/fields.blade.php',
74
            'users/index'       => 'users/index.blade.php',
75
            'users/show'        => 'users/show.blade.php',
76
            'users/show_fields' => 'users/show_fields.blade.php',
77
            'users/table'       => 'users/table.blade.php',
78
        ];
79
    }
80
81
    private function updateRoutes()
82
    {
83
        $path = config('infyom.laravel_generator.path.routes', base_path('routes/web.php'));
84
85
        $routeContents = file_get_contents($path);
86
87
        $routesTemplate = get_artomator_template('routes.user');
88
89
        $routeContents .= "\n\n".$routesTemplate;
90
91
        file_put_contents($path, $routeContents);
92
        $this->comment("\nUser route added");
93
    }
94
95
    private function updateMenu()
96
    {
97
        $viewsPath = config('infyom.laravel_generator.path.views', resource_path('views/'));
98
        $templateType = config('infyom.laravel_generator.templates', 'adminlte-templates');
99
        $path = $viewsPath.'layouts/menu.blade.php';
100
        $menuContents = file_get_contents($path);
101
        $sourceFile = file_get_contents(get_template_file_path('scaffold/users/menu', $templateType));
102
        $menuContents .= "\n".$sourceFile;
103
104
        file_put_contents($path, $menuContents);
105
        $this->comment("\nUser Menu added");
106
    }
107
108
    private function publishUserController()
109
    {
110
        $templateData = get_template('user/user_controller', 'laravel-generator');
111
        if (false === config('infyom.laravel_generator.options.repository_pattern')) {
112
            $templateData = get_template('user/user_controller_without_repository', 'laravel-generator');
113
            $templateData = $this->fillTemplate($templateData);
114
        }
115
116
        $templateData = $this->fillTemplate($templateData);
117
118
        $controllerPath = config('infyom.laravel_generator.path.controller', app_path('Http/Controllers/'));
119
120
        $fileName = 'UserController.php';
121
122
        if (file_exists($controllerPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
123
            return;
124
        }
125
126
        FileUtil::createFile($controllerPath, $fileName, $templateData);
127
128
        $this->info('UserController created');
129
    }
130
131
    private function publishUserRepository()
132
    {
133
        $templateData = get_template('user/user_repository', 'laravel-generator');
134
135
        $templateData = $this->fillTemplate($templateData);
136
137
        $repositoryPath = config('infyom.laravel_generator.path.repository', app_path('Repositories/'));
138
139
        $fileName = 'UserRepository.php';
140
141
        FileUtil::createDirectoryIfNotExist($repositoryPath);
142
143
        if (file_exists($repositoryPath.$fileName) && false === $this->confirmOverwrite($fileName)) {
144
            return;
145
        }
146
147
        FileUtil::createFile($repositoryPath, $fileName, $templateData);
148
149
        $this->info('UserRepository created');
150
    }
151
152
    private function publishCreateUserRequest()
153
    {
154
        $templateData = get_template('user/create_user_request', 'laravel-generator');
155
156
        $templateData = $this->fillTemplate($templateData);
157
158
        $requestPath = config('infyom.laravel_generator.path.request', app_path('Http/Requests/'));
159
160
        $fileName = 'CreateUserRequest.php';
161
162
        FileUtil::createDirectoryIfNotExist($requestPath);
163
164
        if (file_exists($requestPath.$fileName) && false === $this->confirmOverwrite($fileName)) {
165
            return;
166
        }
167
168
        FileUtil::createFile($requestPath, $fileName, $templateData);
169
170
        $this->info('CreateUserRequest created');
171
    }
172
173
    private function publishUpdateUserRequest()
174
    {
175
        $templateData = get_template('user/update_user_request', 'laravel-generator');
176
177
        $templateData = $this->fillTemplate($templateData);
178
179
        $requestPath = config('infyom.laravel_generator.path.request', app_path('Http/Requests/'));
180
181
        $fileName = 'UpdateUserRequest.php';
182
        if (file_exists($requestPath.$fileName) && false === $this->confirmOverwrite($fileName)) {
183
            return;
184
        }
185
186
        FileUtil::createFile($requestPath, $fileName, $templateData);
187
188
        $this->info('UpdateUserRequest created');
189
    }
190
191
    /**
192
     * Replaces dynamic variables of template.
193
     * THIS IS A NEW FUNCTION ADDED.
194
     *
195
     * @param string $templateData
196
     *
197
     * @return string
198
     */
199
    private function fillLicense($templateData)
200
    {
201
        $replacements = [
202
            '$LICENSE_PACKAGE$' => config('pwweb.artomator.license.package', 'boo'),
203
            '$LICENSE_AUTHORS$' => license_authors(config('pwweb.artomator.license.authors')),
204
            '$LICENSE_COPYRIGHT$' => config('pwweb.artomator.license.copyright'),
205
            '$LICENSE$' => config('pwweb.artomator.license.license'),
206
        ];
207
        foreach ($replacements as $key => $replacement) {
208
            $templateData = str_replace($key, $replacement, $templateData);
209
        }
210
211
        return $templateData;
212
    }
213
214
    /**
215
     * Replaces dynamic variables of template.
216
     *
217
     * @param string $templateData
218
     *
219
     * @return string
220
     */
221
    private function fillTemplate($templateData)
222
    {
223
        $templateData = str_replace('$NAMESPACE_CONTROLLER$', config('infyom.laravel_generator.namespace.controller'), $templateData);
224
225
        $templateData = str_replace('$NAMESPACE_REQUEST$', config('infyom.laravel_generator.namespace.request'), $templateData);
226
227
        $templateData = str_replace('$NAMESPACE_REPOSITORY$', config('infyom.laravel_generator.namespace.repository'), $templateData);
228
        $templateData = str_replace('$NAMESPACE_USER$', config('auth.providers.users.model'), $templateData);
229
230
        // return $templateData;
231
        // ADDED THE FOLLOWING LINE:
232
        return $this->fillLicense($templateData);
233
    }
234
235
    /**
236
     * Get the console command options.
237
     *
238
     * @return array
239
     */
240
    public function getOptions()
241
    {
242
        return [];
243
    }
244
245
    /**
246
     * Get the console command arguments.
247
     *
248
     * @return array
249
     */
250
    protected function getArguments()
251
    {
252
        return [];
253
    }
254
}
255