Passed
Push — master ( 9bc279...a50a5d )
by Mohammad
02:42
created

Generator::makeRepositoryPatternFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Shamaseen\Repository\Generator\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\File;
7
8
/**
9
 * Class RepositoryGenerator
10
 * @package Shamaseen\Repository\Generator\Commands
11
 */
12
class Generator extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'make:repository
20
    {name : Class (singular) for example User} {--view}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create repository generator';
28
29
    /**
30
     * The repository name.
31
     *
32
     * @var string
33
     */
34
    protected $repoName;
35
    /**
36
     * The repository name space.
37
     *
38
     * @var string
39
     */
40
    protected $repoNamespace;
41
42
    /**
43
     * Create a new command instance.
44
     *
45
     */
46
    public function __construct()
47
    {
48
        parent::__construct();
49
    }
50
51
    /**
52
     * Execute the console command.
53
     *
54
55
     */
56
    public function handle()
57
    {
58
        $file = preg_split( " (/|\\\\) ", (string)$this->argument('name')) ?? [];
59
        $this->repoName = $file[count($file) - 1];
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        $this->repoName = $file[count(/** @scrutinizer ignore-type */ $file) - 1];
Loading history...
60
        if($this->option('only-view'))
61
        {
62
            $this->makeViewsAndLanguage();
63
            return null;
64
        }
65
66
        $this->makeRepositoryPatternFiles($file);
67
    }
68
69
    function makeRepositoryPatternFiles($file)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
70
    {
71
        unset($file[count($file) - 1]);
72
        $path = implode("\\", $file);
73
74
        $model= str_plural(\Config::get('repository.model'));
75
        $interface= str_plural(\Config::get('repository.interface'));
76
        $repository= str_plural(\Config::get('repository.repository'));
77
78
        $this->generate($path, \Config::get('repository.controllers_folder'), 'Controller');
79
        $this->generate($path, $model, 'Entity');
80
        $this->generate($path, \Config::get('repository.requests_folder'), 'Request');
81
        $this->generate($path, $interface, 'Interface');
82
        $this->generate($path, $repository, 'Repository');
83
84
        File::append(\Config::get('repository.route_path') . '/web.php', "\n" . 'Route::resource(\'' . strtolower(str_plural($this->repoName)) . "', '".$path."\\".$this->repoName."Controller');");
85
    }
86
87
    function makeViewsAndLanguage()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
    {
89
        foreach (\Config::get('repository.languages') as $lang)
90
        {
91
            $this->generate(lcfirst($this->repoName),\Config::get('repository.lang_path')."/{$lang}" , 'lang');
92
        }
93
        $this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'create');
94
        $this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'edit');
95
        $this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'index');
96
        $this->generate(lcfirst($this->repoName),\Config::get('repository.resources_path')."/views" , 'show');
97
    }
98
99
    /**
100
     * Get stub content to generate needed files
101
     *
102
     * @param string $type determine which stub should choose to get content
103
     * @return false|string
104
     */
105
    protected function getStub($type)
106
    {
107
        return file_get_contents(\Config::get('repository.stubs_path') . "/$type.stub");
108
    }
109
110
    /**
111
     * @param string $path Class path
112
     * @param string $folder default path to generate in
113
     * @param string $type define which kind of files should generate
114
     * @return false
115
     */
116
    protected function generate($path, $folder, $type)
117
    {
118
        $content = $this->getStub($type);
119
120
        if($content === false)
121
        {
122
            echo 'file '.$type.".stub is not exist !";
123
            return false;
124
        }
125
126
        $template = str_replace(
127
            [
128
                '{{modelName}}',
129
                "{{folder}}",
130
                "{{path}}",
131
                "{{modelBaseFolderName}}",
132
                "{{interfaceBaseFolderName}}",
133
            ],
134
            [
135
                $this->repoName,
136
                str_plural($folder),
137
                $path,
138
                str_plural(\Config::get('repository.model','Entity')),
139
                str_plural(\Config::get('repository.interface','Interface')),
140
            ],
141
            $this->getStub($type)
142
        );
143
144
        $folder = str_replace('\\','/',$folder);
145
        $path = str_replace('\\','/',$path);
146
        
147
        switch ($type)
148
        {
149
            case 'Entity':
150
                $filePath = $this->getFolderOrCreate(\Config::get('repository.app_path') . "/{$folder}/{$path}");
151
                $filePath = rtrim($filePath,'/');
152
                $filePath .= "/";
153
                file_put_contents($filePath . "{$this->repoName}.php", $template);
154
                break;
155
            case 'Controller':
156
            case 'Request':
157
            case 'Repository':
158
            case 'Interface':
159
                $filePath = $this->getFolderOrCreate(\Config::get('repository.app_path') . "/{$folder}/{$path}");
160
                $filePath = rtrim($filePath,'/');
161
                $filePath .= "/";
162
                file_put_contents($filePath . "{$this->repoName}{$type}.php", $template);
163
                break;
164
            case 'create':
165
            case 'edit':
166
            case 'index':
167
            case 'show':
168
                $filePath = $this->getFolderOrCreate($folder."/".str_plural($path))."/";
169
                $repoName = lcfirst($type);
170
                file_put_contents($filePath . $repoName.".blade.php", $template);
171
            break;
172
            default:
173
                $filePath = $this->getFolderOrCreate($folder)."/";
174
                $repoName = lcfirst($this->repoName);
175
                file_put_contents($filePath . $repoName.".php", $template);
176
        }
177
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type false.
Loading history...
178
    }
179
180
    /**
181
     * Check if folder exist
182
     * @param string $path class path
183
     * @return string
184
     */
185
    public function getFolderOrCreate($path)
186
    {
187
        if (!file_exists($path)) {
188
            mkdir($path, 0777, true);
189
        }
190
        return $path;
191
    }
192
}
193