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

82 lines
1.9 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
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(array $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();
}
}