1
|
|
|
<?php |
2
|
|
|
namespace Caffeinated\Modules\Console\Handlers; |
3
|
|
|
|
4
|
|
|
use Caffeinated\Modules\Modules; |
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
View Code Duplication |
class ModuleMakeControllerHandler |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Caffeinated\Modules\Modules |
13
|
|
|
*/ |
14
|
|
|
protected $module; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
18
|
|
|
*/ |
19
|
|
|
protected $finder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Illuminate\Console\Command |
23
|
|
|
*/ |
24
|
|
|
protected $console; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string $moduleName The name of the module |
28
|
|
|
*/ |
29
|
|
|
protected $moduleName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string $className The name of the request class |
33
|
|
|
*/ |
34
|
|
|
protected $className; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor method. |
38
|
|
|
* |
39
|
|
|
* @param \Caffeinated\Modules\Modules $module |
40
|
|
|
* @param \Illuminate\Filesystem\Filesystem $finder |
41
|
|
|
*/ |
42
|
|
|
public function __construct(Modules $module, Filesystem $finder) |
43
|
|
|
{ |
44
|
|
|
$this->module = $module; |
45
|
|
|
$this->finder = $finder; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Fire off the handler. |
50
|
|
|
* |
51
|
|
|
* @param \Illuminate\Console\Command $console |
52
|
|
|
* @param string $slug |
53
|
|
|
* @param string $class |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function fire(Command $console, $slug, $class) |
57
|
|
|
{ |
58
|
|
|
$this->console = $console; |
59
|
|
|
$this->moduleName = Str::studly($slug); |
60
|
|
|
$this->className = studly_case($class); |
61
|
|
|
|
62
|
|
|
if ($this->module->exists($slug)) { |
63
|
|
|
$this->makeFile(); |
64
|
|
|
|
65
|
|
|
return $this->console->info("Created Module Controller: [$slug] ".$this->getFilename()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->console->info("Module [$slug] does not exist."); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create new migration file. |
73
|
|
|
* |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
protected function makeFile() |
77
|
|
|
{ |
78
|
|
|
return $this->finder->put($this->getDestinationFile(), $this->getStubContent()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get file destination. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getDestinationFile() |
87
|
|
|
{ |
88
|
|
|
return $this->getPath().$this->formatContent($this->getFilename()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get module migration path. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
protected function getPath() |
97
|
|
|
{ |
98
|
|
|
$path = $this->module->getModulePath($this->moduleName); |
99
|
|
|
|
100
|
|
|
return $path.'Http/Controllers/'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get migration filename. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function getFilename() |
109
|
|
|
{ |
110
|
|
|
return $this->className.'.php'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get stub content. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
protected function getStubContent() |
119
|
|
|
{ |
120
|
|
|
return $this->formatContent($this->finder->get(__DIR__.'/../Stubs/controller.stub')); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Replace placeholder text with correct values. |
125
|
|
|
* |
126
|
|
|
* @param string $content |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
protected function formatContent($content) |
130
|
|
|
{ |
131
|
|
|
return str_replace( |
132
|
|
|
['{{className}}', '{{moduleName}}', '{{namespace}}'], |
133
|
|
|
[$this->className, $this->moduleName, $this->module->getNamespace()], |
134
|
|
|
$content |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.