Passed
Push — master ( 864347...46e361 )
by Bruno
09:55
created

ModelariumCommand::getPathGraphql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Modelarium\GeneratedCollection;
7
use Modelarium\GeneratedItem;
8
use Modelarium\Laravel\Processor as LaravelProcessor;
9
use Symfony\Component\Console\Output\BufferedOutput;
10
11
// use Formularium\FrameworkComposer;
12
// use Formularium\Frontend\Blade\Framework as FrameworkBlade;
13
// use Formularium\Frontend\Vue\Framework as FrameworkVue;
14
15
class ModelariumCommand extends Command
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'modelarium:scaffold
23
        {name : The model name}
24
        {--framework=* : The frameworks to use}
25
        {--overwrite : overwrite files if they exist}
26
        {--lighthouse : use lighthouse directives}
27
        {--everything : make everything}
28
        {--model : make model}
29
        {--event : make event}
30
        {--migration : make migration}
31
        {--factory : make factory}
32
        {--seed : make seed}
33
        {--policy : make policy}
34
        {--frontend : make frontend files}';
35
36
    /**
37
     * The console command description.
38
     *
39
     * @var string
40
     */
41
    protected $description = 'Creates scaffolding using Modelarium';
42
43
    /**
44
     * Create a new command instance.
45
     *
46
     * @return void
47
     */
48
    public function __construct()
49
    {
50
        parent::__construct();
51
    }
52
53
    /**
54
     * Execute the console command.
55
     *
56
     * @return mixed
57
     */
58
    public function handle()
59
    {
60
        $name = $this->argument('name');
61
62
        $processor = new LaravelProcessor();
63
64
        // parse args
65
        $processor->setRunModel($this->option('model') || $this->option('everything'));
66
        $processor->setRunMigration($this->option('migration') || $this->option('everything'));
67
        $processor->setRunFactory($this->option('factory') || $this->option('everything'));
68
        $processor->setRunSeed($this->option('seed') || $this->option('everything'));
69
        $processor->setRunPolicy($this->option('policy') || $this->option('everything'));
70
        $processor->setRunEvent($this->option('event') || $this->option('everything'));
71
72
        $files = [
73
            __DIR__ . '/../../Graphql/definitions.graphql',
74
            __DIR__ . '/../../../Types/Graphql/scalars.graphql'
75
        ];
76
77
        if ($this->option('lighthouse')) {
78
            // TODO: see issue #2
79
            // generate lighthouse directives
80
            // $output = new BufferedOutput();
81
            // $this->call('lighthouse:ide-helper');
82
            // $output->fetch();
83
84
            // @phpstan-ignore-next-line
85
            $files[] = base_path('schema-directives.graphql');
0 ignored issues
show
Bug introduced by
The function base_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

85
            $files[] = /** @scrutinizer ignore-call */ base_path('schema-directives.graphql');
Loading history...
86
        } else {
87
            // @phpstan-ignore-next-line
88
            $files[] = base_path(__DIR__ . '/../../Graphql/definitionsLighthouse.graphql');
89
        }
90
91
        if ($name === '*' || $name === 'all') {
92
            // @phpstan-ignore-next-line
93
            $path = base_path('graphql');
94
            $dir = \Safe\scandir($path);
95
96
            // parse directives from lighthouse
97
            $modelNames = array_diff($dir, array('.', '..'));
98
            
99
            foreach ($modelNames as $n) {
100
                if (mb_strpos($n, '.graphql') === false) {
101
                    continue;
102
                }
103
                // @phpstan-ignore-next-line
104
                $files[] = base_path('graphql/' . $n);
105
            }
106
            $processor->processFiles($files);
107
        } elseif (!$name || is_array($name)) {
108
            $this->error('Invalid name parameter');
109
            return;
110
        } else {
111
            try {
112
                $data = \Safe\file_get_contents($this->getPathGraphql($name));
113
                $processor->processString($data);
114
            } catch (\Safe\Exceptions\FilesystemException $e) {
115
                $this->error("Cannot open model $name");
116
            }
117
        }
118
119
        $this->writeFiles(
120
            $processor->getCollection(),
121
            // @phpstan-ignore-next-line
122
            base_path(),
123
            (bool)$this->option('overwrite')
124
        );
125
        $this->info('Finished. You might want to run `composer dump-autoload`');
126
    }
127
128
    protected function frontend(): void
129
    {
130
        // TODO
131
        // // setup stuff
132
        // $frameworks = $this->option('framework');
133
        // FrameworkComposer::set($frameworks);
134
135
        // /**
136
        //  * @var FrameworkVue $vue
137
        //  */
138
        // $vue = FrameworkComposer::getByName('Vue');
139
        // $blade = FrameworkComposer::getByName('Blade');
140
141
        // if ($this->hasOption('stubdir')) {
142
        //     $this->stubDir = $this->option('stubdir');
143
        // }
144
145
146
147
        // if ($this->option('frontend') || $this->option('everything')) {
148
        //     if ($vue) {
149
        //         $this->makeVueScaffold();
150
        //         $this->makeVue($vue, 'Base', 'viewable');
151
        //         $this->makeVue($vue, 'Item', 'viewable');
152
        //         $this->makeVue($vue, 'ListPage', 'viewable');
153
        //         $this->makeVue($vue, 'ShowPage', 'viewable');
154
        //         $this->makeVue($vue, 'EditPage', 'editable');
155
        //         $this->line('Generated Vue');
156
        //     } elseif ($blade) {
157
        //         $this->makeBlade($blade, 'show', 'viewable');
158
        //         $this->makeBlade($blade, 'index', 'viewable');
159
        //         $this->makeBlade($blade, 'form', 'editable');
160
        //         $this->line('Generated Blade');
161
        //     } else {
162
        //         // TODO: react?
163
        //     }
164
        // }
165
    }
166
167
    protected function getPathGraphql(string $name): string
168
    {
169
        // @phpstan-ignore-next-line
170
        return base_path('graphql/' . $name . '.graphql');
0 ignored issues
show
Bug introduced by
The function base_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

170
        return /** @scrutinizer ignore-call */ base_path('graphql/' . $name . '.graphql');
Loading history...
171
    }
172
173
    public function writeFiles(GeneratedCollection $collection, string $basepath, bool $overwrite = true): self
174
    {
175
        foreach ($collection as $element) {
176
            /**
177
             * @var GeneratedItem $element
178
             */
179
            $path = $basepath . '/' . $element->filename;
180
            $this->writeFile(
181
                $path,
182
                ($element->onlyIfNewFile ? false : $overwrite),
183
                $element->contents
184
            );
185
        }
186
        return $this;
187
    }
188
189
    /**
190
     * Takes a stub file and generates the target file with replacements.
191
     *
192
     * @param string $targetPath The path for the stub file.
193
     * @param boolean $overwrite
194
     * @param string $data The data to write
195
     * @return void
196
     */
197
    protected function writeFile(string $targetPath, bool $overwrite, string $data)
198
    {
199
        if (file_exists($targetPath) && !$overwrite) {
200
            $this->comment("File $targetPath already exists, not overwriting.");
201
            return;
202
        }
203
204
        $dir = dirname($targetPath);
205
        if (!is_dir($dir)) {
206
            \Safe\mkdir($dir, 0777, true);
207
        }
208
209
        $ret = \Safe\file_put_contents($targetPath, $data);
210
        if (!$ret) {
211
            $this->error("Cannot write to $targetPath");
212
            return;
213
        }
214
        $this->line("Wrote $targetPath");
215
    }
216
}
217