/
PluginMake.php
254 lines (217 loc) · 7.31 KB
/
PluginMake.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* PluginMake.php
*
* PHP version 7
*
* @category Commands
* @package App\Console\Commands
* @author XE Team (developers) <developers@xpressengine.com>
* @copyright 2015 Copyright (C) NAVER <http://www.navercorp.com>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
* @link http://www.xpressengine.com
*/
namespace App\Console\Commands;
use Illuminate\Filesystem\Filesystem;
use Xpressengine\Plugin\Composer\ComposerFileWriter;
use Xpressengine\Plugin\PluginHandler;
/**
* Class PluginMake
*
* @category Commands
* @package App\Console\Commands
* @author XE Team (developers) <developers@xpressengine.com>
* @copyright 2015 Copyright (C) NAVER <http://www.navercorp.com>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
* @link http://www.xpressengine.com
*/
class PluginMake extends MakeCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:plugin
{name : The name of the plugin}
{vendor : The vendor name. like your name}
{--namespace= : The namespace of the plugin. use double backslash(\\\\) as delimiter. default "<Vendor>\\\\XePlugin\\\\\<Name>"}
{--title= : The title of plugin}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new plugin of XpressEngine';
/**
* PluginHandler instance.
*
* @var PluginHandler
*/
protected $handler;
/**
* PluginMake constructor.
*
* @param Filesystem $files Filesystem instance
* @param ComposerFileWriter $writer ComposerFileWriter instance
* @param PluginHandler $handler PluginHandler instance
*/
public function __construct(Filesystem $files, ComposerFileWriter $writer, PluginHandler $handler)
{
parent::__construct($files, $writer);
$this->handler = $handler;
}
/**
* Execute the console command.
*
* @return void
* @throws \Exception
*/
public function handle()
{
$name = $this->argument('name');
$namespace = $this->getNamespace($name, $this->argument('vendor'));
$title = $this->getTitleInput($name);
try {
$this->copyStubDirectory($path = app('path.privates').DIRECTORY_SEPARATOR.$name);
$this->makeUsable($path, $name, $namespace, $title);
$this->info('Generate the plugin');
$this->writer->reset()->cleanOperation();
$this->writer->install($packageName = 'xpressengine-plugin/'.$name, '*');
$this->writer->write();
$this->info('Run composer update command');
$this->line('> composer update');
$result = $this->composerUpdate([$packageName]);
if (0 !== $result) {
throw new \Exception('Fail to run composer update');
}
// plugin activate
$this->activatePlugin($name);
} catch (\Exception $e) {
$this->files->deleteDirectory($path);
throw $e;
}
$this->info("Plugin is created and activated successfully.");
$this->info("See ./plugins/$name directory.");
$this->info("Input and modify your plugin information in ./plugins/$name/composer.json file.");
}
/**
* Get namespace.
*
* @param string $name plugin name
* @param string $vendor vendor name
* @return string
* @throws \Exception
*/
protected function getNamespace($name, $vendor)
{
if (!$namespace = $this->option('namespace')) {
$namespace = studly_case($vendor) . '\\XePlugin\\' . studly_case($name);
}
// check namespace
if(!str_contains($namespace, '\\')) {
throw new \Exception('The namespace must have at least 1 delimiter(\\), use double backslash(\\\\) as delimiter');
}
return $namespace;
}
/**
* Get title.
*
* @param string $name given name.
* @return string
*/
protected function getTitleInput($name)
{
return $this->option('title') ?: studly_case($name) . ' plugin';
}
/**
* Get stub path.
*
* @return string
*/
protected function getStubPath()
{
return __DIR__ . '/stubs/plugin';
}
/**
* Make file for plugin by stub.
*
* @param string $path path for plugin
* @param string $name name
* @param string $namespace namespace
* @param string $title plugin title
* @return void
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function makeUsable($path, $name, $namespace, $title)
{
// plugin.php 파일 생성
$this->makePluginClass($path, $name, $namespace);
// composer.json 파일 생성
$this->makeComposerJson($path, $name, $namespace, $title);
// Controller.php
$this->makeControllerClass($path, $name, $namespace, $title);
$this->files->move($path . '/views/index.blade.stub', $path . '/views/index.blade.php');
}
/**
* Make plugin class.
*
* @param string $path path for plugin
* @param string $name name
* @param string $namespace namespace
* @return void
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function makePluginClass($path, $name, $namespace)
{
$search = ['DummyNamespace', 'DummyPluginName'];
$replace = [$namespace, $name];
$this->buildFile($path.'/plugin.stub', $search, $replace, $path.'/plugin.php');
}
/**
* Make composer file.
*
* @param string $path path for plugin
* @param string $name name
* @param string $namespace namespace
* @param string $title plugin title
* @return void
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function makeComposerJson($path, $name, $namespace, $title)
{
$namespace = str_replace('\\', '\\\\', $namespace);
$search = ['DummyNamespace', 'DummyPluginName', 'DummyPluginTitle', 'DummyCoreVer'];
$replace = [$namespace, $name, $title, '~'.__XE_VERSION__];
$this->buildFile($path.'/composer.json.stub', $search, $replace, $path.'/composer.json');
}
/**
* Make controller class.
*
* @param string $path path for plugin
* @param string $name name
* @param string $namespace namespace
* @param string $title plugin title
* @return void
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function makeControllerClass($path, $name, $namespace, $title)
{
$search = ['DummyNamespace', 'DummyPluginName', 'DummyPluginTitle'];
$replace = [$namespace, $name, $title];
$this->buildFile($path.'/src/Controller.stub', $search, $replace, $path.'/src/Controller.php');
}
/**
* Activate plugin.
*
* @param string $name plugin name
* @return void
*/
protected function activatePlugin($name)
{
$this->handler->getAllPlugins(true);
if (!$this->handler->isActivated($name)) {
$this->handler->activatePlugin($name);
}
}
}