]> BookStack Code Mirror - bookstack/blob - app/Settings/StatusController.php
9559e344ce20ded960212d74d588a0c972e41a59
[bookstack] / app / Settings / StatusController.php
1 <?php
2
3 namespace BookStack\Settings;
4
5 use BookStack\Http\Controllers\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();
24                 Cache::add('status_test', $rand);
25
26                 return Cache::pull('status_test') === $rand;
27             }),
28             'session' => $this->trueWithoutError(function () {
29                 $rand = Str::random();
30                 Session::put('status_test', $rand);
31
32                 return Session::get('status_test') === $rand;
33             }),
34         ];
35
36         $hasError = in_array(false, $statuses);
37
38         return response()->json($statuses, $hasError ? 500 : 200);
39     }
40
41     /**
42      * Check the callable passed returns true and does not throw an exception.
43      */
44     protected function trueWithoutError(callable $test): bool
45     {
46         try {
47             return $test() === true;
48         } catch (\Exception $e) {
49             return false;
50         }
51     }
52 }