Skip to content

Commit

Permalink
Make Url::fromFile static.
Browse files Browse the repository at this point in the history
  • Loading branch information
svandragt committed Oct 1, 2020
1 parent ad62b75 commit 090f4d3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/system/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions src/system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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 ];
Expand Down
13 changes: 6 additions & 7 deletions src/system/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public function __construct($path = null)
if (is_string($path)) {
$this->setUrl($path);
}

return $this;
}

protected function setUrl(string $path): void
Expand All @@ -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://';
}

Expand All @@ -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, '/');
Expand All @@ -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;
}
}

0 comments on commit 090f4d3

Please sign in to comment.