Completed
Push — master ( f8086d...5fe97e )
by Arman
17s queued 14s
created

ModuleGenerateCommand::viewBubblesTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.0
13
 */
14
15
namespace Quantum\Console\Commands;
16
17
use Quantum\Libraries\Storage\FileSystem;
18
use Quantum\Console\QtCommand;
19
use Quantum\Di\Di;
20
21
/**
22
 * Class OpenApiUiAssetsCommand
23
 * @package Quantum\Console\Commands
24
 */
25
class ModuleGenerateCommand extends QtCommand
26
{
27
28
    /**
29
     * File System
30
     * @var FileSystem
31
     */
32
    protected $fs;
33
34
    /**
35
     * Command name
36
     * @var string
37
     */
38
    protected $name = 'module:generate';
39
40
    /**
41
     * Command description
42
     * @var string
43
     */
44
    protected $description = 'Generate new module';
45
46
    /**
47
     * Command arguments
48
     * @var string[][]
49
     */
50
    protected $args = [
51
        ['module', 'required', 'The module name'],
52
    ];
53
54
    /**
55
     * Command help text
56
     * @var string
57
     */
58
    protected $help = 'The command will create files for new module';
59
60
    /**
61
     * Command options
62
     * @var array
63
     */
64
    protected $options = [
65
        ['yes', 'y', 'none', 'Module enabled status']
66
    ];
67
68
    /**
69
     * Folder names
70
     * @var string[][]
71
     */
72
    protected $folders = [
73
        DS,
74
        DS . 'Controllers',
75
        DS . 'Models',
76
        DS . 'Config',
77
        DS . 'Views',
78
        DS . 'Views' . DS . 'layouts',
79
        DS . 'Views' . DS . 'partials',
80
    ];
81
82
    /**
83
     * Executes the command
84
     * @throws \Quantum\Exceptions\FileSystemException
85
     * @throws \Quantum\Exceptions\DiException
86
     */
87
    public function exec()
88
    {
89
        $this->fs = Di::get(FileSystem::class);
90
        $newModuleName = ucfirst($this->getArgument('module'));
91
92
        $modulesConfigPath = base_dir() . DS . 'shared' . DS . 'config' . DS . 'modules.php';
93
        $modules = require_once $modulesConfigPath;
94
95
        foreach ($modules['modules'] as $module => $options) {
96
            if ($module == $newModuleName  || $options['prefix'] == strtolower($newModuleName)) {
97
                $this->error('A module or prefix named ' . $newModuleName . ' already exists');
98
                return;
99
            }
100
        }
101
        
102
        $this->fs->put(
103
            $modulesConfigPath,
104
            str_replace(
105
                "'modules' => [",
106
                $this->addModuleConfig($newModuleName),
107
                $this->fs->get($modulesConfigPath)
108
            )
109
        );
110
111
        foreach ($this->folders as $folder) {
112
            $this->fs->makeDirectory(modules_dir() . DS . $newModuleName . $folder);
0 ignored issues
show
Bug introduced by
Are you sure $folder of type string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
            $this->fs->makeDirectory(modules_dir() . DS . $newModuleName . /** @scrutinizer ignore-type */ $folder);
Loading history...
113
        }
114
115
        $files = [
116
            'Controllers' . DS . 'MainController.php'       => $this->controllerTemplate($newModuleName),
117
            'Views' . DS . 'index.php'                      => $this->viewTemplate($newModuleName),
118
            'Views' . DS . 'layouts' . DS . 'main.php'      => $this->viewLayoutsTemplate(),
119
            'Views' . DS . 'partials' . DS . 'bubbles.php'  => $this->viewBubblesTemplate(),
120
            'Config' . DS . 'routes.php'                    => $this->routesTemplate(),
121
        ];
122
123
        foreach ($files as $file => $value) {
124
            $this->fs->put(modules_dir() . DS . $newModuleName . DS . $file, $value);
125
        }
126
127
        $this->info($newModuleName . ' module resources successfully published');
128
    }
129
130
    /**
131
     * Add module to config
132
     * @param string $module
133
     * @return string
134
     */
135
    private function addModuleConfig(string $module): string
136
    {
137
        $enabled = $this->getOption('yes') ? "true" : "false";
138
        
139
        return "'modules' => [
140
        '" . $module . "' => [
141
            'prefix' => '" . strtolower($module) . "',
142
            'enabled' => " . $enabled . ",
143
        ],";
144
    }
145
146
    /**
147
     * Controller template
148
     * @param string $moduleName
149
     * @return string
150
     */
151
    private function controllerTemplate($moduleName)
152
    {
153
        return '<?php
154
155
namespace Modules\\' . $moduleName . '\Controllers;
156
157
use Quantum\Factory\ViewFactory;
158
use Quantum\Mvc\QtController;
159
use Quantum\Http\Response;
160
161
class MainController extends QtController
162
{
163
    public function index(Response $response, ViewFactory $view)
164
    {
165
        $view->setLayout(\'layouts' . DS . 'main\');
166
        $view->setParams([
167
            \'title\' => config()->get(\'app_name\'),
168
        ]);
169
        $response->html($view->render(\'index\'));
170
    }
171
};';
172
    }
173
174
    /**
175
     * View template
176
     * @param string $moduleName
177
     * @return string
178
     */
179
    private function viewTemplate($moduleName)
180
    {
181
        return '<div class="main-wrapper teal accent-4">
182
    <div class="container wrapper">
183
        <div class="center-align white-text">
184
            <div class="logo-block">
185
                <img src="<?php echo base_url() ?>/assets/images/quantum-logo-white.png" alt="<?php echo config()->get(\'app_name\') ?>" />
186
            </div>
187
            <h1>' . strtoupper($moduleName) . ' HOME PAGE</h1>
188
        </div>
189
    </div>
190
</div>
191
<?php echo partial(\'partials' . DS . 'bubbles\') ?>';
192
    }
193
194
    /**
195
     * View bubbles template
196
     * @param string $moduleName
197
     * @return string
198
     */
199
    private function viewBubblesTemplate()
200
    {
201
        return '<ul class="bg-bubbles">
202
    <li></li>
203
    <li></li>
204
    <li></li>
205
    <li></li>
206
    <li></li>
207
    <li></li>
208
    <li></li>
209
    <li></li>
210
    <li></li>
211
    <li></li>
212
</ul>';
213
    }
214
215
    /**
216
     * viewLayouts template
217
     * @return string
218
     */
219
    private function viewLayoutsTemplate()
220
    {
221
        return '<!DOCTYPE html>
222
<html>
223
    <head>
224
        <meta charset="UTF-8">
225
        <meta name="viewport" content="width=device-width, initial-scale=1">
226
        <title><?php echo $title ?></title>
227
228
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
229
        <link rel="shortcut icon" href="<?php echo asset()->url(\'images/favicon.ico\') ?>">
230
        <link rel=\'stylesheet\' href=\'<?php echo asset()->url(\'css/materialize.min.css\') ?>\' type=\'text/css\' media=\'screen,projection\' />
231
        <link rel=\'stylesheet\' href=\'<?php echo asset()->url(\'css/custom.css\') ?>\' type=\'text/css\' />
232
    </head>
233
    <body>
234
235
        <main><?php echo view() ?></main>
236
237
        <?php echo debugbar() ?>
238
239
        <script type=\'text/javascript\' src=\'<?php echo asset()->url(\'js/materialize.min.js\') ?>\'></script>
240
        <script type=\'text/javascript\' src=\'<?php echo asset()->url(\'js/custom.js\') ?>\'></script>
241
    </body>
242
</html>';
243
    }
244
245
    /**
246
     * Routes template
247
     * @return string
248
     */
249
    private function routesTemplate()
250
    {
251
        return '<?php
252
253
use Quantum\Factory\ViewFactory;
254
use Quantum\Http\Response;
255
256
return function ($route) {
257
    $route->get(\'/\', \'MainController\', \'index\');
258
};';
259
    }
260
}
261