Completed
Push — master ( cdb1c3...ab4554 )
by Prateek
04:40 queued 02:13
created

Generate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Prateekkarki\Laragen\Commands;
4
5
use Illuminate\Console\Command;
6
7
class Generate extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'laragen:make';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Generate code for your project';
22
23
    /**
24
     * Create a new command instance.
25
     *
26
     * @return void
27
     */
28
    public function __construct()
29
    {
30
        parent::__construct();
31
    }
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return mixed
37
     */
38
    public function handle()
39
    {
40
        $config = config('laragen');
41
        foreach ($config['modules'] as $model => $module) {
42
            $this->migration(ucfirst(str_singular($model)), $module);
43
        }
44
    }
45
46
    protected function migration($model, $module)
0 ignored issues
show
Unused Code introduced by
The parameter $module is not used and could be removed. ( Ignorable by Annotation )

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

46
    protected function migration($model, /** @scrutinizer ignore-unused */ $module)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
        $migrationTemplate = str_replace(
49
            [
50
                '{{modelName}}',
51
                '{{modelNamePlural}}',
52
                '{{modelNamePluralLowerCase}}',
53
                '{{modelTableData}}',
54
                '{{modelTimestampType}}'
55
            ],
56
            [
57
                $model,
58
                str_plural($model),
59
                strtolower(str_plural($model)),
60
                '',
61
                ''
62
            ],
63
            $this->getStub('Migration')
64
        );
65
        file_put_contents($this->laravel->databasePath(). "/migrations/" . date('Y_m_d_His') . "_create_" . strtolower(str_plural($model)) . "_table.php", $migrationTemplate);
66
    }
67
68
    protected function model($model)
69
    {
70
        $modelTemplate = str_replace(
71
            ['{{modelName}}'],
72
            [$model],
73
            $this->getStub('Model')
74
        );
75
76
        file_put_contents(app_path("/Models/{$model}.php"), $modelTemplate);
77
    }
78
79
    protected function controller($model)
80
    {
81
        $controllerTemplate = str_replace(
82
            [
83
                '{{modelName}}',
84
                '{{modelNamePluralLowerCase}}',
85
                '{{modelNameSingularLowerCase}}'
86
            ],
87
            [
88
                $model,
89
                strtolower(str_plural($model)),
90
                strtolower($model)
91
            ],
92
            $this->getStub('Controller')
93
        );
94
95
        file_put_contents(app_path("/Http/Controllers/{$model}Controller.php"), $controllerTemplate);
96
    }
97
98
    protected function getStub($type)
99
    {
100
        return file_get_contents(__DIR__ . "/../resources/stubs/" . $type . ".stub");
101
    }
102
}
103