WriteFrame.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\console\Commands;
  3. use Illuminate\Console\Command;
  4. use Symfony\Component\Finder\Finder;
  5. use Symfony\Component\Finder\SplFileInfo;
  6. class WriteFrame extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'write:frame {file}';
  14. // protected $fileCount;
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Transfer the specified file to word';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $value = (string)$this->argument('file');
  38. $finder = Finder::create()->in($this->destPath($value));
  39. $fileCount = $finder->count();
  40. $bar = $this->output->createProgressBar($fileCount);
  41. if (strpos($value, '/') !== false) {
  42. $all = explode('/', $value);
  43. $name = $all[count($all) -1];
  44. } else {
  45. $name = $value;
  46. }
  47. foreach ($finder as $file) {
  48. if ($file->isFile()) {
  49. $fileContent = $file->getContents();
  50. $this->writeWord($fileContent, $name);
  51. }
  52. $bar->advance();
  53. }
  54. $bar->finish();
  55. $this->comment('Importing Word Success');
  56. }
  57. private function writeWord($content, $name)
  58. {
  59. $fileName = base_path($name) . '.docx';
  60. // if (file_exists($fileName) && $this->fileCount > 1) {
  61. // $ans = $this->ask('Do you wish to continue?');
  62. // if (in_array($ans, ['yes', 'y'])) {
  63. // file_put_contents($fileName, $content, FILE_APPEND);
  64. // } else {
  65. // exit();
  66. // }
  67. // } else {
  68. file_put_contents($fileName, $content, FILE_APPEND);
  69. // }
  70. }
  71. /**
  72. * @return string
  73. */
  74. private function destPath($name)
  75. {
  76. $path = base_path($name);
  77. if (is_dir($path)) {
  78. return $path;
  79. }
  80. $this->error('Folder does not exist!');
  81. exit();
  82. }
  83. }