Update Framework
This commit is contained in:
parent
06f17dff5b
commit
69f3313f0f
19 changed files with 974 additions and 35 deletions
|
@ -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();
|
||||
}
|
||||
}
|
14
src/Runtime/Contracts/Container/Container.php
Normal file
14
src/Runtime/Contracts/Container/Container.php
Normal 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 = []);
|
||||
|
||||
}
|
9
src/Runtime/Contracts/View/Factory.php
Normal file
9
src/Runtime/Contracts/View/Factory.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Runtime\Contracts\View;
|
||||
|
||||
interface Factory {
|
||||
|
||||
public function make($name, $arguments = []);
|
||||
|
||||
}
|
3
src/Runtime/Exceptions/ClassNotFoundException.php
Normal file
3
src/Runtime/Exceptions/ClassNotFoundException.php
Normal file
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
|
||||
class ClassNotFoundException extends \Exception {}
|
18
src/Runtime/Exceptions/ExceptionHandler.php
Normal file
18
src/Runtime/Exceptions/ExceptionHandler.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
47
src/Runtime/Factory/BootstrapFactory.php
Normal file
47
src/Runtime/Factory/BootstrapFactory.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
35
src/Runtime/Helpers/helpers.php
Normal file
35
src/Runtime/Helpers/helpers.php
Normal 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);
|
||||
}
|
||||
}
|
95
src/Runtime/Http/Views/Factory.php
Normal file
95
src/Runtime/Http/Views/Factory.php
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue