Skip to content

Commit a463332

Browse files
authored
Merge pull request #57 from akrabat/custom-exceptions
Add custom exceptions
2 parents c2874bd + 12ce65a commit a463332

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Slim\Views\Exception;
6+
7+
final class PhpTemplateNotFoundException extends \RuntimeException
8+
{
9+
}

src/PhpRenderer.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use InvalidArgumentException;
1515
use Psr\Http\Message\ResponseInterface;
16-
use RuntimeException;
16+
use Slim\Views\Exception\PhpTemplateNotFoundException;
1717
use Throwable;
1818

1919
/**
@@ -52,7 +52,7 @@ public function __construct(string $templatePath = '', array $attributes = [], s
5252
* @note $data cannot contain template as a key
5353
*
5454
* @throws InvalidArgumentException
55-
* @throws RuntimeException if $templatePath . $template does not exist
55+
* @throws PhpTemplateNotFoundException if $templatePath . $template does not exist
5656
* @throws Throwable
5757
*/
5858
public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface
@@ -82,7 +82,7 @@ public function setLayout(string $layout): void
8282
} else {
8383
$layoutPath = $this->templatePath . $layout;
8484
if (!is_file($layoutPath)) {
85-
throw new RuntimeException('Layout template "' . $layout . '" does not exist');
85+
throw new PhpTemplateNotFoundException('Layout template "' . $layout . '" does not exist');
8686
}
8787
$this->layout = $layout;
8888
}
@@ -148,7 +148,7 @@ public function setTemplatePath(string $templatePath): void
148148
* @note $data cannot contain template as a key
149149
*
150150
* @throws InvalidArgumentException
151-
* @throws RuntimeException
151+
* @throws PhpTemplateNotFoundException
152152
* @throws Throwable
153153
*/
154154
public function fetch(string $template, array $data = [], bool $useLayout = false): string
@@ -169,7 +169,7 @@ public function fetch(string $template, array $data = [], bool $useLayout = fals
169169
* @note $data cannot contain template as a key
170170
*
171171
* @throws InvalidArgumentException
172-
* @throws RuntimeException
172+
* @throws PhpTemplateNotFoundException
173173
* @throws Throwable
174174
*/
175175
public function fetchTemplate(string $template, array $data = []): string
@@ -179,7 +179,8 @@ public function fetchTemplate(string $template, array $data = []): string
179179
}
180180

181181
if (!is_file($this->templatePath . $template)) {
182-
throw new RuntimeException('View cannot render "' . $template . '" because the template does not exist');
182+
throw new PhpTemplateNotFoundException('View cannot render "' . $template
183+
. '" because the template does not exist');
183184
}
184185

185186
$data = array_merge($this->attributes, $data);

tests/PhpRendererTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
use InvalidArgumentException;
88
use PHPUnit\Framework\TestCase;
9-
use RuntimeException;
109
use Slim\Psr7\Headers;
1110
use Slim\Psr7\Response;
1211
use Slim\Psr7\Stream;
12+
use Slim\Views\Exception\PhpTemplateNotFoundException;
1313
use Slim\Views\PhpRenderer;
1414
use Throwable;
1515

@@ -107,7 +107,7 @@ public function testTemplateNotFound(): void
107107
$body = new Stream(fopen('php://temp', 'r+'));
108108
$response = new Response(200, $headers, $body);
109109

110-
$this->expectException(RuntimeException::class);
110+
$this->expectException(PhpTemplateNotFoundException::class);
111111
$renderer->render($response, 'adfadftemplate.phtml', []);
112112
}
113113

@@ -171,7 +171,7 @@ public function testExceptionInLayout(): void
171171
public function testLayoutNotFound(): void
172172
{
173173
$renderer = new PhpRenderer(__DIR__ . '/_files/');
174-
$this->expectException(RuntimeException::class);
174+
$this->expectException(PhpTemplateNotFoundException::class);
175175
$renderer->setLayout('non-existent_layout.phtml');
176176
}
177177

0 commit comments

Comments
 (0)