Update Framework

This commit is contained in:
Maarten 2019-12-23 18:55:25 +01:00
parent 06f17dff5b
commit 69f3313f0f
19 changed files with 974 additions and 35 deletions

View file

@ -2,27 +2,40 @@
namespace Runtime;
use App\Http\Controllers\BaseController;
use Runtime\Factory\BootstrapFactory;
class Bootstrap {
/**
* @var BootstrapFactory
*/
private static $instance;
/**
* Start handling process
*
* @return string
* @return BootstrapFactory
*/
public static function handle() {
return (new Bootstrap())->run();
}
private $base;
public function __construct()
public static function createInstance()
{
$this->base = new BaseController();
self::$instance = (new Bootstrap())->run();
return Bootstrap::getInstance();
}
protected function run() {
return $this->base->test();
/**
* @return BootstrapFactory
*/
public static function getInstance()
{
return self::$instance;
}
/**
* @return BootstrapFactory
*/
protected function run()
{
return new BootstrapFactory();
}
}

View file

@ -0,0 +1,14 @@
<?php
interface Container {
/**
* Resolve the given type from the container.
*
* @param string $abstract
* @param array $parameters
* @return mixed
*/
public function make($abstract, array $parameters = []);
}

View file

@ -0,0 +1,9 @@
<?php
namespace Runtime\Contracts\View;
interface Factory {
public function make($name, $arguments = []);
}

View file

@ -0,0 +1,3 @@
<?php
class ClassNotFoundException extends \Exception {}

View file

@ -0,0 +1,18 @@
<?php
namespace Runtime\Exceptions;
class ExceptionHandler {
public static function make($abstract = null)
{
if(is_object($abstract) && $abstract instanceof \Exception) {
echo $abstract->getMessage();
die();
}
echo 'Error: ' . $abstract;
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace Runtime\Factory;
use ClassNotFoundException;
use Pecee\SimpleRouter\SimpleRouter;
use Runtime\Exceptions\ExceptionHandler;
class BootstrapFactory {
public function handle()
{
SimpleRouter::setDefaultNamespace('\App\Http\Controllers');
require_once('../routes/web.php ');
try {
SimpleRouter::start();
}
catch (\Exception $e) {
ExceptionHandler::make($e);
}
}
/**
* @param $abstract
* @return mixed
*/
public function make($abstract)
{
return $this->resolve($abstract);
}
/**
* @param $abstract
* @return mixed|null
*/
private function resolve($abstract) {
if(class_exists($abstract)) {
return new $abstract;
}
ExceptionHandler::make(ClassNotFoundException::class);
return null;
}
}

View file

@ -0,0 +1,35 @@
<?php
use Runtime\Bootstrap;
use Runtime\Http\Views\Factory as ViewFactory;
if(!function_exists('app'))
{
/**
* @param null $abstract
* @return mixed|Container
*/
function app($abstract = null)
{
if (is_null($abstract)) {
Bootstrap::getInstance();
}
return Bootstrap::getInstance()->make($abstract);
}
}
if(!function_exists('view'))
{
/**
* @param $name
* @param array $arguments
* @return mixed
*/
function view($name, $arguments = [])
{
$factory = app(ViewFactory::class);
return $factory->make($name, $arguments);
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace Runtime\Http\Views;
use ClassNotFoundException;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
class Factory implements \Runtime\Contracts\View\Factory {
/**
* @var string
*/
private $resourcesDir = '/../resources/';
/**
* @var string
*/
private $name;
/**
* @var array
*/
private $arguments;
/**
* @var \Twig\Environment
*/
private $twig;
/**
* Factory constructor.
*/
public function __construct()
{
$loader = new FilesystemLoader(getcwd() . $this->resourcesDir . 'views');
$this->twig = new Environment($loader, [
'cache' => $this->resourcesDir . 'cache'
]);
}
/**
* @param $name
* @param array $arguments
*/
public function make($name, $arguments = [])
{
$this->name = $this->name($name);
$this->arguments = $arguments;
if(!$this->exists()) {
throw new ClassNotFoundException();
}
$this->render();
//TODO: Catch errors
}
/**
* @return bool
*/
private function exists()
{
$file = getcwd() . "/../resources/views/{$this->name}.html";
//TODO: Add checker
return true;
}
/**
* @param $name
* @param $arguments
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
private function render()
{
echo $this->twig->render($this->name . '.html', $this->arguments);
}
/**
* @param $name
* @return string|string[]
*/
private function name($name)
{
return str_replace('.', '/', $name);
}
}