stela/index.php

113 lines
2.7 KiB
PHP
Raw Normal View History

2020-01-23 12:10:33 +00:00
<?php
// see app.php for application logic
2020-01-23 14:17:37 +00:00
if (!file_exists('config.php')) die('APP not configured');
$config = require 'config.php';
2020-01-23 12:10:33 +00:00
// AUTOLOADING
spl_autoload_register(function ($class){
require __DIR__ . '/lib/' . str_replace('\\', '/', $class) . '.php';
});
// ERRORS
require __DIR__ . '/lib/tracy.php';
use \Tracy\Debugger;
Debugger::enable(!empty($config['show_debug']) ? Debugger::DEVELOPMENT : Debugger::DETECT);
Debugger::$showBar = false;
Debugger::$errorTemplate = __DIR__ . '/tpl/500.htm';
2020-01-23 14:17:37 +00:00
// FRAMEWORK API
2020-01-23 12:10:33 +00:00
$routeCollector = new FastRoute\RouteCollector(new FastRoute\RouteParser\Std(), new FastRoute\DataGenerator\GroupCountBased());
// user
// requireLogin
2020-01-23 14:17:37 +00:00
$dependencies = [];
2020-01-23 12:10:33 +00:00
function di($service)
{
2020-01-23 14:17:37 +00:00
global $dependencies;
if (isset($dependencies[$service])) {
return $dependencies[$service];
} else {
throw new Exception('Dependency ' . $service . ' not found!');
}
2020-01-23 12:10:33 +00:00
}
function flash($msg)
{
// stub
}
function redirect($url, $status = 302)
{
// stub
}
2020-01-23 15:09:58 +00:00
function render($view, $data=[])
2020-01-23 12:10:33 +00:00
{
2020-01-23 12:18:54 +00:00
$tplFile = __DIR__ . '/tpl/' . $view . '.php';
if (file_exists($tplFile)) {
extract($data, EXTR_SKIP);
ob_start();
include $tplFile;
$output = ob_get_contents();
ob_end_clean();
return $output;
} else {
throw new Exception('Template '.$view.' not found in file '.$tplFile);
}
2020-01-23 12:10:33 +00:00
}
function response($body, $status=200, $headers=[])
{
}
function notFound()
{
// ?
}
function route($method, $url, $callback)
{
global $routeCollector;
if (empty($method)) $method = ['GET', 'POST'];
$routeCollector->addRoute($method, $url, $callback);
}
2020-01-23 14:17:37 +00:00
// DEPENDECIES
$dependencies['config'] = $config;
$dependencies['pdo'] = new PDO('sqlite:' . __DIR__ . '/' . $config['database']);
$dependencies['rows'] = new severak\database\rows($dependencies['pdo']);
// ROUTES
2020-01-23 12:10:33 +00:00
require 'app.php';
// finally running the APP
$routeDispatcher = new FastRoute\Dispatcher\GroupCountBased($routeCollector->getData());
2020-01-23 15:09:58 +00:00
$request = new Nyholm\Psr7\ServerRequest($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], getallheaders());
if ($request->getMethod()=='POST') {
$request = $request->withParsedBody($_POST);
}
2020-01-23 12:10:33 +00:00
$routeInfo = $routeDispatcher->dispatch($request->getMethod(), $request->getUri());
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
2020-01-23 15:09:58 +00:00
throw new Exception('Not found!');
2020-01-23 12:10:33 +00:00
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$allowedMethods = $routeInfo[1];
2020-01-23 15:09:58 +00:00
throw new Exception('Method Not Allowed');
2020-01-23 12:10:33 +00:00
break;
case FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1];
$vars = $routeInfo[2];
2020-01-23 15:09:58 +00:00
echo $handler($request, $vars);
2020-01-23 12:10:33 +00:00
break;
2020-01-23 15:09:58 +00:00
}