Completed
Push — master ( b724d6...e051f1 )
by Iman
02:12
created

WidgetGenerator::createView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
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
        parent::fire();
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('View already exists!');
55
56
            return;
57
        }
58
59
        $this->files->put($path, '');
60
61
        $this->info('View created successfully.');
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
        return __DIR__ . "/../stubs/$stubName.stub";
73
    }
74
75
    /**
76
     * Get the default namespace for the class.
77
     *
78
     * @param string $rootNamespace
79
     *
80
     * @return string
81
     */
82
    protected function getDefaultNamespace($rootNamespace)
83
    {
84
        return $rootNamespace . '\\Widgets';
85
    }
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 mixed|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
        return $path;
109
    }
110
}
111