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

93 lines
2.1 KiB
PHP
Raw Normal View History

2022-01-15 10:26:02 +00:00
<?php
2022-03-02 06:15:12 +00:00
namespace App\Core;
use \Exception;
/**
* Some framework thingy
*/
2022-01-19 18:50:54 +00:00
class App
2022-01-15 10:26:02 +00:00
{
2022-03-02 06:15:12 +00:00
public string $dir;
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(
2022-03-02 06:15:12 +00:00
string $dir,
2022-01-23 21:56:36 +00:00
array $config,
Database $database,
Session $session,
User $user
)
2022-01-19 18:50:54 +00:00
{
2022-03-02 06:15:12 +00:00
$this->dir = $dir;
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-03-07 06:13:17 +00:00
/**
* 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
*/
2022-01-24 09:18:18 +00:00
public function model(string $model, $injection = NULL): object
2022-01-15 10:26:02 +00:00
{
2022-03-07 06:13:17 +00:00
// require model file
2022-03-02 06:15:12 +00:00
$path = $this->dir . '/model/' . $model . '.php';
2022-02-08 23:02:11 +00:00
if (!file_exists($path))
{
throw new Exception("Model does not exist");
}
require $path;
2022-03-07 06:13:17 +00:00
// 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-03-03 04:11:14 +00:00
/**
* Render given view
*/
2022-01-15 10:26:02 +00:00
public function view(string $view, array $data = []): void
{
2022-03-03 04:11:14 +00:00
// import variables into the current symbol table from an array
2022-01-15 10:26:02 +00:00
extract($data);
2022-03-03 04:11:14 +00:00
// require view file
2022-03-02 06:15:12 +00:00
$path = $this->dir . '/view/' . $view . '.php';
2022-02-08 23:02:11 +00:00
if (!file_exists($path))
{
throw new Exception("View does not exist");
}
require $path;
2022-01-15 10:26:02 +00:00
}
2022-01-19 19:16:09 +00:00
2022-03-03 04:11:14 +00:00
/**
2022-04-13 19:22:21 +00:00
* Convert data into json response
2022-03-03 04:11:14 +00:00
*/
2022-04-13 19:22:21 +00:00
public function api(mixed $data, int $status_code = 200): void
2022-01-19 19:16:09 +00:00
{
2022-03-07 06:13:17 +00:00
// set headers
2022-01-19 19:16:09 +00:00
http_response_code($status_code);
header('Content-type: application/json');
2022-03-07 06:13:17 +00:00
// convert and respond with data
2022-01-19 19:16:09 +00:00
echo json_encode($data);
2022-02-08 21:31:43 +00:00
die();
2022-01-19 19:16:09 +00:00
}
2022-01-23 21:56:36 +00:00
2022-03-03 04:11:14 +00:00
/**
* Redirect to given url
*/
2022-01-23 21:56:36 +00:00
public function redirect(string $url): void
{
header("Location: $url");
die();
}
2022-01-15 10:26:02 +00:00
}