Issues (7)

src/Consoles/ComponentGenerator.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Joesama\Pintu\Consoles;
4
5
use ReflectionClass;
6
use PhpSchool\CliMenu\CliMenu;
7
use Illuminate\Console\Command;
8
use PhpSchool\CliMenu\MenuStyle;
9
use Illuminate\Filesystem\Filesystem;
10
use Joesama\Pintu\Components\Manager;
11
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
12
13
class ComponentGenerator extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'pintu';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create package component & routing configuration file.';
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return mixed
33
     */
34
    public function handle(Filesystem $file)
35
    {
36
        $placeHolder = 'Joesama\\Pintu\\PintuProvider';
37
38
        $console = $this;
39
40
        $itemCallable = function (CliMenu $menu) use ($placeHolder, $console, $file) {
41
            $successStyle = (new MenuStyle)
42
                ->setBg('254')
43
                ->setFg('166');
44
45
            $result = $menu->askText($successStyle)
46
                ->setPromptText('Type service provider namespace. [esc] to exit')
47
                ->setPlaceholderText($placeHolder)
48
                ->setValidationFailedText('Please type full qualified service provider namespace')
49
                ->setValidator(function ($provider) use ($placeHolder, $console) {
50
                    if (!$console->fileIsExist($provider)) {
51
                        $this->setValidationFailedText("Class {$provider} must be exist");
0 ignored issues
show
The method setValidationFailedText() does not exist on Joesama\Pintu\Consoles\ComponentGenerator. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

51
                        $this->/** @scrutinizer ignore-call */ 
52
                               setValidationFailedText("Class {$provider} must be exist");
Loading history...
52
                        return false;
53
                    }
54
55
                    return $provider !== $placeHolder;
56
                })
57
                ->ask();
58
59
            $providerOption = $result->fetch();
60
61
            if (strlen($providerOption) > 0 && $providerOption !== $placeHolder) {
62
                $console->makeComponentFile($menu, $file, $result->fetch());
63
            }
64
        };
65
66
        $menu = ($builder = new CliMenuBuilder)
67
            ->setWidth($builder->getTerminal()->getWidth() - 2 * 2)
68
            ->setMarginAuto()
69
            ->setPadding(2, 4)
70
            ->setTitle('Component File Generator')
71
            ->setTitleSeparator('=')
72
            ->setBackgroundColour('166')
73
            ->setForegroundColour('254')
74
            ->setExitButtonText("Leave console")
75
            ->setUnselectedMarker('❅ ')
76
            ->setSelectedMarker('> ')
77
            ->addItem('Create component file', $itemCallable)
78
            ->build();
79
80
        $menu->open();
81
    }
82
83
    /**
84
     * Check service provider file exist.
85
     *
86
     * @param string $provider
87
     *
88
     * @return bool
89
     */
90
    private function fileIsExist(string $provider): bool
91
    {
92
        return class_exists($provider) ? true : false;
93
    }
94
95
    /**
96
     * Generate component file.
97
     *
98
     * @param CliMenu $menu
99
     * @param Filesystem $file
100
     * @param string $providerOption
101
     *
102
     * @return void
103
     */
104
    private function makeComponentFile(CliMenu $menu, Filesystem $file, string $providerOption)
105
    {
106
        $provider = new ReflectionClass($providerOption);
107
108
        $manager = new Manager($provider);
109
110
        $filePath = $manager->getComponentFilePath();
111
112
        $stub = $file->get($manager->getComponentFileStub());
113
114
        $successStyle = (new MenuStyle)
115
            ->setBg('254')
116
            ->setFg('166');
117
118
        if ($file->exists($filePath)) {
119
            $result = $menu->askText($successStyle)
120
                ->setPromptText("Component file {$filePath} already exists!. Want to overwrite?")
121
                ->setPlaceholderText(' Y / N ')
122
                ->setValidationFailedText('Please choose either Y / N')
123
                ->setValidator(function ($force) {
124
                    return in_array(strtolower($force), ['y', 'n']);
125
                })->ask();
126
127
            $force = $result->fetch();
128
129
            if (strtolower($force) === strtolower('N')) {
130
                $menu->confirm('Component file are remained same!!!', $successStyle)->display('OK!');
131
            } elseif (strtolower($force) === strtolower('Y')) {
132
                $file->copy($filePath, \str_replace('component.php', date('dmyHis') . '.php', $filePath));
133
134
                $this->copyStubToLocation($file, $filePath, $stub);
135
136
                $menu->confirm('Component file successfully overwritten!!!', $successStyle)->display('OK!');
137
            }
138
        } else {
139
            $this->copyStubToLocation($file, $filePath, $stub);
140
141
            $menu->confirm('Component file successfully created!!!', $successStyle)->display('OK!');
142
        }
143
    }
144
145
    /**
146
     * Copy stubs to location.
147
     *
148
     * @param Filesystem $file
149
     * @param string $filePath
150
     * @param $stub
151
     *
152
     * @return void
153
     */
154
    private function copyStubToLocation(Filesystem $file, string $filePath, $stub)
155
    {
156
        if (!$file->isDirectory(dirname($filePath))) {
157
            $file->makeDirectory(dirname($filePath), 0655, true, true);
158
        }
159
160
        $file->put($filePath, $stub);
161
    }
162
}
163