Fixed windows directory separator compatibility

pull/515/head
fadrian06 1 year ago
parent 8b574d43f9
commit 9cd5b9dc0d

@ -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);
}
}

@ -1,4 +1,7 @@
<?php
use flight\template\View;
/**
* Flight: An extensible micro-framework.
*
@ -65,7 +68,7 @@ class ViewTest extends PHPUnit\Framework\TestCase
public function testRenderBadFilePath() {
$this->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', '°')
);
}
}

Loading…
Cancel
Save