ExtendCommand.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace Dcat\Admin\Console;
  3. use Dcat\Admin\Support\Helper;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Filesystem\Filesystem;
  6. use Illuminate\Support\Str;
  7. class ExtendCommand extends Command
  8. {
  9. /**
  10. * The console command name.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'admin:extend {extension} {--namespace=}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Build a dcat-admin extension';
  21. /**
  22. * @var string
  23. */
  24. protected $basePath = '';
  25. /**
  26. * @var Filesystem
  27. */
  28. protected $filesystem;
  29. /**
  30. * @var string
  31. */
  32. protected $namespace;
  33. /**
  34. * @var string
  35. */
  36. protected $className;
  37. /**
  38. * @var string
  39. */
  40. protected $package;
  41. /**
  42. * @var string
  43. */
  44. protected $extensionDir;
  45. /**
  46. * @var array
  47. */
  48. protected $dirs = [
  49. 'database/migrations',
  50. 'database/seeds',
  51. 'resources/assets',
  52. 'resources/views',
  53. 'src/Http/Controllers',
  54. 'routes',
  55. ];
  56. /**
  57. * Execute the console command.
  58. *
  59. * @return void
  60. */
  61. public function handle(Filesystem $filesystem)
  62. {
  63. $this->filesystem = $filesystem;
  64. $this->extensionDir = config('admin.extension_dir') ?: app_path('Admin/Extensions');
  65. if (! file_exists($this->extensionDir)) {
  66. $this->makeDir();
  67. }
  68. $this->package = $this->argument('extension');
  69. InputExtensionName :
  70. if (! Helper::validateExtensionName($this->package)) {
  71. $this->package = $this->ask("[$this->package] is not a valid package name, please input a name like (<vendor>/<name>)");
  72. goto InputExtensionName;
  73. }
  74. $this->makeDirs();
  75. $this->makeFiles();
  76. $this->info("The extension scaffolding generated successfully. \r\n");
  77. $this->showTree();
  78. }
  79. /**
  80. * Show extension scaffolding with tree structure.
  81. */
  82. protected function showTree()
  83. {
  84. $tree = <<<TREE
  85. {$this->extensionPath()}
  86. ├── LICENSE
  87. ├── README.md
  88. ├── composer.json
  89. ├── database
  90. │   ├── migrations
  91. │   └── seeds
  92. ├── resources
  93. │   ├── assets
  94. │   └── views
  95. │   └── index.blade.php
  96. ├── routes
  97. │   └── web.php
  98. └── src
  99. ├── {$this->className}.php
  100. ├── bootstrap.php
  101. └── Http
  102. └── Controllers
  103. └── {$this->className}Controller.php
  104. TREE;
  105. $this->info($tree);
  106. }
  107. /**
  108. * Make extension files.
  109. */
  110. protected function makeFiles()
  111. {
  112. $this->namespace = $this->getRootNameSpace();
  113. $this->className = $this->getClassName();
  114. // copy files
  115. $this->copy([
  116. __DIR__.'/stubs/extension/view.stub' => 'resources/views/index.blade.php',
  117. __DIR__.'/stubs/extension/.gitignore.stub' => '.gitignore',
  118. __DIR__.'/stubs/extension/README.md.stub' => 'README.md',
  119. __DIR__.'/stubs/extension/LICENSE.stub' => 'LICENSE',
  120. ]);
  121. // make composer.json
  122. $composerContents = str_replace(
  123. [':package', ':namespace', ':class_name'],
  124. [$this->package, str_replace('\\', '\\\\', $this->namespace).'\\\\', $this->className],
  125. file_get_contents(__DIR__.'/stubs/extension/composer.json.stub')
  126. );
  127. $this->putFile('composer.json', $composerContents);
  128. $basePackage = Helper::slug(basename($this->package));
  129. // make class
  130. $classContents = str_replace(
  131. [':namespace', ':class_name', ':title', ':path', ':base_package'],
  132. [$this->namespace, $this->className, Str::title($this->className), $basePackage, $basePackage],
  133. file_get_contents(__DIR__.'/stubs/extension/extension.stub')
  134. );
  135. $this->putFile("src/{$this->className}.php", $classContents);
  136. // make bootstrap
  137. $bootstrap = str_replace(
  138. [':namespace', ':class_name'],
  139. [$this->namespace, $this->className],
  140. file_get_contents(__DIR__.'/stubs/extension/bootstrap.stub')
  141. );
  142. $this->putFile('src/bootstrap.php', $bootstrap);
  143. // make service provider
  144. $providerContents = str_replace(
  145. [':namespace', ':class_name', ':base_package', ':package'],
  146. [$this->namespace, $this->className, $basePackage, $this->package],
  147. file_get_contents(__DIR__.'/stubs/extension/service-provider.stub')
  148. );
  149. $this->putFile("src/{$this->className}ServiceProvider.php", $providerContents);
  150. // make controller
  151. $controllerContent = str_replace(
  152. [':namespace', ':class_name', ':base_package'],
  153. [$this->namespace, $this->className, $basePackage],
  154. file_get_contents(__DIR__.'/stubs/extension/controller.stub')
  155. );
  156. $this->putFile("src/Http/Controllers/{$this->className}Controller.php", $controllerContent);
  157. // make routes
  158. $routesContent = str_replace(
  159. [':namespace', ':class_name', ':path'],
  160. [$this->namespace, $this->className, $basePackage],
  161. file_get_contents(__DIR__.'/stubs/extension/routes.stub')
  162. );
  163. $this->putFile('routes/web.php', $routesContent);
  164. }
  165. /**
  166. * Get root namespace for this package.
  167. *
  168. * @return array|null|string
  169. */
  170. protected function getRootNameSpace()
  171. {
  172. if (! $namespace = $this->option('namespace')) {
  173. [$vendor, $name] = explode('/', $this->package);
  174. $default = str_replace(['-', '-'], '', Str::title($vendor).'\\'.Str::title($name));
  175. $namespace = $this->ask('Root namespace', $default);
  176. }
  177. return $namespace;
  178. }
  179. /**
  180. * Get extension class name.
  181. *
  182. * @return string
  183. */
  184. protected function getClassName()
  185. {
  186. return ucfirst(Str::camel(basename($this->package)));
  187. }
  188. /**
  189. * Create package dirs.
  190. */
  191. protected function makeDirs()
  192. {
  193. $this->basePath = rtrim($this->extensionDir, '/').'/'.ltrim($this->package, '/');
  194. $this->makeDir($this->dirs);
  195. }
  196. /**
  197. * Extension path.
  198. *
  199. * @param string $path
  200. *
  201. * @return string
  202. */
  203. protected function extensionPath($path = '')
  204. {
  205. $path = rtrim($path, '/');
  206. if (empty($path)) {
  207. return rtrim($this->basePath, '/');
  208. }
  209. return rtrim($this->basePath, '/').'/'.ltrim($path, '/');
  210. }
  211. /**
  212. * Put contents to file.
  213. *
  214. * @param string $to
  215. * @param string $content
  216. */
  217. protected function putFile($to, $content)
  218. {
  219. $to = $this->extensionPath($to);
  220. $this->filesystem->put($to, $content);
  221. }
  222. /**
  223. * Copy files to extension path.
  224. *
  225. * @param string|array $from
  226. * @param string|null $to
  227. */
  228. protected function copy($from, $to = null)
  229. {
  230. if (is_array($from) && is_null($to)) {
  231. foreach ($from as $key => $value) {
  232. $this->copy($key, $value);
  233. }
  234. return;
  235. }
  236. if (! file_exists($from)) {
  237. return;
  238. }
  239. $to = $this->extensionPath($to);
  240. $this->filesystem->copy($from, $to);
  241. }
  242. /**
  243. * Make new directory.
  244. *
  245. * @param array|string $paths
  246. */
  247. protected function makeDir($paths = '')
  248. {
  249. foreach ((array) $paths as $path) {
  250. $path = $this->extensionPath($path);
  251. $this->filesystem->makeDirectory($path, 0755, true, true);
  252. }
  253. }
  254. }