1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ikechukwukalu\Makeservice\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
#[AsCommand(name: 'make:trait')] |
11
|
|
|
class MakeTraitCommand extends GeneratorCommand |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The console command name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name = 'make:trait'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The name of the console command. |
22
|
|
|
* |
23
|
|
|
* This name is used to identify the command during lazy loading. |
24
|
|
|
* |
25
|
|
|
* @var string|null |
26
|
|
|
* |
27
|
|
|
* @deprecated |
28
|
|
|
*/ |
29
|
|
|
protected static $defaultName = 'make:trait'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The console command description. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $description = 'Create a new trait class'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The type of class being generated. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $type = 'Trait'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Determine if the class already exists. |
47
|
|
|
* |
48
|
|
|
* @param string $rawName |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
protected function alreadyExists($rawName) |
52
|
|
|
{ |
53
|
|
|
return class_exists($rawName) || |
54
|
|
|
$this->files->exists($this->getPath($this->qualifyClass($rawName))); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the stub file for the generator. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
protected function getStub() |
63
|
|
|
{ |
64
|
|
|
return __DIR__.'/stubs/trait.stub'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Resolve the fully-qualified path to the stub. |
69
|
|
|
* |
70
|
|
|
* @param string $stub |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function resolveStubPath($stub) |
74
|
|
|
{ |
75
|
|
|
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) |
76
|
|
|
? $customPath |
77
|
|
|
: __DIR__.$stub; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the default namespace for the class. |
82
|
|
|
* |
83
|
|
|
* @param string $rootNamespace |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getDefaultNamespace($rootNamespace) |
87
|
|
|
{ |
88
|
|
|
return $rootNamespace.'\Traits'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the console command options. |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function getOptions() |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the trait already exists'], |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|