Skip to content

Commit 06f9d38

Browse files
committed
BaseController enhancements. Added an extra optional previous exception parameter to the forceHttp*** methods and also modified renderLayout & renderView to force a 404 response when the layout or view file cannot be found.
1 parent ef951df commit 06f9d38

File tree

1 file changed

+48
-28
lines changed

1 file changed

+48
-28
lines changed

src/controllers/BaseController.php

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ public function makeLink(string $path): string {
513513
* file (i.e. the file named $file_name).
514514
*
515515
* @psalm-suppress MixedInferredReturnType
516+
* @psalm-suppress InvalidReturnType
516517
*/
517518
public function renderLayout( string $file_name, array $data = ['content'=>'Content should be placed here!'] ): string {
518519

@@ -523,11 +524,20 @@ public function renderLayout( string $file_name, array $data = ['content'=>'Cont
523524
*/
524525
$this->layout_renderer = $this->getContainerItem(ContainerKeys::LAYOUT_RENDERER); // get new instance for each call to this method renderLayout
525526

526-
/**
527-
* @psalm-suppress MixedReturnStatement
528-
* @psalm-suppress MixedMethodCall
529-
*/
530-
return $this->layout_renderer->renderToString($file_name, $data);
527+
try {
528+
/**
529+
* @psalm-suppress MixedReturnStatement
530+
* @psalm-suppress MixedMethodCall
531+
*/
532+
return $this->layout_renderer->renderToString($file_name, $data);
533+
534+
} catch (\Rotexsoft\FileRenderer\FileNotFoundException $ex) {
535+
536+
$this->forceHttp404(
537+
$this->vespula_locale->gettext('error_layout_not_found'),
538+
null, $ex
539+
);
540+
}
531541
}
532542

533543
/**
@@ -547,6 +557,7 @@ public function renderLayout( string $file_name, array $data = ['content'=>'Cont
547557
* file (i.e. the file named $file_name).
548558
*
549559
* @psalm-suppress MixedInferredReturnType
560+
* @psalm-suppress InvalidReturnType
550561
*/
551562
public function renderView( string $file_name, array $data = [] ): string {
552563

@@ -601,11 +612,20 @@ public function renderView( string $file_name, array $data = [] ): string {
601612

602613
$data['controller_object'] = $this;
603614

604-
/**
605-
* @psalm-suppress MixedReturnStatement
606-
* @psalm-suppress MixedMethodCall
607-
*/
608-
return $this->view_renderer->renderToString($file_name, $data);
615+
try {
616+
/**
617+
* @psalm-suppress MixedReturnStatement
618+
* @psalm-suppress MixedMethodCall
619+
*/
620+
return $this->view_renderer->renderToString($file_name, $data);
621+
622+
} catch (\Rotexsoft\FileRenderer\FileNotFoundException $ex) {
623+
624+
$this->forceHttp404(
625+
$this->vespula_locale->gettext('error_view_not_found'),
626+
null, $ex
627+
);
628+
}
609629
}
610630

611631
/**
@@ -1196,13 +1216,13 @@ public function getContainer(): \Psr\Container\ContainerInterface {
11961216
*
11971217
* @psalm-suppress PossiblyUnusedMethod
11981218
*/
1199-
public function forceHttp400(string $message, ?ServerRequestInterface $request=null): void {
1219+
public function forceHttp400(string $message, ?ServerRequestInterface $request=null, ?\Throwable $previous_exception = null): void {
12001220

12011221
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
12021222
$this->getContainer(),
12031223
\SlimMvcTools\SlimHttpExceptionClassNames::HttpBadRequestException,
12041224
($request ?? $this->request),
1205-
$message
1225+
$message, $previous_exception
12061226
);
12071227
}
12081228

@@ -1211,13 +1231,13 @@ public function forceHttp400(string $message, ?ServerRequestInterface $request=n
12111231
*
12121232
* @psalm-suppress PossiblyUnusedMethod
12131233
*/
1214-
public function forceHttp401(string $message, ?ServerRequestInterface $request=null): void {
1234+
public function forceHttp401(string $message, ?ServerRequestInterface $request=null, ?\Throwable $previous_exception = null): void {
12151235

12161236
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
12171237
$this->getContainer(),
12181238
\SlimMvcTools\SlimHttpExceptionClassNames::HttpUnauthorizedException,
12191239
($request ?? $this->request),
1220-
$message
1240+
$message, $previous_exception
12211241
);
12221242
}
12231243

@@ -1226,13 +1246,13 @@ public function forceHttp401(string $message, ?ServerRequestInterface $request=n
12261246
*
12271247
* @psalm-suppress PossiblyUnusedMethod
12281248
*/
1229-
public function forceHttp403(string $message, ?ServerRequestInterface $request=null): void {
1249+
public function forceHttp403(string $message, ?ServerRequestInterface $request=null, ?\Throwable $previous_exception = null): void {
12301250

12311251
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
12321252
$this->getContainer(),
12331253
\SlimMvcTools\SlimHttpExceptionClassNames::HttpForbiddenException,
12341254
($request ?? $this->request),
1235-
$message
1255+
$message, $previous_exception
12361256
);
12371257
}
12381258

@@ -1241,13 +1261,13 @@ public function forceHttp403(string $message, ?ServerRequestInterface $request=n
12411261
*
12421262
* @psalm-suppress PossiblyUnusedMethod
12431263
*/
1244-
public function forceHttp404(string $message, ?ServerRequestInterface $request=null): void {
1264+
public function forceHttp404(string $message, ?ServerRequestInterface $request=null, ?\Throwable $previous_exception = null): void {
12451265

12461266
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
12471267
$this->getContainer(),
12481268
\SlimMvcTools\SlimHttpExceptionClassNames::HttpNotFoundException,
12491269
($request ?? $this->request),
1250-
$message
1270+
$message, $previous_exception
12511271
);
12521272
}
12531273

@@ -1256,13 +1276,13 @@ public function forceHttp404(string $message, ?ServerRequestInterface $request=n
12561276
*
12571277
* @psalm-suppress PossiblyUnusedMethod
12581278
*/
1259-
public function forceHttp405(string $message, ?ServerRequestInterface $request=null): void {
1279+
public function forceHttp405(string $message, ?ServerRequestInterface $request=null, ?\Throwable $previous_exception = null): void {
12601280

12611281
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
12621282
$this->getContainer(),
12631283
\SlimMvcTools\SlimHttpExceptionClassNames::HttpMethodNotAllowedException,
12641284
($request ?? $this->request),
1265-
$message
1285+
$message, $previous_exception
12661286
);
12671287
}
12681288

@@ -1271,13 +1291,13 @@ public function forceHttp405(string $message, ?ServerRequestInterface $request=n
12711291
*
12721292
* @psalm-suppress PossiblyUnusedMethod
12731293
*/
1274-
public function forceHttp410(string $message, ?ServerRequestInterface $request=null): void {
1294+
public function forceHttp410(string $message, ?ServerRequestInterface $request=null, ?\Throwable $previous_exception = null): void {
12751295

12761296
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
12771297
$this->getContainer(),
12781298
\SlimMvcTools\SlimHttpExceptionClassNames::HttpGoneException,
12791299
($request ?? $this->request),
1280-
$message
1300+
$message, $previous_exception
12811301
);
12821302
}
12831303

@@ -1286,13 +1306,13 @@ public function forceHttp410(string $message, ?ServerRequestInterface $request=n
12861306
*
12871307
* @psalm-suppress PossiblyUnusedMethod
12881308
*/
1289-
public function forceHttp429(string $message, ?ServerRequestInterface $request=null): void {
1309+
public function forceHttp429(string $message, ?ServerRequestInterface $request=null, ?\Throwable $previous_exception = null): void {
12901310

12911311
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
12921312
$this->getContainer(),
12931313
\SlimMvcTools\SlimHttpExceptionClassNames::HttpTooManyRequestsException,
12941314
($request ?? $this->request),
1295-
$message
1315+
$message, $previous_exception
12961316
);
12971317
}
12981318

@@ -1301,13 +1321,13 @@ public function forceHttp429(string $message, ?ServerRequestInterface $request=n
13011321
*
13021322
* @psalm-suppress PossiblyUnusedMethod
13031323
*/
1304-
public function forceHttp500(string $message, ?ServerRequestInterface $request=null): void {
1324+
public function forceHttp500(string $message, ?ServerRequestInterface $request=null, ?\Throwable $previous_exception = null): void {
13051325

13061326
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
13071327
$this->getContainer(),
13081328
\SlimMvcTools\SlimHttpExceptionClassNames::HttpInternalServerErrorException,
13091329
($request ?? $this->request),
1310-
$message
1330+
$message, $previous_exception
13111331
);
13121332
}
13131333

@@ -1316,13 +1336,13 @@ public function forceHttp500(string $message, ?ServerRequestInterface $request=n
13161336
*
13171337
* @psalm-suppress PossiblyUnusedMethod
13181338
*/
1319-
public function forceHttp501(string $message, ?ServerRequestInterface $request=null): void {
1339+
public function forceHttp501(string $message, ?ServerRequestInterface $request=null, ?\Throwable $previous_exception = null): void {
13201340

13211341
throw Utils::createSlimHttpExceptionWithLocalizedDescription(
13221342
$this->getContainer(),
13231343
\SlimMvcTools\SlimHttpExceptionClassNames::HttpNotImplementedException,
13241344
($request ?? $this->request),
1325-
$message
1345+
$message, $previous_exception
13261346
);
13271347
}
13281348

0 commit comments

Comments
 (0)