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