ImportCommand.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Dcat\Admin\Console;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Extension;
  5. use Dcat\Admin\Support\Helper;
  6. use Illuminate\Foundation\Console\VendorPublishCommand;
  7. use Illuminate\Support\Arr;
  8. class ImportCommand extends VendorPublishCommand
  9. {
  10. /**
  11. * The console command name.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'admin:import {extension?} {--force : Overwrite any existing files}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Import a dcat-admin extension';
  22. /**
  23. * Execute the console command.
  24. *
  25. * @return void
  26. */
  27. public function handle()
  28. {
  29. $extension = $this->argument('extension');
  30. $extensions = Admin::getExtensions();
  31. if (empty($extension) || ! Arr::has($extensions, $extension)) {
  32. $extension = $this->choice('Please choose a extension to import', array_keys($extensions));
  33. }
  34. $className = Arr::get($extensions, $extension);
  35. if (! class_exists($className) || ! is_subclass_of($className, Extension::class) || ! $className::make()->getName()) {
  36. $this->error("Invalid Extension [$className]");
  37. return;
  38. }
  39. /* @var Extension $extension */
  40. $extension = $className::make();
  41. $this->setServiceProvider($extension);
  42. $extension->import($this);
  43. $extensionName = $extension->getName();
  44. if ($assets = $extension->assets()) {
  45. $this->publishItem($assets, public_path('vendor/dcat-admin-extensions/'.$extensionName));
  46. }
  47. $this->publishTag(null);
  48. $this->call('view:clear');
  49. $this->call('admin:ide-helper');
  50. $this->updateExtensionConfig($className);
  51. $this->info("Extension [$className] imported");
  52. }
  53. protected function setServiceProvider(Extension $extension)
  54. {
  55. $this->provider = $extension->serviceProvider();
  56. $this->laravel->register($this->provider);
  57. }
  58. /**
  59. * @param $class
  60. *
  61. * @return bool
  62. */
  63. protected function updateExtensionConfig($class)
  64. {
  65. $config = (array) config('admin-extensions');
  66. $name = $class::NAME;
  67. $config[$name] = (array) ($config[$name] ?? []);
  68. $config[$name]['imported'] = true;
  69. $config[$name]['imported_at'] = date('Y-m-d H:i:s');
  70. return Helper::updateExtensionConfig($config);
  71. }
  72. }