Compare commits
No commits in common. "6eacc7f52320115b089ab3b6d79ec1099561365b" and "ab5fa6d69e6a0467337433050f2e8b31aeb3d807" have entirely different histories.
6eacc7f523
...
ab5fa6d69e
18 changed files with 70 additions and 280 deletions
|
@ -1,14 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
class Application
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function bootstrap(): void
|
||||
{
|
||||
// init application
|
||||
}
|
||||
}
|
|
@ -3,8 +3,8 @@
|
|||
namespace App\Controllers\Api;
|
||||
|
||||
use App\Services\Subnet;
|
||||
use Core\Http\Controllers\Controller;
|
||||
use Core\Http\View\Engine;
|
||||
use Core\Controllers\Controller;
|
||||
use Core\View\Render;
|
||||
use Exception;
|
||||
|
||||
class SubnetController extends Controller
|
||||
|
@ -12,7 +12,7 @@ class SubnetController extends Controller
|
|||
/**
|
||||
* Get all subnet data
|
||||
*
|
||||
* @return \Core\Http\View\Render
|
||||
* @return \Core\View\Render
|
||||
*/
|
||||
public function data(): Render
|
||||
{
|
||||
|
|
|
@ -2,15 +2,15 @@
|
|||
|
||||
namespace App\Controllers;
|
||||
|
||||
use Core\Http\Controllers\Controller;
|
||||
use Core\Http\View\Engine;
|
||||
use Core\Controllers\Controller;
|
||||
use Core\View\Render;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Render index
|
||||
*
|
||||
* @return \Core\Http\View\Render
|
||||
* @return \Core\View\Render
|
||||
*/
|
||||
public function index(): Render
|
||||
{
|
||||
|
|
|
@ -2,15 +2,15 @@
|
|||
|
||||
namespace App\Controllers;
|
||||
|
||||
use Core\Http\Controllers\Controller;
|
||||
use Core\Http\View\Engine;
|
||||
use Core\Controllers\Controller;
|
||||
use Core\View\Render;
|
||||
|
||||
class TestController extends Controller
|
||||
{
|
||||
/**
|
||||
* Render index
|
||||
*
|
||||
* @return \Core\Http\View\Render
|
||||
* @return \Core\View\Render
|
||||
*/
|
||||
public function test(int $id): Render
|
||||
{
|
||||
|
|
|
@ -20,11 +20,7 @@
|
|||
"psr-4": {
|
||||
"App\\": "app/",
|
||||
"Core\\": "src/"
|
||||
},
|
||||
"files": [
|
||||
"src/Helpers/helpers.php"
|
||||
]
|
||||
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"optimize-autoloader": true
|
||||
|
|
|
@ -1,8 +1,15 @@
|
|||
<?php
|
||||
|
||||
use Core\Bootstrap;
|
||||
use Core\Env\Env;
|
||||
use Core\Routing\Router;
|
||||
|
||||
require '../vendor/autoload.php';
|
||||
|
||||
$bootstrap = Bootstrap::createInstance();
|
||||
$bootstrap->handle();
|
||||
// Load routes
|
||||
require '../config/routes.php';
|
||||
|
||||
// Load env
|
||||
Env::load();
|
||||
|
||||
// Dispatch router
|
||||
Router::dispatch();
|
|
@ -1,45 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Core;
|
||||
|
||||
use Core\Factory\BootstrapFactory;
|
||||
|
||||
class Bootstrap {
|
||||
|
||||
/**
|
||||
* @var \Core\Factory\BootstrapFactory
|
||||
*/
|
||||
private static BootstrapFactory $instance;
|
||||
|
||||
/**
|
||||
* Start handling process
|
||||
*
|
||||
* @return \Core\Factory\BootstrapFactory
|
||||
*/
|
||||
public static function createInstance(): BootstrapFactory
|
||||
{
|
||||
self::$instance = (new Bootstrap())->run();
|
||||
|
||||
return Bootstrap::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance
|
||||
*
|
||||
* @return \Core\Factory\BootstrapFactory
|
||||
*/
|
||||
public static function getInstance(): BootstrapFactory
|
||||
{
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new factory instance
|
||||
*
|
||||
* @return \Core\Factory\BootstrapFactory
|
||||
*/
|
||||
protected function run(): BootstrapFactory
|
||||
{
|
||||
return new BootstrapFactory();
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Core\Http\Controllers;
|
||||
namespace Core\Controllers;
|
||||
|
||||
use Core\Http\Request;
|
||||
use Core\Http\Response;
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Core\Exceptions;
|
||||
|
||||
use Core\Env\Env;
|
||||
use Core\Http\Request;
|
||||
use Throwable;
|
||||
use Whoops\Handler\Handler;
|
||||
|
@ -10,7 +11,7 @@ use Whoops\Handler\PlainTextHandler;
|
|||
use Whoops\Handler\PrettyPageHandler;
|
||||
use Whoops\Run as Whoops;
|
||||
|
||||
class ExceptionHandler
|
||||
class Exceptions
|
||||
{
|
||||
/**
|
||||
* Exceptions handler instance
|
||||
|
@ -40,12 +41,13 @@ class ExceptionHandler
|
|||
/**
|
||||
* Get correct handler
|
||||
*
|
||||
* @param \Core\Http\Request|null $request
|
||||
* @return \Whoops\Handler\Handler
|
||||
*/
|
||||
private static function handler(): Handler
|
||||
private static function handler(Request|null $request): Handler
|
||||
{
|
||||
if (env('debug')) {
|
||||
if (request()?->is('post')) {
|
||||
if (Env::get('debug')) {
|
||||
if ($request?->is('post')) {
|
||||
return new JsonResponseHandler();
|
||||
}
|
||||
|
||||
|
@ -58,44 +60,24 @@ class ExceptionHandler
|
|||
/**
|
||||
* Catch all exceptions
|
||||
*
|
||||
* @param \Core\Http\Request $request
|
||||
* @return void
|
||||
*/
|
||||
public static function catch(): void
|
||||
public static function catch(Request $request): void
|
||||
{
|
||||
self::instance()->register();
|
||||
self::instance($request)->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Catch single exception
|
||||
*
|
||||
* @param \Throwable $exception
|
||||
* @return never
|
||||
* @return void
|
||||
*/
|
||||
public static function catchOne(Throwable $exception): never
|
||||
public static function catchOne(Throwable $exception): void
|
||||
{
|
||||
self::instance()->handleException($exception);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make new exception
|
||||
*
|
||||
* @param null $abstract
|
||||
* @param string|null $message
|
||||
* @return never
|
||||
*/
|
||||
public static function make(mixed $abstract = null, string|null $message = null): never
|
||||
{
|
||||
if(is_string($abstract)) {
|
||||
$abstract = app()->make($abstract, $message);
|
||||
}
|
||||
|
||||
if(is_subclass_of($abstract, 'Exception')) {
|
||||
self::catchOne($abstract);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Core\Exceptions\Exceptions;
|
||||
|
||||
class ClassNotFoundException extends \Exception {}
|
|
@ -1,94 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Core\Factory;
|
||||
|
||||
use App\Application;
|
||||
use Core\Env\Env;
|
||||
use Core\Exceptions\ExceptionHandler;
|
||||
use Core\Exceptions\Exceptions\ClassNotFoundException;
|
||||
use Core\Http\Request;
|
||||
use Core\Routing\RouteCollection;
|
||||
use Core\Routing\RouteDispatcher;
|
||||
|
||||
class BootstrapFactory
|
||||
{
|
||||
private Request $request;
|
||||
|
||||
/**
|
||||
* Handle Http core
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
// Load env
|
||||
Env::load();
|
||||
|
||||
// Create request
|
||||
$this->request = app()->make(Request::class, [$_POST + $_FILES]);
|
||||
|
||||
// Capture all exceptions
|
||||
ExceptionHandler::catch();
|
||||
|
||||
// Load routes
|
||||
require '../config/routes.php';
|
||||
|
||||
try {
|
||||
// Boot application
|
||||
app()->make(Application::class)->bootstrap();
|
||||
|
||||
// Dispatch router
|
||||
app()->make(RouteDispatcher::class)->dispatch($this->request, RouteCollection::retrieve());
|
||||
} catch (\Exception $e) {
|
||||
ExceptionHandler::catchOne($e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $abstract
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function make(string $abstract, mixed $arguments = []): mixed
|
||||
{
|
||||
return $this->resolve($abstract, $arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $abstract
|
||||
* @param array $arguments
|
||||
* @return mixed|null
|
||||
*/
|
||||
private function resolve(string $abstract, mixed $arguments = []): mixed
|
||||
{
|
||||
if (class_exists($abstract)) {
|
||||
try {
|
||||
$reflection = new \ReflectionClass($abstract);
|
||||
return $reflection->newInstanceArgs($arguments);
|
||||
} catch (\ReflectionException $e) {
|
||||
ExceptionHandler::catchOne($e);
|
||||
}
|
||||
}
|
||||
|
||||
ExceptionHandler::make(ClassNotFoundException::class, sprintf("Class '%s' not found", $abstract));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the request instance
|
||||
*
|
||||
* @return \Core\Http\Request
|
||||
*/
|
||||
public function request(): Request
|
||||
{
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get path to file/folder in resources folder
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public function resourcePath(string $path = ''): string
|
||||
{
|
||||
return "../resources/" . $path;
|
||||
}
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Core\Bootstrap;
|
||||
use Core\Env\Env;
|
||||
use Core\Http\Request;
|
||||
|
||||
|
||||
if(!function_exists('app'))
|
||||
{
|
||||
/**
|
||||
* Make abstract of instance or get application instance
|
||||
*
|
||||
* @param mixed $abstract
|
||||
* @param array $arguments
|
||||
* @return \Core\Factory\BootstrapFactory|mixed
|
||||
*/
|
||||
function app(mixed $abstract = null, array $arguments = []): mixed
|
||||
{
|
||||
if (is_null($abstract)) {
|
||||
return Bootstrap::getInstance();
|
||||
}
|
||||
|
||||
return Bootstrap::getInstance()->make($abstract, $arguments);
|
||||
}
|
||||
}
|
||||
|
||||
if(!function_exists('env'))
|
||||
{
|
||||
/**
|
||||
* Get env variable
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
function env(string $key): mixed
|
||||
{
|
||||
return Env::get($key);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('request')) {
|
||||
/**
|
||||
* Get the request instance
|
||||
*
|
||||
* @return \Core\Http\Request
|
||||
*/
|
||||
function request(): Request
|
||||
{
|
||||
return app()->request();
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Core\Http;
|
||||
|
||||
use Core\Exceptions\Exceptions;
|
||||
|
||||
class Request
|
||||
{
|
||||
private array $data = [];
|
||||
|
@ -14,6 +16,9 @@ class Request
|
|||
public function __construct(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
// Capture all exceptions
|
||||
Exceptions::catch($this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
namespace Core\Http;
|
||||
|
||||
use Core\Http\View\Engine\HtmlEngine;
|
||||
use Core\Http\View\Engine\JsonEngine;
|
||||
use Core\Http\View\Render;
|
||||
use Core\View\Render;
|
||||
use Core\View\Render\HtmlRender;
|
||||
use Core\View\Render\JsonRender;
|
||||
|
||||
class Response
|
||||
{
|
||||
|
@ -25,20 +25,20 @@ class Response
|
|||
* Render HTML
|
||||
*
|
||||
* @param string $view
|
||||
* @return \Core\Http\View\Render
|
||||
* @return \Core\View\Render
|
||||
*/
|
||||
public function view(string $view): Render
|
||||
{
|
||||
return (new HtmlEngine())->view($view);
|
||||
return (new HtmlRender())->view($view);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render JSON
|
||||
*
|
||||
* @return \Core\Http\View\Render
|
||||
* @return \Core\View\Render
|
||||
*/
|
||||
public function json(): Render
|
||||
{
|
||||
return new JsonEngine();
|
||||
return new JsonRender();
|
||||
}
|
||||
}
|
|
@ -2,10 +2,11 @@
|
|||
|
||||
namespace Core\Routing;
|
||||
|
||||
use Core\Exceptions\ExceptionHandler;
|
||||
use Core\Env\Env;
|
||||
use Core\Exceptions\Exceptions;
|
||||
use Core\Exceptions\Exceptions\NotFoundHttpException;
|
||||
use Core\Http\Request;
|
||||
use Core\Http\View\Engine;
|
||||
use Core\View\Render;
|
||||
use Exception;
|
||||
|
||||
class RouteDispatcher
|
||||
|
@ -25,13 +26,10 @@ class RouteDispatcher
|
|||
private array $routeCollection;
|
||||
|
||||
/**
|
||||
* Dispatch the router dispatcher
|
||||
*
|
||||
* @param \Core\Http\Request $request
|
||||
* @param array $routeCollection
|
||||
* @return void
|
||||
*/
|
||||
public function dispatch(Request $request, array $routeCollection): void
|
||||
public function __construct(Request $request, array $routeCollection)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->routeCollection = $routeCollection;
|
||||
|
@ -50,6 +48,18 @@ class RouteDispatcher
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch the router dispatcher
|
||||
*
|
||||
* @param \Core\Http\Request $request
|
||||
* @param array $routeCollection
|
||||
* @return void
|
||||
*/
|
||||
public static function dispatch(Request $request, array $routeCollection): void
|
||||
{
|
||||
new self($request, $routeCollection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate a matching route for the incoming request.
|
||||
*
|
||||
|
@ -143,8 +153,8 @@ class RouteDispatcher
|
|||
*/
|
||||
private function handleException(Exception $e, int $statusCode, string $message): void
|
||||
{
|
||||
if (env('debug')) {
|
||||
ExceptionHandler::catchOne($e);
|
||||
if (Env::get('debug')) {
|
||||
Exceptions::catchOne($e);
|
||||
}
|
||||
|
||||
http_response_code($statusCode);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Core\Http\View;
|
||||
namespace Core\View;
|
||||
|
||||
abstract class Render
|
||||
{
|
|
@ -1,10 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Core\Http\View\Engine;
|
||||
namespace Core\View\Render;
|
||||
|
||||
use Core\Http\View\Render;
|
||||
use Core\View\Render;
|
||||
|
||||
class HtmlEngine extends Render
|
||||
class HtmlRender extends Render
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
|
@ -13,7 +13,7 @@ class HtmlEngine extends Render
|
|||
public function render(): void
|
||||
{
|
||||
$basePath = $_SERVER['DOCUMENT_ROOT'];
|
||||
$viewsPath = app()->resourcePath('views/' . str_replace('.', '/', $this->view) . '.php');
|
||||
$viewsPath = $basePath . '/../resources/views/' . str_replace('.', '/', $this->view) . '.php';
|
||||
|
||||
if (file_exists($viewsPath)) {
|
||||
extract($this->data);
|
|
@ -1,11 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Core\Http\View\Engine;
|
||||
namespace Core\View\Render;
|
||||
|
||||
use Core\View\Render;
|
||||
|
||||
use Core\Http\View\Render;
|
||||
|
||||
class JsonEngine extends Render
|
||||
class JsonRender extends Render
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
Loading…
Add table
Add a link
Reference in a new issue