22
33namespace ABWebDevelopers \ImageResize ;
44
5- use System \Classes \PluginBase ;
65use ABWebDevelopers \ImageResize \Classes \Resizer ;
76use ABWebDevelopers \ImageResize \Commands \ImageResizeClear ;
87use ABWebDevelopers \ImageResize \Commands \ImageResizeGc ;
98use ABWebDevelopers \ImageResize \Models \Settings ;
109use ABWebDevelopers \ImageResize \ReportWidgets \ImageResizeClearWidget ;
10+ use App ;
11+ use Artisan ;
12+ use DB ;
1113use 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
1418class 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}
0 commit comments