Files

112 lines
4.0 KiB
PHP
Raw Permalink Normal View History

2022-02-04 09:53:55 +00:00
<?php
2022-03-15 21:38:39 +00:00
/**
2022-07-19 16:38:55 +01:00
* ██ ██ ██ ███ ███ ██████
* ██ ██ ██ ████ ████ ██ ██
* ██ ██ ██ ██ ████ ██ ██████
* ██ ██ ██ ██ ██ ██ ██ ██
* █████ ██████ ██ ██ ██
2022-03-15 21:38:39 +00:00
*
2022-07-19 16:38:55 +01:00
* @author Dale Davies <dale@daledavies.co.uk>
* @copyright Copyright (c) 2022, Dale Davies
* @license MIT
2022-03-15 21:38:39 +00:00
*/
2022-02-04 09:53:55 +00:00
namespace Jump;
use \Jump\Pages\ErrorPage;
use \Tracy\Debugger;
2022-02-04 09:53:55 +00:00
class Main {
private Cache $cache;
2022-03-15 21:38:39 +00:00
private Config $config;
private Language $language;
2022-04-13 16:28:12 +01:00
private \Nette\Http\Request $request;
private \Nette\Routing\RouteList $router;
2022-04-13 16:28:12 +01:00
private \Nette\Http\Session $session;
2022-02-04 09:53:55 +00:00
public function __construct() {
// Some initial configuration of Tracy for logging/debugging.
Debugger::$errorTemplate = __DIR__ . '/../templates/errorpage.php';
2023-04-12 22:17:13 +01:00
Debugger::$customCssFiles = [__DIR__ . '/../assets/css/debugger.css'];
Debugger::setLogger(new \Jump\Debugger\ErrorLogger);
Debugger::getBlueScreen()->addPanel(
[\Jump\Debugger\JumpVersionPanel::class, 'panel']
);
Debugger::getBlueScreen()->addPanel(
[\Jump\Debugger\JumpConfigPanel::class, 'panel']
);
$debugmode = Debugger::Development;
// We can't do much without the config object so get that next.
2022-02-04 09:53:55 +00:00
$this->config = new Config();
// Now we have config, enable detailed debugging info as early as possible
// during initialisation
if (!$this->config->parse_bool($this->config->get('debug'))) {
$debugmode = Debugger::Production;
}
// Tell Tracy to handle errors and exceptions.
Debugger::enable($debugmode);
// Carry on setting things up.
2022-02-04 09:53:55 +00:00
$this->cache = new Cache($this->config);
$this->router = new \Nette\Routing\RouteList;
$this->language = new Language($this->config, $this->cache);
2022-02-04 09:53:55 +00:00
2022-03-15 21:38:39 +00:00
// Set up the routes that Jump expects.
$this->router->addRoute('/', [
'class' => 'Jump\Pages\HomePage'
]);
$this->router->addRoute('/tag/<tag>', [
2022-03-15 21:38:39 +00:00
'class' => 'Jump\Pages\TagPage'
]);
$this->router->addRoute('/api/icon?siteid=<siteid>', [
2022-07-18 14:29:49 +01:00
'class' => 'Jump\API\Icon'
]);
2022-07-25 12:55:53 +01:00
$this->router->addRoute('/api/status[/<token>]', [
'class' => 'Jump\API\Status'
]);
2022-07-18 14:29:49 +01:00
$this->router->addRoute('/api/unsplash[/<token>]', [
'class' => 'Jump\API\Unsplash'
]);
$this->router->addRoute('/api/weather[/<token>[/<lat>[/<lon>]]]', [
'class' => 'Jump\API\Weather'
]);
2022-02-04 09:53:55 +00:00
}
public function init() {
2022-04-13 16:28:12 +01:00
// Create a request object based on globals so we can utilise url rewriting etc.
$this->request = (new \Nette\Http\RequestFactory)->fromGlobals();
// Initialise a new session using the request object.
$this->session = new \Nette\Http\Session($this->request, new \Nette\Http\Response);
$this->session->setName($this->config->get('sessionname'));
$this->session->setExpiration($this->config->get('sessiontimeout'));
2022-03-15 21:38:39 +00:00
// Try to match the correct route based on the HTTP request.
2022-04-13 16:28:12 +01:00
$matchedroute = $this->router->match($this->request);
2022-02-04 09:53:55 +00:00
2022-03-15 21:38:39 +00:00
// If we do not have a matched route then just serve up the home page.
$outputclass = $matchedroute['class'] ?? 'Jump\Pages\HomePage';
2022-02-04 09:53:55 +00:00
2022-03-15 21:38:39 +00:00
// Instantiate the correct class to build the requested page, get the
// content and return it.
$page = new $outputclass($this->config, $this->cache, $this->session, $this->language, $matchedroute ?? null);
2022-03-15 21:38:39 +00:00
return $page->get_output();
2022-02-04 09:53:55 +00:00
}
/**
* Global exception handler, display friendly message if something goes wrong.
*
* @param $exception
* @return void
*/
public function exception_handler($exception): void {
error_log($exception->getMessage());
ErrorPage::display($this->config, 500, 'Something went wrong, please use debug option to see details.');
}
2022-02-04 09:53:55 +00:00
}