84 lines
1.8 KiB
PHP
84 lines
1.8 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_name): object
|
|
{
|
|
// require model file
|
|
$path = $this->dir . '/model/' . $model_name . '.php';
|
|
if (!file_exists($path))
|
|
{
|
|
throw new Exception("Model does not exist");
|
|
}
|
|
require $path;
|
|
return new $model_name($this->database);
|
|
}
|
|
|
|
/**
|
|
* Render given view
|
|
*/
|
|
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();
|
|
}
|
|
} |