From 090f4d370f37c86472dd1447ce30510ef9b87780 Mon Sep 17 00:00:00 2001 From: Sander van Dragt Date: Thu, 1 Oct 2020 15:41:35 +0100 Subject: [PATCH] Make Url::fromFile static. --- src/system/Cache.php | 3 +-- src/system/Model.php | 3 +-- src/system/Url.php | 13 ++++++------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/system/Cache.php b/src/system/Cache.php index e0c1d94..e4216d3 100644 --- a/src/system/Cache.php +++ b/src/system/Cache.php @@ -177,8 +177,7 @@ public function generateSite(): string foreach ($Files->files() as $index => $file_path) { $File = new File($file_path); - $Url = new Url(); - $cache_urls[] = $Url->convertFileToURL($File); + $cache_urls[] = Url::fromFile($File); } $urls = array( diff --git a/src/system/Model.php b/src/system/Model.php index 6a64718..a59b03d 100644 --- a/src/system/Model.php +++ b/src/system/Model.php @@ -48,7 +48,6 @@ protected function listContents($record): StdClass { $Content = new StdClass(); - $Url = new Url(); $File = new File($record); $content_sections = preg_split('/\R\R\R/', trim(file_get_contents($File->path)), count($this->model)); @@ -70,7 +69,7 @@ protected function listContents($record): StdClass exit(); } - $Content->link = $Url->convertFileToURL($File)->url_absolute; + $Content->link = Url::fromFile($File)->url_absolute; for ($i = 0, $len = count($this->model); $i < $len; $i++) { $content_section = $content_sections[ $i ]; diff --git a/src/system/Url.php b/src/system/Url.php index 079bebe..a8e8a8c 100644 --- a/src/system/Url.php +++ b/src/system/Url.php @@ -14,8 +14,6 @@ public function __construct($path = null) if (is_string($path)) { $this->setUrl($path); } - - return $this; } protected function setUrl(string $path): void @@ -33,7 +31,7 @@ protected function setUrl(string $path): void protected function protocol(): string { $protocol = 'http://'; - if (! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) { + if ( ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) || $_SERVER['SERVER_PORT'] === 443) { $protocol = 'https://'; } @@ -44,11 +42,11 @@ protected function protocol(): string * Converts a file to an url. * make sure to call Url->url_absolute after. * - * @param object $file_object File object + * @param File $file_object File object * * @return self url object */ - public function convertFileToURL($file_object): self + public static function fromFile(File $file_object): self { $url = str_replace(DIRECTORY_SEPARATOR, "/", $file_object->path); $url = '/' . ltrim($url, '/'); @@ -64,8 +62,9 @@ public function convertFileToURL($file_object): self } Log::debug(__FUNCTION__ . " relative_url: $relative_url"); - $this->setUrl($relative_url); + $Url = new Url(); + $Url->setUrl($relative_url); - return $this; + return $Url; } }