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