diff --git a/flight/template/View.php b/flight/template/View.php index 818a100..a3f0246 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -130,6 +130,7 @@ class View $this->template = $this->getTemplate($file); if (!file_exists($this->template)) { + $this->template = self::normalizePath($this->template); throw new \Exception("Template file not found: {$this->template}."); } @@ -208,4 +209,13 @@ class View echo $value; return $value; } + + protected static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string + { + if ($separator === '/') { + return str_replace(['\\', '/'], $separator, $path); + } + + return str_replace(['/', '\\'], $separator, $path); + } } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 2013861..9a6cc0d 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -1,4 +1,7 @@ expectException(Exception::class); - $this->expectExceptionMessage('Template file not found: '.__DIR__ . '/views/badfile.php'); + $this->expectExceptionMessage('Template file not found: ' . __DIR__ . DIRECTORY_SEPARATOR . 'views'. DIRECTORY_SEPARATOR . 'badfile.php'); $this->view->render('badfile'); } @@ -117,4 +120,27 @@ class ViewTest extends PHPUnit\Framework\TestCase $result = $this->view->e('script'); $this->assertEquals('script', $result); } + + public function testNormalizePath(): void + { + $viewMock = new class extends View { + public static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string + { + return parent::normalizePath($path, $separator); + } + }; + + self::assertSame( + 'C:/xampp/htdocs/libs/Flight/core/index.php', + $viewMock::normalizePath('C:\xampp\htdocs\libs\Flight/core/index.php', '/') + ); + self::assertSame( + 'C:\xampp\htdocs\libs\Flight\core\index.php', + $viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '\\') + ); + self::assertSame( + 'C:°xampp°htdocs°libs°Flight°core°index.php', + $viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '°') + ); + } }