Skip to content

Commit

Permalink
Services: migrate absoluteURL declaration
Browse files Browse the repository at this point in the history
* Move absoluteURL declaration into CoreServiceProvider.
  • Loading branch information
yookoala committed Nov 7, 2023
1 parent c8d50c3 commit 1e7168d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
22 changes: 3 additions & 19 deletions gibbon.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
$session = $container->get('session');
$gibbon->session = $session;

// Setup global absoluteURL for all urls.
Url::setBaseURL($container->get('absoluteURL'));

// Handle Gibbon installation redirect
if (!$gibbon->isInstalled() && !$gibbon->isInstalling()) {
header("Location: ./installer/install.php");
Expand Down Expand Up @@ -102,25 +105,6 @@
}
}

// Setup global absoluteURL for all urls.
if ($gibbon->isInstalled() && $session->has('absoluteURL')) {
Url::setBaseUrl($session->get('absoluteURL'));
} else {
// TODO: put this absoluteURL detection somewhere?
$absoluteURL = (function () {
// Find out the base installation URL path.
$prefixLength = strlen(realpath($_SERVER['DOCUMENT_ROOT']));
$baseDir = realpath(__DIR__) . '/';
$urlBasePath = substr($baseDir, $prefixLength);

// Construct the full URL to the base URL path.
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
return "{$protocol}://{$host}{$urlBasePath}";
})();
Url::setBaseUrl($absoluteURL);
}

// Autoload the current module namespace
if (!empty($session->get('module'))) {
$moduleNamespace = preg_replace('/[^a-zA-Z0-9]/', '', $session->get('module'));
Expand Down
37 changes: 28 additions & 9 deletions src/Services/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public function boot()
return !empty($hasSessionTable);
}, false);

// Define how session can be created.
$container->share('session', function () {
return $this->getContainer()->get(SessionInterface::class);
});
$container->share(SessionInterface::class, function () {
$container = $this->getContainer();
$sessionTableHasSetup = $container->has('sessionTableHasSetup') && $container->get('sessionTableHasSetup');
return SessionFactory::create($this->getContainer(), $sessionTableHasSetup);
});

// Define locale object.
$container->share('locale', function () {
// Get dependencies.
Expand All @@ -155,6 +165,24 @@ public function boot()
}
return $locale;
});

// Setup global absoluteURL of all URLs.
$container->share('absoluteURL', function () use ($core) {
$session = $this->getContainer()->get(SessionInterface::class);
if ($core->isInstalled() && $session->has('absoluteURL')) {
return $session->get('absoluteURL');
}

// Find out the base installation URL path.
$prefixLength = strlen(realpath($_SERVER['DOCUMENT_ROOT']));
$baseDir = realpath(__DIR__ . '/../../') . '/';
$urlBasePath = substr($baseDir, $prefixLength);

// Construct the full URL to the base URL path.
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
return "{$protocol}://{$host}{$urlBasePath}";
});
}

/**
Expand Down Expand Up @@ -182,15 +210,6 @@ public function register()

// $pdo->setLogger($container->get('mysql_logger'));

$container->share('session', function () {
return $this->getContainer()->get(SessionInterface::class);
});
$container->share(SessionInterface::class, function () {
$container = $this->getContainer();
$sessionTableHasSetup = $container->has('sessionTableHasSetup') && $container->get('sessionTableHasSetup');
return SessionFactory::create($this->getContainer(), $sessionTableHasSetup);
});

$container->share('twig', function () use ($absolutePath) {
$session = $this->getLeagueContainer()->get('session');
$loader = new \Twig\Loader\FilesystemLoader($absolutePath.'/resources/templates');
Expand Down

0 comments on commit 1e7168d

Please sign in to comment.