Skip to content

Commit

Permalink
[ENH] setup application server domain name and protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
bim-g committed Jan 5, 2024
1 parent e08c157 commit 5989d1f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
58 changes: 47 additions & 11 deletions config/constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,58 @@
* they declare as global tho to be accessible from anywhere in the project
*/

//web root configuration
/**
* web app file path
*/
define('WEB_ROOT', str_replace('index.php', '', $_SERVER['SCRIPT_NAME']));
/**
* os system absolute file path
*/
define('ROOT', str_replace('index.php', '', $_SERVER['SCRIPT_FILENAME']));

/**
* Get App domain
* define default domain
*/
$server_name = $_SERVER['SERVER_NAME'] ?? 'wepesi.com';
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? strtolower(explode('/', $_SERVER['SERVER_PROTOCOL'])[0]) : 'http';
$domain = $_SERVER['REMOTE_ADDR'] == '::1' ? "$protocol://$server_name" : $server_name;
define('DEFAULT_DOMAIN', "$protocol://$server_name");
define('APP_DOMAIN', $domain);
/**
* Get host domain ip address
* @return string
*/
function getDomainIP(): string
{
$ip = $_SERVER['REMOTE_ADDR'];

// default timezone
const TIMEZONE = 'Africa/Kigali';
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif ($ip == '::1') {
$ip = gethostbyname(getHostName());
}
return $ip;
}

// define in witch cycle are you are working on.
// in case you are in dev, indexing file will not be generated, but in prod fase, it will be generated
define('APP_DEV', true);
/**
* Get server information's
* @return object
*/
function serverDomain(): object
{
$server_name = $_SERVER['SERVER_NAME'];
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? strtolower(explode('/', $_SERVER['SERVER_PROTOCOL'])[0]) : 'http';
$domain = getDomainIp() === '127.0.0.1' ? "$protocol://$server_name" : $server_name;
return (object)[
'server_name' => $server_name,
'protocol' => $protocol,
'domain' => $domain
];
}

/**
* Define default domain
*/
define('DEFAULT_DOMAIN', serverDomain()->protocol . "://" . serverDomain()->server_name);
/**
* Define Application host domain
*/
define('APP_DOMAIN', serverDomain()->domain);
39 changes: 3 additions & 36 deletions src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Wepesi\Core\Routing\Router;

/**
*
* Application root
*/
class Application
{
Expand Down Expand Up @@ -55,47 +55,14 @@ public function __construct(string $path, AppConfiguration $config)
{

self::$ROOT_DIR = str_replace("\\", '/', $path);
self::$APP_DOMAIN = $this->domainSetup()->app_domain;
self::$APP_DOMAIN = serverDomain()->domain;
self::$params = $config->generate();
self::$APP_TEMPLATE = self::$params['app_template'] ?? null;
self::$APP_LANG = self::$params['lang'] ?? 'fr';
$this->router = new Router();
self::$LAYOUT_CONTENT = 'layout_content';
}

/**
* @return object
*/
private function domainSetup(): object
{
$server_name = $_SERVER['SERVER_NAME'];
$protocol = strtolower(explode('/', $_SERVER['SERVER_PROTOCOL'])[0]);
$domain = self::getDomainIp() === '127.0.0.1' ? "$protocol://$server_name" : $server_name;
return (object)[
'server_name' => $server_name,
'protocol' => $protocol,
'app_domain' => $domain,
];
}

/**
* use method to get domain ip
* @return string
*/
public static function getDomainIp(): string
{
$ip = $_SERVER['REMOTE_ADDR'];

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif ($ip == '::1') {
$ip = gethostbyname(getHostName());
}
return $ip;
}

/**
* simple builtin dumper for dump data
* @param $ex
Expand Down Expand Up @@ -134,4 +101,4 @@ public function run()
{
$this->router->run();
}
}
}

0 comments on commit 5989d1f

Please sign in to comment.