|
1
|
|
|
<?php |
|
2
|
|
|
namespace Caffeinated\Modules\Console\Generators; |
|
3
|
|
|
|
|
4
|
|
|
use Caffeinated\Modules\Modules; |
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
|
|
10
|
|
|
class MakeControllerCommand extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The console command name. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $name = 'make:module:controller'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description = 'Create a new module controller file'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Array to store the configuration details. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $container; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create a new command instance. |
|
35
|
|
|
* |
|
36
|
|
|
* @param Filesystem $files |
|
37
|
|
|
* @param Modules $module |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Filesystem $files, Modules $module) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct(); |
|
42
|
|
|
|
|
43
|
|
|
$this->files = $files; |
|
44
|
|
|
$this->module = $module; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Execute the console command. |
|
49
|
|
|
* |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
public function fire() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->container['slug'] = strtolower($this->argument('slug')); |
|
55
|
|
|
$this->container['className'] = $this->argument('name'); |
|
56
|
|
|
|
|
57
|
|
|
if ($this->module->exists($this->container['slug'])) { |
|
58
|
|
|
$this->container['module'] = $this->module->getProperties($this->container['slug']); |
|
59
|
|
|
|
|
60
|
|
|
$this->makeFile(); |
|
61
|
|
|
|
|
62
|
|
|
return $this->info('Module controller created successfully.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->error('Module does not exist.'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Create a new migration file. |
|
70
|
|
|
* |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function makeFile() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->files->put($this->getDestinationFile(), $this->getStubContent()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get file destination. |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getDestinationFile() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->getPath().$this->formatContent($this->getFilename()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get module migration path. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function getPath() |
|
94
|
|
|
{ |
|
95
|
|
|
$path = $this->module->getModulePath($this->container['slug']); |
|
96
|
|
|
|
|
97
|
|
|
return $path.'Http/Controllers/'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get the migration filename. |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getFilename() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->container['className'].'.php'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the stub content. |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getStubContent() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->formatContent($this->files->get(__DIR__.'/../../../resources/stubs/controller.stub')); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Replace placeholder text with correct values. |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function formatContent($content) |
|
126
|
|
|
{ |
|
127
|
|
|
return str_replace( |
|
128
|
|
|
['{{className}}', '{{namespace}}', '{{path}}'], |
|
129
|
|
|
[$this->container['className'], $this->container['module']['namespace'], $this->module->getNamespace()], |
|
130
|
|
|
$content |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the console command arguments. |
|
136
|
|
|
* |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getArguments() |
|
140
|
|
|
{ |
|
141
|
|
|
return [ |
|
142
|
|
|
['slug', InputArgument::REQUIRED, 'The slug of the module'], |
|
143
|
|
|
['name', InputArgument::REQUIRED, 'The name of the controller'] |
|
144
|
|
|
]; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|