Просмотр файла app/Http/Controllers/Admin/CheckerController.php

Размер файла: 2.28Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Http\Controllers\Admin;
  6.  
  7. use Illuminate\Http\RedirectResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\View\View;
  10. use Symfony\Component\Finder\Finder;
  11. use Symfony\Component\Finder\SplFileInfo;
  12.  
  13. class CheckerController extends AdminController
  14. {
  15. /**
  16. * Главная страница
  17. *
  18. * @return View
  19. */
  20. public function index(): View
  21. {
  22. $diff = [];
  23.  
  24. if (file_exists(storage_path('framework/cache/checker.php'))) {
  25. $files = $this->scanFiles(base_path());
  26. $filesScan = json_decode(file_get_contents(storage_path('framework/cache/checker.php')), true);
  27.  
  28. $diff['left'] = array_diff($files, $filesScan);
  29. $diff['right'] = array_diff($filesScan, $files);
  30. }
  31.  
  32. return view('admin/checkers/index', compact('diff'));
  33. }
  34.  
  35. /**
  36. * Сканирование сайта
  37. *
  38. * @param Request $request
  39. *
  40. * @return RedirectResponse
  41. */
  42. public function scan(Request $request): RedirectResponse
  43. {
  44. if ($request->input('_token') === csrf_token()) {
  45. $files = $this->scanFiles(base_path());
  46.  
  47. file_put_contents(storage_path('framework/cache/checker.php'), json_encode($files));
  48.  
  49. setFlash('success', __('admin.checkers.success_crawled'));
  50. } else {
  51. setFlash('danger', __('validator.token'));
  52. }
  53.  
  54. return redirect('admin/checkers');
  55. }
  56.  
  57. /**
  58. * Сканирует директорию сайта
  59. *
  60. * @param string $dir
  61. *
  62. * @return array
  63. */
  64. private function scanFiles(string $dir): array
  65. {
  66. $state = [];
  67. $excludeFiles = preg_filter('/^/', '*.', explode(',', setting('nocheck')));
  68.  
  69. $finder = new Finder();
  70. $files = $finder->in($dir)
  71. ->files()
  72. ->exclude(basename(storage_path()))
  73. ->notName($excludeFiles);
  74.  
  75. if (file_exists(base_path('.gitignore'))) {
  76. $files->ignoreVCSIgnored(true);
  77. }
  78.  
  79. /** @var SplFileInfo $file */
  80. foreach ($files as $file) {
  81. $state[] = $file->getRelativePathname() . ' / ' . dateFixed($file->getMTime(), 'd.m.y H:i', true) . ' / ' . formatSize($file->getSize());
  82. }
  83.  
  84. return $state;
  85. }
  86. }