This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/app/lib/App/Core/App.php
2022-03-30 08:51:46 +00:00

93 lines
2.1 KiB
PHP

<?php
namespace App\Core;
use \Exception;
/**
* Some framework thingy
*/
class App
{
public string $dir;
public array $config;
public Database $database;
public Session $session;
public User $user;
public function __construct(
string $dir,
array $config,
Database $database,
Session $session,
User $user
)
{
$this->dir = $dir;
$this->config = $config;
$this->database = $database;
$this->session = $session;
$this->user = $user;
}
/**
* Grab model
*
* TODO: have a look to see if this might name conflict with anything and
* maybe also throw an exception if the model class is not found within the file
*/
public function model(string $model, $injection = NULL): object
{
// require model file
$path = $this->dir . '/model/' . $model . '.php';
if (!file_exists($path))
{
throw new Exception("Model does not exist");
}
require $path;
// instantiate model
if (!$injection)
{
$injection = $this->database;
}
return new $model($injection);
}
/**
* Render given view
*/
public function view(string $view, array $data = []): void
{
// import variables into the current symbol table from an array
extract($data);
// require view file
$path = $this->dir . '/view/' . $view . '.php';
if (!file_exists($path))
{
throw new Exception("View does not exist");
}
require $path;
}
/**
* Turn data array into json response
*/
public function api($data, int $status_code = 200): void
{
// set headers
http_response_code($status_code);
header('Content-type: application/json');
// convert and respond with data
echo json_encode($data);
die();
}
/**
* Redirect to given url
*/
public function redirect(string $url): void
{
header("Location: $url");
die();
}
}