1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Nwidart\Modules\Exceptions\FileAlreadyExistException; |
7
|
|
|
use Nwidart\Modules\Generators\FileGenerator; |
8
|
|
|
|
9
|
|
|
abstract class GeneratorCommand extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name of 'name' argument. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $argumentName = ''; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get template contents. |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
abstract protected function getTemplateContents(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get the destination file path. |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
abstract protected function getDestinationFilePath(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Execute the console command. |
34
|
|
|
*/ |
35
|
115 |
|
public function handle() |
36
|
|
|
{ |
37
|
115 |
|
$path = str_replace('\\', '/', $this->getDestinationFilePath()); |
38
|
|
|
|
39
|
115 |
View Code Duplication |
if (!$this->laravel['files']->isDirectory($dir = dirname($path))) { |
|
|
|
|
40
|
19 |
|
$this->laravel['files']->makeDirectory($dir, 0777, true); |
41
|
|
|
} |
42
|
|
|
|
43
|
115 |
|
$contents = $this->getTemplateContents(); |
44
|
|
|
|
45
|
|
|
try { |
46
|
115 |
|
$overwriteFile = $this->hasOption('force') ? $this->option('force') : false; |
47
|
115 |
|
(new FileGenerator($path, $contents))->withFileOverwrite($overwriteFile)->generate(); |
48
|
|
|
|
49
|
115 |
|
$this->info("Created : {$path}"); |
50
|
5 |
|
} catch (FileAlreadyExistException $e) { |
51
|
5 |
|
$this->error("File : {$path} already exists."); |
52
|
|
|
} |
53
|
115 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get class name. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
115 |
|
public function getClass() |
61
|
|
|
{ |
62
|
115 |
|
return class_basename($this->argument($this->argumentName)); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get default namespace. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
7 |
|
public function getDefaultNamespace() : string |
71
|
|
|
{ |
72
|
7 |
|
return ''; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get class namespace. |
77
|
|
|
* |
78
|
|
|
* @param \Nwidart\Modules\Module $module |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
115 |
|
public function getClassNamespace($module) |
83
|
|
|
{ |
84
|
115 |
|
$extra = str_replace($this->getClass(), '', $this->argument($this->argumentName)); |
85
|
|
|
|
86
|
115 |
|
$extra = str_replace('/', '\\', $extra); |
87
|
|
|
|
88
|
115 |
|
$namespace = $this->laravel['modules']->config('namespace'); |
89
|
|
|
|
90
|
115 |
|
$namespace .= '\\' . $module->getStudlyName(); |
91
|
|
|
|
92
|
115 |
|
$namespace .= '\\' . $this->getDefaultNamespace(); |
93
|
|
|
|
94
|
115 |
|
$namespace .= '\\' . $extra; |
95
|
|
|
|
96
|
115 |
|
$namespace = str_replace('/', '\\', $namespace); |
97
|
|
|
|
98
|
115 |
|
return trim($namespace, '\\'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.