Skip to content

Commit

Permalink
updated file to reflect last update
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardosahon committed Jan 10, 2024
1 parent b0a23b1 commit 715e9d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/Core/Traits/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ public function dont_use_objects(): self
return $this->switch("use_objects", false);
}

public function use_domain_as_sub(): self
{
return $this->switch("use_domain_as_sub", true);
}

/**
* Prevents the data sent through the ViewHandler of a specific domain from being cached.
* This only takes effect in development environment, if Lay detects the server is in production, it'll cache by default
Expand Down
6 changes: 6 additions & 0 deletions src/Core/Traits/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ private static function initialize() : self {
# Used by the Domain module to instruct the handler to cache all the listed domains in a session or cookie,
# depending on the value sent by dev
"cache_domains" => $options['switch']['cache_domains'] ?? true,
# If true, when linking to a domain using the Anchor tag class, on production server;
# example.com/blog will be converted to
# blog.example.com
"use_domain_as_sub" => $options['switch']['use_domain_as_sub'] ?? false,
# Internal option set by lay to tell dev that the /web/domain/ folder is where html is being served from
"using_domain" => $options['header']['using_domain'] ?? null,
# Internal option set by lay to tell dev that the /web/ folder is where html is being served from
"using_web" => $options['header']['using_web'] ?? null,
"use_domain_file" => null, // this is updated when webroot is created after first class is init
"name" => [
Expand Down
25 changes: 17 additions & 8 deletions src/Core/View/Tags/Anchor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ final class Anchor {

use Standard;

public function href(?string $link = "", ?string $domain_id = null) : self {
public function href(?string $link = "", ?string $domain_id = null, ?bool $use_subdomain = null) : self {
$dom = DomainResource::get()->domain;
$link = is_null($link) ? '' : $link;
$link = ltrim($link, "/");
$use_subdomain = $use_subdomain !== null ? $use_subdomain : LayConfig::new()->use_domain_as_sub();

$base = LayConfig::site_data();
$base_full = $dom->domain_uri;
Expand All @@ -33,17 +34,25 @@ public function href(?string $link = "", ?string $domain_id = null) : self {

$same_domain = $domain_id == $dom->domain_id;

if(!$same_domain && $dom->domain_type != DomainType::SUB) {
if(!$same_domain) {
$pattern = $pattern == "*" ? "" : $pattern;
$base_full = explode($dom->pattern . "/", $base_full . $pattern, 2)[0];
}

$base_full = rtrim($base_full, "/") . "/";
if($dom->domain_type != DomainType::SUB) {
$base_full = explode($dom->pattern . "/", $base_full . $pattern, 2)[0];

if($use_subdomain && !empty($pattern) && LayConfig::$ENV_IS_PROD)
$base_full = $base->proto . $pattern . "." . $base->domain_no_proto;
}
else {
$x = explode(".", $base->domain_no_proto, 2);
$base_full = $base->proto . end($x);

if(!$same_domain && $dom->domain_type == DomainType::SUB) {
$x = explode(".", $base->domain_no_proto, 2);
$base_full = $base->proto . end($x) . "/";
if($use_subdomain && !empty($pattern))
$base_full = $base->proto . $pattern . "." . end($x);
}
}

$base_full = rtrim($base_full, "/") . "/";
}

if(str_starts_with($link, "#"))
Expand Down
3 changes: 1 addition & 2 deletions src/Core/View/ViewBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use JetBrains\PhpStorm\ExpectedValues;
use JetBrains\PhpStorm\NoReturn;

// TODO: Find a way to cache views
final class ViewBuilder
{
use IsSingleton;
Expand Down Expand Up @@ -52,7 +51,7 @@ public function init_start(): self

if (!self::$href_set) {
self::$href_set = true;
$this->local("href", fn(?string $href = "", ?string $domain_id = null) => Anchor::new()->href($href, $domain_id)->get_href());
$this->local("href", fn(?string $href = "", ?string $domain_id = null, ?bool $use_subdomain = null) => Anchor::new()->href($href, $domain_id, $use_subdomain)->get_href());
}

return $this;
Expand Down

0 comments on commit 715e9d4

Please sign in to comment.