Completed
Push — master ( f49b06...6d18b2 )
by Iman
01:39
created

WidgetGenerator::getViewPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Imanghafoori\Widgets;
4
5
use Illuminate\Console\GeneratorCommand as LaravelGeneratorCommand;
6
7
class WidgetGenerator extends LaravelGeneratorCommand
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'make:widget	{name : The name of the widget class} {--p|plain}';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Create a new widget class';
22
23
    /**
24
     * String to store the command type.
25
     *
26
     * @var string
27
     */
28
    protected $type = 'Widget Class';
29
30
    /**
31
     * Execute the console command.
32
     *
33
     * @return void
34
     */
35
    public function fire()
36
    {
37
        $this->makeWidgetClass();
38
39
        if (! $this->option('plain')) {
40
            $this->createView();
41
        }
42
    }
43
44
    /**
45
     * Create a new view file for the widget.
46
     *
47
     * return void
48
     */
49
    private function createView()
50
    {
51
        $path = $this->getViewPath();
52
53
        if ($this->files->exists($path)) {
54
            $this->error($this->qualifyClass($this->getNameInput()).'View.blade.php - Already exists! (@_@)');
55
56
            return;
57
        }
58
59
        $this->files->put($path, $this->getViewStub());
60
61
        $this->info(' - '.$this->qualifyClass($this->getNameInput()).'View.blade.php - was created. (^_^)');
62
    }
63
64
    /**
65
     * Get the stub file for the generator.
66
     *
67
     * @return string
68
     */
69
    protected function getStub()
70
    {
71
        $stubName = $this->option('plain') ? 'widget_plain' : 'widget';
72
73
        return __DIR__."/../stubs/$stubName.stub";
74
    }
75
76
    /**
77
     * Get the default namespace for the class.
78
     *
79
     * @param string $rootNamespace
80
     *
81
     * @return string
82
     */
83
    protected function getDefaultNamespace($rootNamespace)
84
    {
85
        return $rootNamespace.'\\Widgets';
86
    }
87
88
    /**
89
     * Get the console command options.
90
     *
91
     * @return array
92
     */
93
    protected function getOptions()
94
    {
95
        return [
96
            ['plain', null, InputOption::VALUE_NONE, 'No docs on widget class. No view is being created too.'],
97
        ];
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    private function getViewPath()
104
    {
105
        $name = $this->qualifyClass($this->getNameInput());
106
        $path = $this->getPath($name);
107
        $path = str_replace('.php', 'View.blade.php', $path);
108
109
        return $path;
110
    }
111
112
    /**
113
     * Creates the widget class.
114
     * @return bool
115
     */
116
    private function makeWidgetClass()
117
    {
118
        $name = $this->qualifyClass($this->getNameInput());
119
120
        $path = $this->getPath($name);
121
122
        // First we will check to see if the class already exists. If it does, we don't want
123
        // to create the class and overwrite the user's code. So, we will bail out so the
124
        // code is untouched. Otherwise, we will continue generating this class' files.
125
        if ($this->alreadyExists($this->getNameInput())) {
126
            $this->error($this->qualifyClass($this->getNameInput()).'.php - Already exists (@_@)');
127
128
            return false;
129
        }
130
131
        // Next, we will generate the path to the location where this class' file should get
132
        // written. Then, we will build the class and make the proper replacements on the
133
        // stub files so that it gets the correctly formatted namespace and class name.
134
        $this->makeDirectory($path);
135
136
        $this->files->put($path, $this->buildClass($name));
137
138
        $this->info(' - '.$name.'.php - was created.  (^_^)');
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    private function getViewStub()
145
    {
146
        return 'Note that you can reference partials within "App\Widgets" folder like this: @include("Widgets::somePartial") ';
147
    }
148
}
149