From 8b574d43f9ef95fb71af5f2b4f92bed38959ee31 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Fri, 12 Jan 2024 17:10:41 -0400 Subject: [PATCH 1/7] Fixed composer validate problem --- composer.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 767db45..121725a 100644 --- a/composer.json +++ b/composer.json @@ -49,11 +49,9 @@ "lint": "phpstan --no-progress -cphpstan.neon" }, "suggest": { - "latte/latte": "Latte template engine" - }, - "suggest-dev": { + "latte/latte": "Latte template engine", "tracy/tracy": "Tracy debugger" - }, + }, "replace": { "mikecao/flight": "2.0.2" } From 9cd5b9dc0d552e6cef20765c56b41564bb10718d Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Fri, 12 Jan 2024 17:29:07 -0400 Subject: [PATCH 2/7] Fixed windows directory separator compatibility --- flight/template/View.php | 10 ++++++++++ tests/ViewTest.php | 28 +++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) 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', '°') + ); + } } From dc5f1a9c083f923c9ca2e05b0cd4ea6362a6b967 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Fri, 12 Jan 2024 17:36:06 -0400 Subject: [PATCH 3/7] Engine::_getUrl() docblock fixed --- flight/Engine.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index 44affc1..85ae34d 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -707,9 +707,8 @@ class Engine /** * Gets a url from an alias that's supplied. * - * @param string $alias the route alias - * @param array the params for the route if applicable - * @return string + * @param string $alias the route alias. + * @param array $params The params for the route if applicable. */ public function _getUrl(string $alias, array $params = []): string { From 7ac12b7891ad37adb1cc6e3b84f82edfa3723b0e Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Fri, 12 Jan 2024 17:46:30 -0400 Subject: [PATCH 4/7] Removed repeated code --- flight/template/View.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/flight/template/View.php b/flight/template/View.php index a3f0246..550dce0 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -212,10 +212,6 @@ class View protected static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string { - if ($separator === '/') { - return str_replace(['\\', '/'], $separator, $path); - } - - return str_replace(['/', '\\'], $separator, $path); + return str_replace(['\\', '/'], $separator, $path); } } From 147c42449de409239d376e256e97811a512c19b5 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Fri, 12 Jan 2024 17:55:53 -0400 Subject: [PATCH 5/7] Added PHP Type hints to View class --- flight/template/View.php | 94 +++++++++++++++------------------------- 1 file changed, 36 insertions(+), 58 deletions(-) diff --git a/flight/template/View.php b/flight/template/View.php index 550dce0..9426081 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -17,40 +17,24 @@ namespace flight\template; */ class View { - /** - * Location of view templates. - * - * @var string - */ - public $path; + /** Location of view templates. */ + public string $path; - /** - * File extension. - * - * @var string - */ - public $extension = '.php'; + /** File extension. */ + public string $extension = '.php'; - /** - * View variables. - * - * @var array - */ - protected $vars = []; + /** @var array View variables. */ + protected array $vars = []; - /** - * Template file. - * - * @var string - */ - private $template; + /** Template file. */ + private string $template; /** * Constructor. * * @param string $path Path to templates directory */ - public function __construct($path = '.') + public function __construct(string $path = '.') { $this->path = $path; } @@ -58,11 +42,9 @@ class View /** * Gets a template variable. * - * @param string $key Key - * - * @return mixed Value + * @return mixed Variable value or `null` if doesn't exists */ - public function get($key) + public function get(string $key) { return $this->vars[$key] ?? null; } @@ -70,13 +52,13 @@ class View /** * Sets a template variable. * - * @param string|iterable $key Key + * @param string|iterable $key * @param mixed $value Value - * @return static + * @return $this */ - public function set($key, $value = null) + public function set($key, $value = null): self { - if (\is_array($key) || \is_object($key)) { + if (\is_iterable($key)) { foreach ($key as $k => $v) { $this->vars[$k] = $v; } @@ -90,11 +72,9 @@ class View /** * Checks if a template variable is set. * - * @param string $key Key - * * @return bool If key exists */ - public function has($key) + public function has(string $key): bool { return isset($this->vars[$key]); } @@ -102,10 +82,9 @@ class View /** * Unsets a template variable. If no key is passed in, clear all variables. * - * @param string $key Key - * @return static + * @return $this */ - public function clear($key = null) + public function clear(?string $key = null): self { if (null === $key) { $this->vars = []; @@ -120,25 +99,24 @@ class View * Renders a template. * * @param string $file Template file - * @param array $data Template data + * @param array $data Template data * * @throws \Exception If template not found - * @return void */ - public function render($file, $data = null) + public function render(string $file, ?array $data = null): void { $this->template = $this->getTemplate($file); - if (!file_exists($this->template)) { + if (!\file_exists($this->template)) { $this->template = self::normalizePath($this->template); throw new \Exception("Template file not found: {$this->template}."); } 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; } @@ -147,17 +125,17 @@ class View * Gets the output of a template. * * @param string $file Template file - * @param array $data Template data + * @param ?array $data Template data * * @return string Output of template */ - public function fetch($file, $data = null) + public function fetch(string $file, ?array $data = null): string { - ob_start(); + \ob_start(); $this->render($file, $data); - return ob_get_clean(); + return \ob_get_clean(); } /** @@ -167,9 +145,9 @@ class View * * @return bool Template file exists */ - public function exists($file) + public function exists(string $file): bool { - return file_exists($this->getTemplate($file)); + return \file_exists($this->getTemplate($file)); } /** @@ -179,17 +157,17 @@ class View * * @return string Template file location */ - public function getTemplate($file) + public function getTemplate(string $file): string { $ext = $this->extension; - if (!empty($ext) && (substr($file, -1 * \strlen($ext)) != $ext)) { + if (!empty($ext) && (\substr($file, -1 * \strlen($ext)) != $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; } @@ -203,15 +181,15 @@ class View * * @return string Escaped string */ - public function e($str) + public function e(string $str): string { - $value = htmlentities($str); + $value = \htmlentities($str); echo $value; return $value; } protected static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string { - return str_replace(['\\', '/'], $separator, $path); + return \str_replace(['\\', '/'], $separator, $path); } } From 9251a6da90753a0922c40e1e56b8bb96ac59473e Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Fri, 12 Jan 2024 18:00:06 -0400 Subject: [PATCH 6/7] Apply code-style --- tests/ViewTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 9a6cc0d..896b129 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -130,15 +130,15 @@ class ViewTest extends PHPUnit\Framework\TestCase } }; - self::assertSame( + $this->assertSame( 'C:/xampp/htdocs/libs/Flight/core/index.php', $viewMock::normalizePath('C:\xampp\htdocs\libs\Flight/core/index.php', '/') ); - self::assertSame( + $this->assertSame( 'C:\xampp\htdocs\libs\Flight\core\index.php', $viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '\\') ); - self::assertSame( + $this->assertSame( 'C:°xampp°htdocs°libs°Flight°core°index.php', $viewMock::normalizePath('C:/xampp/htdocs/libs/Flight\core\index.php', '°') ); From 193cf9bb8b6bd23a874345a84efb32a529a1bba3 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 17 Jan 2024 11:01:12 -0400 Subject: [PATCH 7/7] Removed type hints to PHP 5 compatibility --- flight/template/View.php | 55 +++++++++++++++++++++++++--------------- tests/ViewTest.php | 2 +- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/flight/template/View.php b/flight/template/View.php index 9426081..21963ee 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -17,24 +17,24 @@ namespace flight\template; */ class View { - /** Location of view templates. */ - public string $path; + /** @var string Location of view templates. */ + public $path; - /** File extension. */ - public string $extension = '.php'; + /** @var string File extension. */ + public $extension = '.php'; /** @var array View variables. */ - protected array $vars = []; + protected $vars = []; - /** Template file. */ - private string $template; + /** @var string Template file. */ + private $template; /** * Constructor. * * @param string $path Path to templates directory */ - public function __construct(string $path = '.') + public function __construct($path = '.') { $this->path = $path; } @@ -42,9 +42,11 @@ class View /** * Gets a template variable. * + * @param string $key + * * @return mixed Variable value or `null` if doesn't exists */ - public function get(string $key) + public function get($key) { return $this->vars[$key] ?? null; } @@ -56,7 +58,7 @@ class View * @param mixed $value Value * @return $this */ - public function set($key, $value = null): self + public function set($key, $value = null) { if (\is_iterable($key)) { foreach ($key as $k => $v) { @@ -72,9 +74,11 @@ class View /** * Checks if a template variable is set. * + * @param string $key + * * @return bool If key exists */ - public function has(string $key): bool + public function has($key) { return isset($this->vars[$key]); } @@ -82,9 +86,11 @@ class View /** * Unsets a template variable. If no key is passed in, clear all variables. * + * @param ?string $key + * * @return $this */ - public function clear(?string $key = null): self + public function clear($key = null) { if (null === $key) { $this->vars = []; @@ -99,17 +105,18 @@ class View * Renders a template. * * @param string $file Template file - * @param array $data Template data + * @param ?array $data Template data * + * @return void * @throws \Exception If template not found */ - public function render(string $file, ?array $data = null): void + public function render($file, $data = null) { $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}."); + $normalized_path = self::normalizePath($this->template); + throw new \Exception("Template file not found: {$normalized_path}."); } if (\is_array($data)) { @@ -129,7 +136,7 @@ class View * * @return string Output of template */ - public function fetch(string $file, ?array $data = null): string + public function fetch($file, $data = null) { \ob_start(); @@ -145,7 +152,7 @@ class View * * @return bool Template file exists */ - public function exists(string $file): bool + public function exists($file) { return \file_exists($this->getTemplate($file)); } @@ -157,7 +164,7 @@ class View * * @return string Template file location */ - public function getTemplate(string $file): string + public function getTemplate($file) { $ext = $this->extension; @@ -181,14 +188,20 @@ class View * * @return string Escaped string */ - public function e(string $str): string + public function e($str) { $value = \htmlentities($str); echo $value; return $value; } - protected static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string + /** + * @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); } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 896b129..f83e0c5 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -124,7 +124,7 @@ class ViewTest extends PHPUnit\Framework\TestCase public function testNormalizePath(): void { $viewMock = new class extends View { - public static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string + public static function normalizePath($path, $separator = DIRECTORY_SEPARATOR) { return parent::normalizePath($path, $separator); }