Test Failed
Push — master ( 470d4b...33c92c )
by Terzi
04:31
created

src/Console/PanelMakeCommand.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Terranet\Administrator\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Symfony\Component\Console\Input\InputOption;
7
8
class PanelMakeCommand extends GeneratorCommand
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $name = 'administrator:panel';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Create new administrator dashboard panel.';
23
24
    /**
25
     * The type of class being generated.
26
     *
27
     * @var string
28
     */
29
    protected $type = 'Dashboard';
30
31
    public function handle()
32
    {
33
        parent::handle();
34
35
        if (!$this->option('no-view')) {
36
            $name = class_basename($this->qualifyClass($this->getNameInput()));
37
38
            $view = $this->templatePath($this->getViewName());
39
40
            $this->makeDirectory($view);
41
42
            $this->files->put($view, $this->templateContents(
43
                $name,
44
                ltrim(str_replace(base_path(), '', $view), \DIRECTORY_SEPARATOR)
45
            ));
46
        }
47
    }
48
49
    /**
50
     * Replace the class name for the given stub.
51
     *
52
     * @param string $stub
53
     * @param string $name
54
     *
55
     * @return string
56
     */
57
    protected function replaceClass($stub, $name)
58
    {
59
        $class = str_replace($this->getNamespace($name).'\\', '', $name);
60
        $view = $this->getViewName();
61
62
        $stub = str_replace('DummyClass', $class, $stub);
63
64
        return str_replace('DummyTemplate', 'admin.dashboard.'.$view, $stub);
65
    }
66
67
    /**
68
     * Get the stub file for the generator.
69
     *
70
     * @return string
71
     */
72
    protected function getStub()
73
    {
74
        if ($this->option('no-view')) {
75
            return __DIR__.'/stubs/dashboard.panel.simple.stub';
76
        }
77
78
        return __DIR__.'/stubs/dashboard.panel.stub';
79
    }
80
81
    /**
82
     * Get the default namespace for the class.
83
     *
84
     * @param string $rootNamespace
85
     *
86
     * @return string
87
     */
88
    protected function getDefaultNamespace($rootNamespace)
89
    {
90
        return $rootNamespace.'\\'.config('administrator.paths.panel');
91
    }
92
93
    /**
94
     * Get the console command options.
95
     *
96
     * @return array
97
     */
98
    protected function getOptions()
99
    {
100
        return [
101
            ['no-view', null, InputOption::VALUE_NONE, 'Do not generate view template for this panel.'],
102
        ];
103
    }
104
105
    /**
106
     * @param $name
107
     *
108
     * @return string
109
     */
110
    protected function templatePath($name)
111
    {
112
        $tpl = base_path('resources/views/admin/dashboard/'.$name.'.blade.php');
113
114
        return $tpl;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    protected function getViewName()
121
    {
122
        return snake_case(class_basename($this->qualifyClass($this->getNameInput())));
0 ignored issues
show
The function snake_case 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

122
        return /** @scrutinizer ignore-call */ snake_case(class_basename($this->qualifyClass($this->getNameInput())));
Loading history...
123
    }
124
125
    private function templateContents($title, $path)
126
    {
127
        return <<<OUT
128
<div class="panel">
129
    <div class="panel-heading">
130
        <h4 class="panel-title">{$title}</h4>
131
    </div>
132
    <div class="panel-body">
133
        <p class="well">Check me out here [{$path}]</p>
134
    </div>
135
</div>
136
OUT;
137
    }
138
}
139