Skip to content

Commit

Permalink
Merge pull request #171
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
leonardosahon authored Oct 14, 2024
2 parents bc7eeea + 429e1fb commit 1919fc1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Core/Traits/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ trait Init {

private static LayMode $LAY_MODE;
private static bool $INITIALIZED = false;
private static bool $MOCKED = false;
private static bool $FIRST_CLASS_CITI_ACTIVE = false;

protected static string $dir;
Expand All @@ -35,13 +36,23 @@ private static function set_web_root(&$options) : void
$options['base_no_proto'] = self::$base_no_proto;
$options['base_no_proto_no_www'] = self::$base_no_proto_no_www;

$options['server_mocked'] = false;
$web = "";

if(!$options['using_domain']) {
$web = $options['using_web'] ? "" : "web/";
$options['use_domain_file'] = true;
}

if(self::$MOCKED) {
$options['server_mocked'] = true;
$options['use_domain_file'] = false;

$web = "";
$options['using_web'] = "";
$options['using_domain'] = "";
}

$options['domain'] = self::$base . ( $web ?: "" );
$options['domain_no_proto'] = self::$base_no_proto . ($web ? "/$web" : "");
$options['domain_no_proto_no_www'] = self::$base_no_proto_no_www . ($web ? "/$web" : "");
Expand All @@ -57,7 +68,7 @@ private static function set_dir() : void {
)[0] . $s;
}

private static function first_class_citizens() : void {
private static function first_class_citizens(bool $use_https = false) : void {
self::$FIRST_CLASS_CITI_ACTIVE = true;
self::set_dir();

Expand Down Expand Up @@ -93,7 +104,7 @@ private static function first_class_citizens() : void {
$base = str_replace($slash, "/", end($base));
$http_host = $_SERVER['HTTP_HOST'] ?? $_ENV['LAY_CUSTOM_HOST'] ?? "cli";
$env_host = $_SERVER['REMOTE_ADDR'] ?? $_ENV['LAY_CUSTOM_REMOTE_ADDR'] ?? "cli";
$proto = ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? $_SERVER['REQUEST_SCHEME'] ?? 'http') . "://";
$proto = ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? $_SERVER['REQUEST_SCHEME'] ?? ($use_https ? 'https' : 'http')) . "://";
$base_no_proto = rtrim(str_replace($slash,"/", $base),"/");

if($http_host != "cli" && !isset($_ENV['LAY_CUSTOM_HOST']))
Expand Down Expand Up @@ -234,4 +245,12 @@ public static function get_mode() : LayMode {

return self::$LAY_MODE;
}

public static function mock_server(string $host, bool $use_https) : void
{
$_ENV['LAY_CUSTOM_HOST'] = $host;
$_ENV['LAY_CUSTOM_REMOTE_ADDR'] = "lay_remote_addr";
self::$MOCKED = true;
self::first_class_citizens($use_https);
}
}
1 change: 1 addition & 0 deletions src/Core/Traits/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public static function server_data() : object
"domain" => 'string',
"domain_no_proto" => 'string',
"domain_no_proto_no_www" => 'string',
"server_mocked" => 'bool',
"author" => 'string',
"global_api" => 'string',
"name" => 'object[long, short]',
Expand Down
16 changes: 16 additions & 0 deletions src/Core/View/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Domain {

private static bool $list_domain_only = false;
private static bool $cli_mode = false;
private static bool $mocking_domain = false;

private static bool $lay_init = false;
private static LayConfig $layConfig;
Expand Down Expand Up @@ -200,6 +201,9 @@ private function activate_domain(string $id, string $pattern, string $builder) :
// Make lazy CORS configuration become active after loading all foundation files incase there was an overwrite
LayConfig::call_lazy_cors();

if(self::$mocking_domain)
return;

try{
$builder = new ReflectionClass($builder);
} catch (ReflectionException $e){
Expand Down Expand Up @@ -468,6 +472,18 @@ public function create(string $id, string $builder, array $patterns = ["*"], boo
$this->cache_patterns($id, $patterns);
}

public function mock(string $domain_id, ?string $host = null, bool $use_https = true) : void
{
self::$mocking_domain = true;
$_SERVER['REQUEST_URI'] = "";
$this->index($domain_id);

if($host)
LayConfig::mock_server($host, $use_https);

include_once LayConfig::server_data()->web . "index.php";
}

public function list() : array
{
self::$list_domain_only = true;
Expand Down

0 comments on commit 1919fc1

Please sign in to comment.