2022-01-15 10:26:02 +00:00
|
|
|
<?php
|
|
|
|
|
2022-01-19 18:50:54 +00:00
|
|
|
class App
|
2022-01-15 10:26:02 +00:00
|
|
|
{
|
2022-01-23 21:56:36 +00:00
|
|
|
public array $config;
|
|
|
|
public Database $database;
|
|
|
|
public Session $session;
|
|
|
|
public User $user;
|
2022-01-20 21:23:21 +00:00
|
|
|
|
2022-01-23 21:56:36 +00:00
|
|
|
public function __construct(
|
|
|
|
array $config,
|
|
|
|
Database $database,
|
|
|
|
Session $session,
|
|
|
|
User $user
|
|
|
|
)
|
2022-01-19 18:50:54 +00:00
|
|
|
{
|
2022-01-23 21:56:36 +00:00
|
|
|
$this->config = $config;
|
2022-01-21 20:32:21 +00:00
|
|
|
$this->database = $database;
|
2022-01-23 21:56:36 +00:00
|
|
|
$this->session = $session;
|
|
|
|
$this->user = $user;
|
2022-01-19 18:50:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 19:16:09 +00:00
|
|
|
// Grab model
|
2022-01-24 09:18:18 +00:00
|
|
|
public function model(string $model, $injection = NULL): object
|
2022-01-15 10:26:02 +00:00
|
|
|
{
|
|
|
|
// Require model file
|
|
|
|
require __DIR__ . '/../model/' . $model . '.php';
|
|
|
|
// Instantiate model
|
2022-01-24 11:55:37 +00:00
|
|
|
if (!$injection)
|
|
|
|
{
|
2022-01-24 09:18:18 +00:00
|
|
|
$injection = $this->database;
|
|
|
|
}
|
|
|
|
return new $model($injection);
|
2022-01-15 10:26:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 19:16:09 +00:00
|
|
|
// Render given view
|
2022-01-15 10:26:02 +00:00
|
|
|
public function view(string $view, array $data = []): void
|
|
|
|
{
|
|
|
|
// Import variables into the current symbol table from an array
|
|
|
|
extract($data);
|
|
|
|
// Require view file
|
|
|
|
require __DIR__ . '/../view/' . $view . '.php';
|
|
|
|
}
|
2022-01-19 19:16:09 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2022-01-23 21:56:36 +00:00
|
|
|
|
|
|
|
// Redirect to given url
|
|
|
|
public function redirect(string $url): void
|
|
|
|
{
|
|
|
|
header("Location: $url");
|
|
|
|
die();
|
|
|
|
}
|
2022-01-15 10:26:02 +00:00
|
|
|
}
|