Completed
Push — master ( a19a47...15bbc5 )
by Arjay
15:59
created

WidgetMakeCommand::createView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\Console;
4
5
use Arrilot\Widgets\Console\WidgetMakeCommand as ArrilotWidgetMakeCommand;
6
7
class WidgetMakeCommand extends ArrilotWidgetMakeCommand
8
{
9
    /**
10
     * The console command name.
11
     *
12
     * @var string
13
     */
14
    protected $name = 'widget:make';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Create a new YajraCMS widget.';
22
23
    /**
24
     * Build the class with the given name.
25
     *
26
     * @param string $name
27
     * @return string
28
     */
29
    protected function buildClass($name)
30
    {
31
        $stub = parent::buildClass($name);
32
33
        $this->createJsonConfig($name);
34
35
        return $stub;
36
    }
37
38
    /**
39
     * Create json config file for widget details.
40
     *
41
     * @param string $name
42
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
43
     */
44
    protected function createJsonConfig($name)
45
    {
46
        if ($this->files->exists($path = $this->getJsonPath())) {
47
            $this->error('Widget json config already exists!');
48
49
            return;
50
        }
51
52
        $stub = $this->files->get($this->getJsonStubPath());
53
        $stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
54
        $stub = $this->replaceFQCN($stub, $name);
55
        $stub = $this->replaceView($stub);
56
57
        $this->files->put($path, $stub);
58
59
        $this->info('Json config created successfully.');
60
    }
61
62
    /**
63
     * Get the json stub file for the generator.
64
     *
65
     * @return string
66
     */
67
    protected function getJsonPath()
68
    {
69
        return $this->laravel->basePath() . '/app/Widgets/' . $this->argument('name') . '.json';
70
    }
71
72
    /**
73
     * Get json stub path.
74
     *
75
     * @return string
76
     */
77
    protected function getJsonStubPath()
78
    {
79
        $stubPath = $this->laravel->make('config')->get('laravel-widgets.widget_json_stub');
80
81
        return $this->laravel->basePath() . '/' . $stubPath;
82
    }
83
84
    /**
85
     * Replace class with FQCN.
86
     *
87
     * @param string $stub
88
     * @param string $name
89
     * @return string
90
     */
91
    protected function replaceFQCN($stub, $name)
92
    {
93
        $fqcn = str_replace("\\", "\\\\", $name);
94
95
        return str_replace('{{fqcn}}', $fqcn, $stub);
96
    }
97
98
    /**
99
     * Create a new view file for the widget.
100
     * return void
101
     */
102
    protected function createView()
103
    {
104
        if ($this->files->exists($path = $this->getViewPath())) {
105
            $this->error('View already exists!');
106
107
            return;
108
        }
109
110
        $this->makeDirectory($path);
111
        $view = $this->files->get($this->getViewStub('view'));
112
        $this->files->put($path, $view);
113
114
        $form = $this->files->get($this->getViewStub('form'));
115
        $this->files->put($this->getViewPath('_form'), $form);
116
117
        $this->info('Views successfully created.');
118
    }
119
120
    /**
121
     * Get the destination view path.
122
     *
123
     * @param string $append
124
     * @return string
125
     */
126
    protected function getViewPath($append = '')
127
    {
128
        return base_path('resources/views') . '/widgets/' . $this->makeViewName() . $append . '.blade.php';
129
    }
130
131
    /**
132
     * Get widget stub by key.
133
     *
134
     * @param string $key
135
     * @return string
136
     */
137
    public function getViewStub($key)
138
    {
139
        $stubPath = $this->laravel->make('config')->get('laravel-widgets.widget_' . $key . '_stub');
140
141
        return $this->laravel->basePath() . '/' . $stubPath;
142
    }
143
}
144