Merge pull request #515 from flightphp/dev

Fixed tests and lint problems in windows
pull/524/head
n0nag0n 1 year ago committed by GitHub
commit a42cbfe13c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -50,11 +50,9 @@
"lint": "phpstan --no-progress -cphpstan.neon" "lint": "phpstan --no-progress -cphpstan.neon"
}, },
"suggest": { "suggest": {
"latte/latte": "Latte template engine" "latte/latte": "Latte template engine",
},
"suggest-dev": {
"tracy/tracy": "Tracy debugger" "tracy/tracy": "Tracy debugger"
}, },
"replace": { "replace": {
"mikecao/flight": "2.0.2" "mikecao/flight": "2.0.2"
} }

@ -758,9 +758,8 @@ class Engine
/** /**
* Gets a url from an alias that's supplied. * Gets a url from an alias that's supplied.
* *
* @param string $alias the route alias * @param string $alias the route alias.
* @param array<string,mixed> the params for the route if applicable * @param array<string, mixed> $params The params for the route if applicable.
* @return string
*/ */
public function _getUrl(string $alias, array $params = []): string public function _getUrl(string $alias, array $params = []): string
{ {

@ -17,32 +17,16 @@ namespace flight\template;
*/ */
class View class View
{ {
/** /** @var string Location of view templates. */
* Location of view templates.
*
* @var string
*/
public $path; public $path;
/** /** @var string File extension. */
* File extension.
*
* @var string
*/
public $extension = '.php'; public $extension = '.php';
/** /** @var array<string, mixed> View variables. */
* View variables.
*
* @var array<string, mixed>
*/
protected $vars = []; protected $vars = [];
/** /** @var string Template file. */
* Template file.
*
* @var string
*/
private $template; private $template;
/** /**
@ -58,9 +42,9 @@ class View
/** /**
* Gets a template variable. * Gets a template variable.
* *
* @param string $key Key * @param string $key
* *
* @return mixed Value * @return mixed Variable value or `null` if doesn't exists
*/ */
public function get($key) public function get($key)
{ {
@ -70,13 +54,13 @@ class View
/** /**
* Sets a template variable. * Sets a template variable.
* *
* @param string|iterable<string, mixed> $key Key * @param string|iterable<string, mixed> $key
* @param mixed $value Value * @param mixed $value Value
* @return static * @return $this
*/ */
public function set($key, $value = null) public function set($key, $value = null)
{ {
if (\is_array($key) || \is_object($key)) { if (\is_iterable($key)) {
foreach ($key as $k => $v) { foreach ($key as $k => $v) {
$this->vars[$k] = $v; $this->vars[$k] = $v;
} }
@ -90,7 +74,7 @@ class View
/** /**
* Checks if a template variable is set. * Checks if a template variable is set.
* *
* @param string $key Key * @param string $key
* *
* @return bool If key exists * @return bool If key exists
*/ */
@ -102,8 +86,9 @@ class View
/** /**
* Unsets a template variable. If no key is passed in, clear all variables. * Unsets a template variable. If no key is passed in, clear all variables.
* *
* @param string $key Key * @param ?string $key
* @return static *
* @return $this
*/ */
public function clear($key = null) public function clear($key = null)
{ {
@ -120,24 +105,25 @@ class View
* Renders a template. * Renders a template.
* *
* @param string $file Template file * @param string $file Template file
* @param array<string, mixed> $data Template data * @param ?array<string, mixed> $data Template data
* *
* @throws \Exception If template not found
* @return void * @return void
* @throws \Exception If template not found
*/ */
public function render($file, $data = null) public function render($file, $data = null)
{ {
$this->template = $this->getTemplate($file); $this->template = $this->getTemplate($file);
if (!file_exists($this->template)) { if (!\file_exists($this->template)) {
throw new \Exception("Template file not found: {$this->template}."); $normalized_path = self::normalizePath($this->template);
throw new \Exception("Template file not found: {$normalized_path}.");
} }
if (\is_array($data)) { if (\is_array($data)) {
$this->vars = array_merge($this->vars, $data); $this->vars = \array_merge($this->vars, $data);
} }
extract($this->vars); \extract($this->vars);
include $this->template; include $this->template;
} }
@ -146,17 +132,17 @@ class View
* Gets the output of a template. * Gets the output of a template.
* *
* @param string $file Template file * @param string $file Template file
* @param array<string, mixed> $data Template data * @param ?array<string, mixed> $data Template data
* *
* @return string Output of template * @return string Output of template
*/ */
public function fetch($file, $data = null) public function fetch($file, $data = null)
{ {
ob_start(); \ob_start();
$this->render($file, $data); $this->render($file, $data);
return ob_get_clean(); return \ob_get_clean();
} }
/** /**
@ -168,7 +154,7 @@ class View
*/ */
public function exists($file) public function exists($file)
{ {
return file_exists($this->getTemplate($file)); return \file_exists($this->getTemplate($file));
} }
/** /**
@ -182,13 +168,13 @@ class View
{ {
$ext = $this->extension; $ext = $this->extension;
if (!empty($ext) && (substr($file, -1 * \strlen($ext)) != $ext)) { if (!empty($ext) && (\substr($file, -1 * \strlen($ext)) != $ext)) {
$file .= $ext; $file .= $ext;
} }
$is_windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; $is_windows = \strtoupper(\substr(PHP_OS, 0, 3)) === 'WIN';
if (('/' == substr($file, 0, 1)) || ($is_windows === true && ':' == substr($file, 1, 1))) { if (('/' == \substr($file, 0, 1)) || ($is_windows === true && ':' == \substr($file, 1, 1))) {
return $file; return $file;
} }
@ -204,8 +190,19 @@ class View
*/ */
public function e($str) public function e($str)
{ {
$value = htmlentities($str); $value = \htmlentities($str);
echo $value; echo $value;
return $value; return $value;
} }
/**
* @param string $path An unnormalized path.
* @param string $separator Path separator.
*
* @return string Normalized path.
*/
protected static function normalizePath($path, $separator = DIRECTORY_SEPARATOR)
{
return \str_replace(['\\', '/'], $separator, $path);
}
} }

@ -1,4 +1,7 @@
<?php <?php
use flight\template\View;
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
@ -65,7 +68,7 @@ class ViewTest extends PHPUnit\Framework\TestCase
public function testRenderBadFilePath() { public function testRenderBadFilePath() {
$this->expectException(Exception::class); $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'); $this->view->render('badfile');
} }
@ -117,4 +120,27 @@ class ViewTest extends PHPUnit\Framework\TestCase
$result = $this->view->e('script'); $result = $this->view->e('script');
$this->assertEquals('script', $result); $this->assertEquals('script', $result);
} }
public function testNormalizePath(): void
{
$viewMock = new class extends View {
public static function normalizePath($path, $separator = DIRECTORY_SEPARATOR)
{
return parent::normalizePath($path, $separator);
}
};
$this->assertSame(
'C:/xampp/htdocs/libs/Flight/core/index.php',
$viewMock::normalizePath('C:\xampp\htdocs\libs\Flight/core/index.php', '/')
);
$this->assertSame(
'C:\xampp\htdocs\libs\Flight\core\index.php',
$viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '\\')
);
$this->assertSame(
'C:°xampp°htdocs°libs°Flight°core°index.php',
$viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '°')
);
}
} }

Loading…
Cancel
Save