LinkCommand.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Dcat\Admin\Console\Development;
  3. use Dcat\Admin\Admin;
  4. use Illuminate\Filesystem\Filesystem;
  5. use Illuminate\Console\Command;
  6. class LinkCommand extends Command
  7. {
  8. /**
  9. * The console command signature.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'admin:dev:link';
  14. /**
  15. * Execute the console command.
  16. *
  17. * @return void
  18. */
  19. public function handle()
  20. {
  21. $files = $this->laravel->make('files');
  22. $this->linkAssets($files);
  23. $this->linkTests($files);
  24. }
  25. /**
  26. * @param Filesystem $files
  27. *
  28. * @return void
  29. */
  30. protected function linkTests($files)
  31. {
  32. $target = base_path('tests');
  33. $testsPath = realpath(__DIR__.'/../../../tests');
  34. if (is_dir($testsPath)) {
  35. $result = $this->ask("The [{$testsPath}] directory already exists, are you sure to delete it? [yes/no]");
  36. if (strtolower($result) !== 'yes') {
  37. return;
  38. }
  39. $files->deleteDirectory($testsPath);
  40. }
  41. $files->link(
  42. $testsPath, $target
  43. );
  44. $this->info("The [$testsPath] directory has been linked.");
  45. }
  46. /**
  47. * @param Filesystem $files
  48. *
  49. * @return void
  50. */
  51. protected function linkAssets($files)
  52. {
  53. $basePath = Admin::asset()->getRealPath('@admin');
  54. $publicPath = public_path($basePath);
  55. if (! is_dir($publicPath.'/..')) {
  56. $files->makeDirectory($publicPath.'/..', 0755, true, true);
  57. }
  58. if (file_exists($publicPath)) {
  59. $this->warn("The [public/{$basePath}] directory already exists.");
  60. return;
  61. }
  62. $distPath = realpath(__DIR__ . '/../../../resources/dist');
  63. $files->link(
  64. $distPath, $publicPath
  65. );
  66. $this->info("The [$basePath] directory has been linked.");
  67. }
  68. }