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
|
16 |
|
public function fire() |
36
|
|
|
{ |
37
|
16 |
|
$path = str_replace('\\', '/', $this->getDestinationFilePath()); |
38
|
|
|
|
39
|
16 |
|
if (!$this->laravel['files']->isDirectory($dir = dirname($path))) { |
40
|
|
|
$this->laravel['files']->makeDirectory($dir, 0777, true); |
41
|
16 |
|
} |
42
|
|
|
|
43
|
16 |
|
$contents = $this->getTemplateContents(); |
44
|
|
|
|
45
|
|
|
try { |
46
|
16 |
|
with(new FileGenerator($path, $contents))->generate(); |
47
|
|
|
|
48
|
16 |
|
$this->info("Created : {$path}"); |
49
|
16 |
|
} catch (FileAlreadyExistException $e) { |
50
|
|
|
$this->error("File : {$path} already exists."); |
51
|
|
|
} |
52
|
16 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get class name. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
16 |
|
public function getClass() |
60
|
|
|
{ |
61
|
16 |
|
return class_basename($this->argument($this->argumentName)); |
|
|
|
|
62
|
4 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get default namespace. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
6 |
|
public function getDefaultNamespace() |
70
|
|
|
{ |
71
|
6 |
|
return ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get class namespace. |
76
|
|
|
* |
77
|
|
|
* @param \Nwidart\Module\Module $module |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
16 |
|
public function getClassNamespace($module) |
82
|
3 |
|
{ |
83
|
16 |
|
$extra = str_replace($this->getClass(), '', $this->argument($this->argumentName)); |
84
|
|
|
|
85
|
16 |
|
$extra = str_replace('/', '\\', $extra); |
86
|
|
|
|
87
|
16 |
|
$namespace = $this->laravel['modules']->config('namespace'); |
88
|
|
|
|
89
|
16 |
|
$namespace .= '\\'.$module->getStudlyName(); |
90
|
|
|
|
91
|
16 |
|
$namespace .= '\\'.$this->getDefaultNamespace(); |
92
|
|
|
|
93
|
16 |
|
$namespace .= '\\'.$extra; |
94
|
|
|
|
95
|
16 |
|
return rtrim($namespace, '\\'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.