]> BookStack Code Mirror - bookstack/blob - app/Settings/StatusController.php
Vectors: Added command to regenerate for all
[bookstack] / app / Settings / StatusController.php
1 <?php
2
3 namespace BookStack\Settings;
4
5 use BookStack\Http\Controller;
6 use Illuminate\Support\Facades\Cache;
7 use Illuminate\Support\Facades\DB;
8 use Illuminate\Support\Facades\Session;
9 use Illuminate\Support\Str;
10
11 class StatusController extends Controller
12 {
13     /**
14      * Show the system status as a simple json page.
15      */
16     public function show()
17     {
18         $statuses = [
19             'database' => $this->trueWithoutError(function () {
20                 return DB::table('migrations')->count() > 0;
21             }),
22             'cache' => $this->trueWithoutError(function () {
23                 $rand = Str::random(12);
24                 $key = "status_test_{$rand}";
25                 Cache::add($key, $rand);
26
27                 return Cache::pull($key) === $rand;
28             }),
29             'session' => $this->trueWithoutError(function () {
30                 $rand = Str::random();
31                 Session::put('status_test', $rand);
32
33                 return Session::get('status_test') === $rand;
34             }),
35         ];
36
37         $hasError = in_array(false, $statuses);
38
39         return response()->json($statuses, $hasError ? 500 : 200);
40     }
41
42     /**
43      * Check the callable passed returns true and does not throw an exception.
44      */
45     protected function trueWithoutError(callable $test): bool
46     {
47         try {
48             return $test() === true;
49         } catch (\Exception $e) {
50             return false;
51         }
52     }
53 }