94 lines
2.2 KiB
PHP
94 lines
2.2 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, mixed $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
|
|
*
|
|
* Reason for the funky names is to avoid name conflicts
|
|
*/
|
|
public function view(string $__VIEW, array $__DATA = []): void
|
|
{
|
|
$__PATH = $this->dir . '/view/' . $__VIEW . '.php';
|
|
if (!file_exists($__PATH))
|
|
{
|
|
throw new Exception("View does not exist");
|
|
}
|
|
// import variables into the current symbol table from an array
|
|
extract($__DATA);
|
|
require $__PATH;
|
|
}
|
|
|
|
/**
|
|
* Convert data into JSON response
|
|
*/
|
|
public function api(mixed $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();
|
|
}
|
|
} |