Skip to content

Commit 32e3299

Browse files
author
Bradie Tilley
committed
Fix - Update plugin's boot method to not directly reference Settings until DB connection is established
1 parent afb8008 commit 32e3299

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

Plugin.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
namespace ABWebDevelopers\ImageResize;
44

5-
use System\Classes\PluginBase;
65
use ABWebDevelopers\ImageResize\Classes\Resizer;
76
use ABWebDevelopers\ImageResize\Commands\ImageResizeClear;
87
use ABWebDevelopers\ImageResize\Commands\ImageResizeGc;
98
use ABWebDevelopers\ImageResize\Models\Settings;
109
use ABWebDevelopers\ImageResize\ReportWidgets\ImageResizeClearWidget;
10+
use App;
11+
use Artisan;
12+
use DB;
1113
use Event;
12-
use Illuminate\Support\Facades\Artisan;
14+
use Illuminate\Database\QueryException;
15+
use Symfony\Component\Console\Output\ConsoleOutput;
16+
use System\Classes\PluginBase;
1317

1418
class Plugin extends PluginBase
1519
{
@@ -92,11 +96,22 @@ public function boot()
9296
}
9397
});
9498

95-
if (Settings::cleanupOnCacheClear()) {
96-
Event::listen('cache:cleared', function () {
97-
Artisan::call('imageresize:clear');
99+
Event::listen('cache:cleared', function () {
100+
$this->ifDatabaseExists(function () {
101+
if (Settings::cleanupOnCacheClear()) {
102+
if (App::runningInConsole()) {
103+
$output = new ConsoleOutput();
104+
$output->writeln('<info>Imagesizer: Deleting cached resized images...</info>');
105+
}
106+
107+
Artisan::call('imageresize:clear');
108+
109+
if (App::runningInConsole()) {
110+
$output->writeln('<info>Imagesizer: ' . Artisan::output() . '</info>');
111+
}
112+
}
98113
});
99-
}
114+
});
100115
}
101116

102117
/**
@@ -117,6 +132,9 @@ public function registerSchedule($schedule)
117132
$schedule->command('imageresize:gc')->everyFiveMinutes();
118133
}
119134

135+
/**
136+
* @inheritDoc
137+
*/
120138
public function registerReportWidgets()
121139
{
122140
return [
@@ -126,4 +144,24 @@ public function registerReportWidgets()
126144
],
127145
];
128146
}
147+
148+
/**
149+
* Run the callback only if/when the database exists (and system_settings table exists).
150+
*
151+
* @param \Closure $callback
152+
* @return mixed
153+
*/
154+
public function ifDatabaseExists(\Closure $callback)
155+
{
156+
$canConnectToDatabase = false;
157+
try {
158+
// Test database connection (throw exception if no DB is configured yet)
159+
$canConnectToDatabase = DB::table('system_settings')->exists();
160+
} catch (QueryException $e) {
161+
}
162+
163+
if ($canConnectToDatabase) {
164+
return $callback();
165+
}
166+
}
129167
}

updates/version.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@
3636
2.1.5:
3737
- Add new setting that runs `imageresize:clear` when `cache:clear` is run (if configured), and fix a Settings bug that was introduced from v2.1.0
3838
2.1.6:
39-
- Fix migration (2.1.2 migration) that causes installations to fail (due to plugin settings not existing for new projects at time of execution)
39+
- Fix migration (2.1.2 migration) that causes installations to fail (due to plugin settings not existing for new projects at time of execution)
40+
2.1.7:
41+
- Fix - Update plugin's boot method to not directly reference Settings until DB connection is established

0 commit comments

Comments
 (0)