ImportCommand.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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::extensions();
  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. $this->publish($extension);
  43. $extension->import($this);
  44. $this->publishTag(null);
  45. $this->call('view:clear');
  46. $this->call('admin:ide-helper');
  47. $this->updateExtensionConfig($className);
  48. $this->info("Extension [$className] imported");
  49. }
  50. protected function publish(Extension $extension)
  51. {
  52. if (
  53. ($assets = $extension->assets())
  54. && file_exists($assets)
  55. ) {
  56. $this->publishItem(
  57. $assets,
  58. public_path(
  59. Admin::asset()->getRealPath('@extension').'/'.$extension::NAME
  60. )
  61. );
  62. }
  63. }
  64. protected function setServiceProvider(Extension $extension)
  65. {
  66. $this->provider = $extension->serviceProvider();
  67. $this->laravel->register($this->provider);
  68. }
  69. /**
  70. * @param $class
  71. *
  72. * @return bool
  73. */
  74. protected function updateExtensionConfig($class)
  75. {
  76. $config = (array) config('admin-extensions');
  77. $name = $class::NAME;
  78. $config[$name] = (array) ($config[$name] ?? []);
  79. $config[$name]['imported'] = true;
  80. $config[$name]['imported_at'] = date('Y-m-d H:i:s');
  81. return Helper::updateExtensionConfig($config);
  82. }
  83. }