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

Размер файла: 2.21Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers\Admin;
  6.  
  7. use App\Commands\CacheClear;
  8. use App\Commands\ConfigClear;
  9. use App\Commands\ImageClear;
  10. use App\Commands\RouteClear;
  11. use App\Commands\ViewClear;
  12. use App\Models\User;
  13. use Illuminate\Http\Request;
  14.  
  15. class CacheController extends AdminController
  16. {
  17. /**
  18. * Конструктор
  19. */
  20. public function __construct()
  21. {
  22. parent::__construct();
  23.  
  24. if (! isAdmin(User::BOSS)) {
  25. abort(403, __('errors.forbidden'));
  26. }
  27. }
  28.  
  29. /**
  30. * Главная страница
  31. *
  32. * @param Request $request
  33. *
  34. * @return string
  35. */
  36. public function index(Request $request): string
  37. {
  38. $type = $request->input('type', 'files');
  39.  
  40. if ($type === 'images') {
  41. $files = glob(UPLOADS . '/thumbnails/*.{gif,png,jpg,jpeg}', GLOB_BRACE);
  42. $files = paginate($files, 20, compact('type'));
  43. } elseif ($type === 'views') {
  44. $files = glob(STORAGE . '/views/*.php', GLOB_BRACE);
  45. $files = paginate($files, 20, compact('type'));
  46. } else {
  47. $files = glob(STORAGE . '/caches/{*/*/*,*.php}', GLOB_BRACE);
  48. $files = paginate($files, 20, compact('type'));
  49. }
  50.  
  51. return view('admin/caches/index', compact('files', 'type'));
  52. }
  53.  
  54. /**
  55. * Очистка кеша
  56. *
  57. * @param Request $request
  58. *
  59. * @return void
  60. */
  61. public function clear(Request $request): void
  62. {
  63. $type = $request->input('type');
  64.  
  65. if ($request->input('token') === $_SESSION['token']) {
  66. switch ($type) {
  67. case 'images':
  68. runCommand(new ImageClear());
  69. break;
  70. case 'views':
  71. runCommand(new ViewClear());
  72. break;
  73. default:
  74. runCommand(new ConfigClear());
  75. runCommand(new RouteClear());
  76. runCommand(new CacheClear());
  77. }
  78.  
  79. setFlash('success', __('admin.caches.success_cleared'));
  80. } else {
  81. setFlash('danger', __('validator.token'));
  82. }
  83.  
  84. redirect('/admin/caches?type=' . $type);
  85. }
  86. }