Skip to content

Commit 624f4c7

Browse files
authored
[Fix] Exceptions handling in initialization (#299)
* [Fix] Refactored baseTableExists to isEnabled, added exceptions handling for cache/db * Move isEnabled call inside the callback func to prevent calling DB and Cache in every app bootstrap * Fixed StyleCI errors
1 parent f2f3c51 commit 624f4c7

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/Providers/ConsoleServiceProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ class ConsoleServiceProvider extends ServiceProvider
1717
*/
1818
public function boot()
1919
{
20-
if (Totem::baseTableExists()) {
21-
$this->app->resolving(Schedule::class, function ($schedule) {
20+
$this->app->resolving(Schedule::class, function ($schedule) {
21+
if (Totem::isEnabled()) {
2222
$this->schedule($schedule);
23-
});
24-
}
23+
}
24+
});
2525
}
2626

2727
/**
2828
* Prepare schedule from tasks.
2929
*
30-
* @param Schedule $schedule
30+
* @param Schedule $schedule
3131
*/
3232
public function schedule(Schedule $schedule)
3333
{

src/Totem.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\Cache;
88
use Illuminate\Support\Facades\Schema;
99
use Symfony\Component\Console\Command\Command;
10+
use Throwable;
1011

1112
class Totem
1213
{
@@ -20,7 +21,7 @@ class Totem
2021
/**
2122
* Determine if the given request can access the Totem dashboard.
2223
*
23-
* @param \Illuminate\Http\Request $request
24+
* @param \Illuminate\Http\Request $request
2425
*
2526
* @return bool
2627
*/
@@ -34,7 +35,7 @@ public static function check($request)
3435
/**
3536
* Set the callback that should be used to authenticate Totem users.
3637
*
37-
* @param \Closure $callback
38+
* @param \Closure $callback
3839
*
3940
* @return static
4041
*/
@@ -91,16 +92,20 @@ public static function getCommands()
9192
/**
9293
* @return bool
9394
*/
94-
public static function baseTableExists(): bool
95+
public static function isEnabled(): bool
9596
{
96-
if (Cache::get('totem.table.'.TOTEM_TABLE_PREFIX.'tasks')) {
97-
return true;
98-
}
97+
try {
98+
if (Cache::get('totem.table.'.TOTEM_TABLE_PREFIX.'tasks')) {
99+
return true;
100+
}
99101

100-
if (Schema::hasTable(TOTEM_TABLE_PREFIX.'tasks')) {
101-
Cache::forever('totem.table.'.TOTEM_TABLE_PREFIX.'tasks', true);
102+
if (Schema::hasTable(TOTEM_TABLE_PREFIX.'tasks')) {
103+
Cache::forever('totem.table.'.TOTEM_TABLE_PREFIX.'tasks', true);
102104

103-
return true;
105+
return true;
106+
}
107+
} catch (Throwable $e) {
108+
return false;
104109
}
105110

106111
return false;

0 commit comments

Comments
 (0)