93 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			93 lines
		
	
	
		
			2.1 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
 | 
						|
     */
 | 
						|
    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;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 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();
 | 
						|
    }
 | 
						|
} |