026e31d037
Reviewed-on: #1
56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?php
|
|
|
|
if (!function_exists('api_url_for_browser')) {
|
|
/**
|
|
* Base URL for JavaScript in the browser.
|
|
* API_URL may be a Docker-only hostname (api_arrahn-app-1). Browsers use API_BROWSER_URL.
|
|
* Docker dev often uses host.docker.internal for PHP→host; browsers may not resolve it.
|
|
* Production URLs are unchanged when API_BROWSER_URL is empty.
|
|
*/
|
|
function api_url_for_browser(): string
|
|
{
|
|
$browser = rtrim((string) config('api.browser_url'), '/');
|
|
if ($browser !== '') {
|
|
return $browser;
|
|
}
|
|
|
|
$url = rtrim((string) config('api.url'), '/');
|
|
|
|
return str_ireplace('host.docker.internal', '127.0.0.1', $url);
|
|
}
|
|
}
|
|
|
|
if (!function_exists('api_url_for_server')) {
|
|
/**
|
|
* Base URL for PHP Http::get/put from this container.
|
|
* Vue posts MIX_API_URL (localhost:8080) as "hostname"; that is for the browser.
|
|
* Inside Docker, localhost is this container — map it to API_URL.
|
|
*/
|
|
function api_url_for_server(?string $url = null): string
|
|
{
|
|
$internal = rtrim((string) config('api.url'), '/');
|
|
|
|
if ($url === null || $url === '') {
|
|
return $internal;
|
|
}
|
|
|
|
$url = rtrim($url, '/');
|
|
$browser = rtrim((string) config('api.browser_url'), '/');
|
|
|
|
if ($internal === '') {
|
|
return $url;
|
|
}
|
|
|
|
if ($browser !== '' && strcasecmp($url, $browser) === 0) {
|
|
return $internal;
|
|
}
|
|
|
|
$host = parse_url($url, PHP_URL_HOST);
|
|
if (in_array($host, ['localhost', '127.0.0.1', 'host.docker.internal'], true)) {
|
|
return $internal;
|
|
}
|
|
|
|
return $url;
|
|
}
|
|
}
|