33namespace Rias \StatamicRedirect ;
44
55use Illuminate \Routing \Router ;
6+ use Illuminate \Support \Facades \Config ;
67use Illuminate \Support \Facades \DB ;
78use Illuminate \Support \Facades \File ;
89use Illuminate \Support \Facades \Schema ;
@@ -80,13 +81,11 @@ public function register()
8081 $ this ->registerAddonConfig ();
8182
8283 $ this ->app ->singleton (RedirectRepository::class, function () {
83- $ class = $ this ->getRedirectRepository ();
84-
85- return new $ class ($ this ->app ['stache ' ]);
84+ return $ this ->app ->get ($ this ->getRedirectRepository ());
8685 });
8786 }
8887
89- public function boot ()
88+ public function boot (): void
9089 {
9190 parent ::boot ();
9291
@@ -108,11 +107,14 @@ public function boot()
108107 Git::listen (RedirectSaved::class);
109108 }
110109
110+ File::ensureDirectoryExists (storage_path ('redirect ' ));
111+
112+ $ this ->loadViewsFrom (__DIR__ . '/../resources/views ' , 'redirect ' );
113+
111114 $ this
112- ->bootAddonViews ()
113115 ->bootAddonNav ()
114- ->bootStores ()
115- ->bootDatabase ()
116+ ->bootErrors ()
117+ ->bootRedirects ()
116118 ->bootPermissions ();
117119 });
118120
@@ -145,14 +147,7 @@ protected function getRedirectRepository()
145147 return StacheRedirectRepository::class;
146148 }
147149
148- protected function bootAddonViews ()
149- {
150- $ this ->loadViewsFrom (__DIR__ . '/../resources/views ' , 'redirect ' );
151-
152- return $ this ;
153- }
154-
155- protected function bootAddonNav ()
150+ protected function bootAddonNav (): self
156151 {
157152 Nav::extend (function ($ nav ) {
158153 $ items = [];
@@ -178,97 +173,58 @@ protected function bootAddonNav()
178173 return $ this ;
179174 }
180175
181- protected function bootStores ()
182- {
183- $ redirectStore = new RedirectStore ();
184- $ redirectStore ->directory (config ('statamic.redirect.redirect_store ' , base_path ('content/redirects ' )));
185- app (Stache::class)->registerStore ($ redirectStore );
186-
187- return $ this ;
188- }
189-
190- protected function bootDatabase ()
176+ protected function bootErrors (): self
191177 {
192178 if (! config ('statamic.redirect.log_errors ' )) {
193179 return $ this ;
194180 }
195181
196- File::ensureDirectoryExists (storage_path ('redirect ' ));
197-
198- $ sqlitePath = storage_path ('redirect/redirect.sqlite ' );
199- $ this ->ensureDatabaseExists ($ sqlitePath );
200-
201- app ('config ' )->set ('database.connections.redirect-sqlite ' , [
202- 'driver ' => 'sqlite ' ,
203- 'database ' => $ sqlitePath ,
204- ]);
205-
206- $ this ->bootDatabaseForErrors ();
207- $ this ->bootDatabaseForRedirects ();
208-
209- return $ this ;
210- }
211-
212- protected function ensureDatabaseExists ($ sqlitePath )
213- {
214- $ oldSqlitePath = storage_path ('redirect/errors.sqlite ' );
215-
216- if (! file_exists ($ sqlitePath ) && file_exists ($ oldSqlitePath )) {
217- File::move ($ oldSqlitePath , $ sqlitePath );
218-
219- return ;
220- }
221-
222- if (! file_exists ($ sqlitePath )) {
223- File::put ($ sqlitePath , '' );
224-
225- $ gitIgnorePath = storage_path ('redirect/.gitignore ' );
226- if (! file_exists ($ gitIgnorePath )) {
227- File::put ($ gitIgnorePath , "* \n!.gitignore " );
228- }
182+ if (config ('statamic.redirect.error_connection ' , 'redirect-sqlite ' ) !== 'redirect-sqlite ' && ! $ this ->generalConnectionIsBuiltinSqlite ()) {
183+ return $ this ;
229184 }
230- }
231185
232- protected function bootDatabaseForErrors ()
233- {
234- if (
235- config ('statamic.redirect.error_connection ' , 'redirect-sqlite ' ) !== 'redirect-sqlite ' &&
236- ! $ this ->generalConnectionIsBuiltinSqlite ()
237- ) {
238- return ;
239- }
186+ $ this ->createSqliteConnection ();
240187
241188 $ defaultConnection = DB ::getDefaultConnection ();
242189 DB ::setDefaultConnection ('redirect-sqlite ' );
243190
244- if (! Schema::connection ( ' redirect-sqlite ' )-> hasTable ('errors ' )) {
191+ if (! Schema::hasTable ('errors ' )) {
245192 require_once (__DIR__ . '/../database/migrations/create_redirect_error_tables.php.stub ' );
246193 (new \CreateRedirectErrorTables ())->up ();
247194 }
248195
249- if (! Schema::connection ( ' redirect-sqlite ' )-> hasColumn ('errors ' , 'url_md5 ' )) {
196+ if (! Schema::hasColumn ('errors ' , 'url_md5 ' )) {
250197 require_once (__DIR__ . '/../database/migrations/increase_redirect_error_table_url_length.php.stub ' );
251198 (new \IncreaseRedirectErrorTableUrlLength ())->up ();
252199 }
253200
254201 DB ::setDefaultConnection ($ defaultConnection );
202+
203+ return $ this ;
255204 }
256205
257- protected function bootDatabaseForRedirects ()
206+ protected function bootRedirects (): self
258207 {
259- if (
260- config ('statamic.redirect.redirect_connection ' , 'stache ' ) !== 'redirect-sqlite ' &&
261- ! $ this ->generalConnectionIsBuiltinSqlite ()
262- ) {
263- return ;
208+ $ connection = config ('statamic.redirect.redirect_connection ' , 'stache ' );
209+
210+ if ($ connection === 'stache ' ) {
211+ $ redirectStore = new RedirectStore ();
212+ $ redirectStore ->directory (config ('statamic.redirect.redirect_store ' , base_path ('content/redirects ' )));
213+ app (Stache::class)->registerStore ($ redirectStore );
214+
215+ return $ this ;
264216 }
265217
266- if (Schema::connection ('redirect-sqlite ' )->hasTable ('redirects ' )) {
267- return ;
218+ if ($ this ->generalConnectionIsBuiltinSqlite ()) {
219+ $ this ->createSqliteConnection ();
220+ }
221+
222+ if (Schema::connection ($ connection )->hasTable ('redirects ' )) {
223+ return $ this ;
268224 }
269225
270226 $ defaultConnection = DB ::getDefaultConnection ();
271- DB ::setDefaultConnection (' redirect-sqlite ' );
227+ DB ::setDefaultConnection ($ connection );
272228
273229 require_once (__DIR__ . '/../database/migrations/create_redirect_redirects_table.php.stub ' );
274230 (new \CreateRedirectRedirectsTable ())->up ();
@@ -278,6 +234,43 @@ protected function bootDatabaseForRedirects()
278234 (new \IncreaseRedirectRedirectsTableUrlLength ())->up ();
279235
280236 DB ::setDefaultConnection ($ defaultConnection );
237+
238+ return $ this ;
239+ }
240+
241+ protected function createSqliteConnection (): void
242+ {
243+ $ sqlitePath = storage_path ('redirect/redirect.sqlite ' );
244+ $ this ->ensureDatabaseExists ($ sqlitePath );
245+
246+ if (Config::has ('database.connections.redirect-sqlite ' )) {
247+ return ;
248+ }
249+
250+ Config::set ('database.connections.redirect-sqlite ' , [
251+ 'driver ' => 'sqlite ' ,
252+ 'database ' => $ sqlitePath ,
253+ ]);
254+ }
255+
256+ protected function ensureDatabaseExists ($ sqlitePath ): void
257+ {
258+ $ oldSqlitePath = storage_path ('redirect/errors.sqlite ' );
259+
260+ if (! file_exists ($ sqlitePath ) && file_exists ($ oldSqlitePath )) {
261+ File::move ($ oldSqlitePath , $ sqlitePath );
262+
263+ return ;
264+ }
265+
266+ if (! file_exists ($ sqlitePath )) {
267+ File::put ($ sqlitePath , '' );
268+
269+ $ gitIgnorePath = storage_path ('redirect/.gitignore ' );
270+ if (! file_exists ($ gitIgnorePath )) {
271+ File::put ($ gitIgnorePath , "* \n!.gitignore " );
272+ }
273+ }
281274 }
282275
283276 protected function generalConnectionIsBuiltinSqlite ()
@@ -293,7 +286,7 @@ protected function generalConnectionIsBuiltinSqlite()
293286 return false ;
294287 }
295288
296- protected function registerAddonConfig ()
289+ protected function registerAddonConfig (): self
297290 {
298291 $ this ->mergeConfigFrom (__DIR__ . '/../config/redirect.php ' , 'statamic.redirect ' );
299292
0 commit comments