1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains the MakeModule Command class. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016 Attila Fulop |
6
|
|
|
* @author Attila Fulop |
7
|
|
|
* @license MIT |
8
|
|
|
* @since 2017-01-18 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Konekt\Concord\Console\Commands; |
13
|
|
|
|
14
|
|
|
use Illuminate\Console\GeneratorCommand; |
15
|
|
|
use Illuminate\Filesystem\Filesystem; |
16
|
|
|
use Konekt\Concord\Contracts\Concord; |
17
|
|
|
use Konekt\Concord\Contracts\Convention; |
18
|
|
|
use Konekt\Concord\Exceptions\UnknownLaravelVersionException; |
19
|
|
|
|
20
|
|
|
class MakeModuleCommand extends GeneratorCommand |
21
|
|
|
{ |
22
|
|
|
/** @var string */ |
23
|
|
|
protected $name = 'make:module'; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $description = 'Create a new Concord module'; |
27
|
|
|
|
28
|
|
|
/** @var Convention */ |
29
|
|
|
protected $convention; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The type of class being generated. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $type = 'Module'; |
37
|
|
|
|
38
|
|
|
public function __construct(Filesystem $files, Concord $concord) |
39
|
|
|
{ |
40
|
|
|
parent::__construct($files); |
41
|
|
|
|
42
|
|
|
$this->convention = $concord->getConvention(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Execute the console command. |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function handle() |
51
|
|
|
{ |
52
|
|
|
if (parent::handle() === false) { |
|
|
|
|
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$providerFileName = $this->getPath($this->qualifyClass($this->getNameInput())); |
57
|
|
|
$this->files->put(//Replace the |
58
|
|
|
$providerFileName, |
59
|
|
|
str_replace( |
60
|
|
|
'DummyProviderFolder', |
61
|
|
|
str_replace('/', '\\', $this->convention->providersFolder()), |
62
|
|
|
$this->files->get($providerFileName) |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$name = $this->getNameInput(); |
67
|
|
|
$manifestPath = str_replace( |
68
|
|
|
sprintf('%s/ModuleServiceProvider.php', $this->convention->providersFolder()), |
69
|
|
|
$this->convention->manifestFile(), |
70
|
|
|
$this->getPath($this->getFQCNComp($name) |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
if (!$this->files->exists($manifestPath)) { |
75
|
|
|
$this->makeDirectory($manifestPath); |
76
|
|
|
|
77
|
|
|
$this->files->put($manifestPath, $this->buildManifest($name)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the stub file for the generator. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getStub() |
87
|
|
|
{ |
88
|
|
|
return __DIR__ . '/stubs/module_provider.stub'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the default namespace for the class. |
93
|
|
|
* |
94
|
|
|
* @param string $rootNamespace |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
protected function getDefaultNamespace($rootNamespace) |
98
|
|
|
{ |
99
|
|
|
return $rootNamespace . '\\' . str_replace('/', '\\', $this->convention->modulesFolder()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the destination class path. |
104
|
|
|
* |
105
|
|
|
* @param string $name |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function getPath($name) |
109
|
|
|
{ |
110
|
|
|
$name = str_replace_first($this->laravel->getNamespace(), '', $name); |
111
|
|
|
|
112
|
|
|
return sprintf('%s/%s/%s/%s', |
113
|
|
|
$this->laravel['path'], |
114
|
|
|
str_replace('\\', '/', $name), |
115
|
|
|
$this->convention->providersFolder(), |
116
|
|
|
'ModuleServiceProvider.php' |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function buildManifest($name) |
121
|
|
|
{ |
122
|
|
|
$stub = $this->files->get(__DIR__ . '/stubs/module_manifest.stub'); |
123
|
|
|
|
124
|
|
|
return str_replace('DummyName', $name, $stub); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the fully qualified class name from the base generator command in a Laravel 5.3/5.4 |
129
|
|
|
* compatible manner |
130
|
|
|
* |
131
|
|
|
* @param string $name |
132
|
|
|
* |
133
|
|
|
* @throws UnknownLaravelVersionException |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
protected function getFQCNComp($name) |
138
|
|
|
{ |
139
|
|
|
if (method_exists($this, 'qualifyClass')) { |
140
|
|
|
return $this->qualifyClass($name); |
141
|
|
|
} elseif (method_exists($this, 'parseName')) { |
142
|
|
|
return $this->parseName($name); |
|
|
|
|
143
|
|
|
} else { |
144
|
|
|
throw new UnknownLaravelVersionException( |
145
|
|
|
sprintf( |
146
|
|
|
"There's an incompatible parent class `%s` in your installed version of Laravel", |
147
|
|
|
get_parent_class($this) |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|