1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Rifrocket\TraitConsole\Console\Commands; |
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
7
|
|
|
|
8
|
|
|
class Traitconsole extends GeneratorCommand |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The console command name. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $name = 'make:trait'; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Create a new trait'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The type of class being generated. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $type = 'Trait'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the stub file for the generator. |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
protected function getStub() |
38
|
|
|
{ |
39
|
|
|
if ($this->option('boot') != 'DummyMethod'){ |
40
|
|
|
return __DIR__ . '/stubs/boot-trait.stub'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if ($this->option('scope') != 'DummyMethod' ) { |
44
|
|
|
return __DIR__ . '/stubs/scope-trait.stub'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return __DIR__ . '/stubs/trait.stub'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the default namespace for the class. |
52
|
|
|
* |
53
|
|
|
* @param string $rootNamespace |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
protected function getDefaultNamespace($rootNamespace) |
57
|
|
|
{ |
58
|
|
|
return $rootNamespace . '\Traits'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Build the trait with the given name. |
63
|
|
|
* |
64
|
|
|
* @param string $name |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function buildClass($name) |
68
|
|
|
{ |
69
|
|
|
$stub = $this->files->get($this->getStub()); |
70
|
|
|
|
71
|
|
|
return $this->replaceNamespace($stub, $name)->replaceTrait($stub, $name); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Replace the trait name for the given stub. |
76
|
|
|
* |
77
|
|
|
* @param string $stub |
78
|
|
|
* @param string $name |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
protected function replaceTrait($stub, $name) |
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
if ($this->option('boot') != 'DummyMethod') { |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
$class = str_replace($this->getNamespace($name) . '\\', '', $name); |
88
|
|
|
$bootMetod='boot'.$this->option('boot'); |
89
|
|
|
$new = str_replace('DummyTrait', $class, $stub); |
90
|
|
|
return str_replace('bootDummyMethod', $bootMetod, $new); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($this->option('scope') != 'DummyMethod') { |
95
|
|
|
|
96
|
|
|
$class = str_replace($this->getNamespace($name) . '\\', '', $name); |
97
|
|
|
$scopeMetod='scope'.$this->option('scope'); |
98
|
|
|
$new =str_replace('DummyTrait', $class, $stub); |
99
|
|
|
return str_replace('scopeDummyMethod', $scopeMetod, $new); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$class = str_replace($this->getNamespace($name) . '\\', '', $name); |
103
|
|
|
return str_replace('DummyTrait', $class, $stub); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the console command options. |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
protected function getOptions() |
112
|
|
|
{ |
113
|
|
|
return [ |
114
|
|
|
['boot', 'b', InputOption::VALUE_OPTIONAL, 'generated trait should contain an eloquent boot method','DummyMethod'], |
115
|
|
|
['scope', 's', InputOption::VALUE_OPTIONAL, 'generated trait should contain an eloquent query scope','DummyMethod'] |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|