Compare commits
3 commits
ab5fa6d69e
...
6eacc7f523
Author | SHA1 | Date | |
---|---|---|---|
|
6eacc7f523 | ||
|
591e0c194d | ||
|
83bc1a1661 |
18 changed files with 280 additions and 70 deletions
14
app/Application.php
Normal file
14
app/Application.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?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\Controllers\Controller;
|
||||
use Core\View\Render;
|
||||
use Core\Http\Controllers\Controller;
|
||||
use Core\Http\View\Engine;
|
||||
use Exception;
|
||||
|
||||
class SubnetController extends Controller
|
||||
|
@ -12,7 +12,7 @@ class SubnetController extends Controller
|
|||
/**
|
||||
* Get all subnet data
|
||||
*
|
||||
* @return \Core\View\Render
|
||||
* @return \Core\Http\View\Render
|
||||
*/
|
||||
public function data(): Render
|
||||
{
|
||||
|
|
|
@ -2,15 +2,15 @@
|
|||
|
||||
namespace App\Controllers;
|
||||
|
||||
use Core\Controllers\Controller;
|
||||
use Core\View\Render;
|
||||
use Core\Http\Controllers\Controller;
|
||||
use Core\Http\View\Engine;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Render index
|
||||
*
|
||||
* @return \Core\View\Render
|
||||
* @return \Core\Http\View\Render
|
||||
*/
|
||||
public function index(): Render
|
||||
{
|
||||
|
|
|
@ -2,15 +2,15 @@
|
|||
|
||||
namespace App\Controllers;
|
||||
|
||||
use Core\Controllers\Controller;
|
||||
use Core\View\Render;
|
||||
use Core\Http\Controllers\Controller;
|
||||
use Core\Http\View\Engine;
|
||||
|
||||
class TestController extends Controller
|
||||
{
|
||||
/**
|
||||
* Render index
|
||||
*
|
||||
* @return \Core\View\Render
|
||||
* @return \Core\Http\View\Render
|
||||
*/
|
||||
public function test(int $id): Render
|
||||
{
|
||||
|
|
|
@ -20,7 +20,11 @@
|
|||
"psr-4": {
|
||||
"App\\": "app/",
|
||||
"Core\\": "src/"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"src/Helpers/helpers.php"
|
||||
]
|
||||
|
||||
},
|
||||
"config": {
|
||||
"optimize-autoloader": true
|
||||
|
|
|
@ -1,15 +1,8 @@
|
|||
<?php
|
||||
|
||||
use Core\Env\Env;
|
||||
use Core\Routing\Router;
|
||||
use Core\Bootstrap;
|
||||
|
||||
require '../vendor/autoload.php';
|
||||
|
||||
// Load routes
|
||||
require '../config/routes.php';
|
||||
|
||||
// Load env
|
||||
Env::load();
|
||||
|
||||
// Dispatch router
|
||||
Router::dispatch();
|
||||
$bootstrap = Bootstrap::createInstance();
|
||||
$bootstrap->handle();
|
45
src/Bootstrap.php
Normal file
45
src/Bootstrap.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?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();
|
||||
}
|
||||
}
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Core\Exceptions;
|
||||
|
||||
use Core\Env\Env;
|
||||
use Core\Http\Request;
|
||||
use Throwable;
|
||||
use Whoops\Handler\Handler;
|
||||
|
@ -11,7 +10,7 @@ use Whoops\Handler\PlainTextHandler;
|
|||
use Whoops\Handler\PrettyPageHandler;
|
||||
use Whoops\Run as Whoops;
|
||||
|
||||
class Exceptions
|
||||
class ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* Exceptions handler instance
|
||||
|
@ -41,13 +40,12 @@ class Exceptions
|
|||
/**
|
||||
* Get correct handler
|
||||
*
|
||||
* @param \Core\Http\Request|null $request
|
||||
* @return \Whoops\Handler\Handler
|
||||
*/
|
||||
private static function handler(Request|null $request): Handler
|
||||
private static function handler(): Handler
|
||||
{
|
||||
if (Env::get('debug')) {
|
||||
if ($request?->is('post')) {
|
||||
if (env('debug')) {
|
||||
if (request()?->is('post')) {
|
||||
return new JsonResponseHandler();
|
||||
}
|
||||
|
||||
|
@ -60,24 +58,44 @@ class Exceptions
|
|||
/**
|
||||
* Catch all exceptions
|
||||
*
|
||||
* @param \Core\Http\Request $request
|
||||
* @return void
|
||||
*/
|
||||
public static function catch(Request $request): void
|
||||
public static function catch(): void
|
||||
{
|
||||
self::instance($request)->register();
|
||||
self::instance()->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Catch single exception
|
||||
*
|
||||
* @param \Throwable $exception
|
||||
* @return void
|
||||
* @return never
|
||||
*/
|
||||
public static function catchOne(Throwable $exception): void
|
||||
public static function catchOne(Throwable $exception): never
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
5
src/Exceptions/Exceptions/ClassNotFoundException.php
Normal file
5
src/Exceptions/Exceptions/ClassNotFoundException.php
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
namespace Core\Exceptions\Exceptions;
|
||||
|
||||
class ClassNotFoundException extends \Exception {}
|
94
src/Factory/BootstrapFactory.php
Normal file
94
src/Factory/BootstrapFactory.php
Normal file
|
@ -0,0 +1,94 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
51
src/Helpers/helpers.php
Normal file
51
src/Helpers/helpers.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?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();
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Core\Controllers;
|
||||
namespace Core\Http\Controllers;
|
||||
|
||||
use Core\Http\Request;
|
||||
use Core\Http\Response;
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace Core\Http;
|
||||
|
||||
use Core\Exceptions\Exceptions;
|
||||
|
||||
class Request
|
||||
{
|
||||
private array $data = [];
|
||||
|
@ -16,9 +14,6 @@ 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\View\Render;
|
||||
use Core\View\Render\HtmlRender;
|
||||
use Core\View\Render\JsonRender;
|
||||
use Core\Http\View\Engine\HtmlEngine;
|
||||
use Core\Http\View\Engine\JsonEngine;
|
||||
use Core\Http\View\Render;
|
||||
|
||||
class Response
|
||||
{
|
||||
|
@ -25,20 +25,20 @@ class Response
|
|||
* Render HTML
|
||||
*
|
||||
* @param string $view
|
||||
* @return \Core\View\Render
|
||||
* @return \Core\Http\View\Render
|
||||
*/
|
||||
public function view(string $view): Render
|
||||
{
|
||||
return (new HtmlRender())->view($view);
|
||||
return (new HtmlEngine())->view($view);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render JSON
|
||||
*
|
||||
* @return \Core\View\Render
|
||||
* @return \Core\Http\View\Render
|
||||
*/
|
||||
public function json(): Render
|
||||
{
|
||||
return new JsonRender();
|
||||
return new JsonEngine();
|
||||
}
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Core\View\Render;
|
||||
namespace Core\Http\View\Engine;
|
||||
|
||||
use Core\View\Render;
|
||||
use Core\Http\View\Render;
|
||||
|
||||
class HtmlRender extends Render
|
||||
class HtmlEngine extends Render
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
|
@ -13,7 +13,7 @@ class HtmlRender extends Render
|
|||
public function render(): void
|
||||
{
|
||||
$basePath = $_SERVER['DOCUMENT_ROOT'];
|
||||
$viewsPath = $basePath . '/../resources/views/' . str_replace('.', '/', $this->view) . '.php';
|
||||
$viewsPath = app()->resourcePath('views/' . str_replace('.', '/', $this->view) . '.php');
|
||||
|
||||
if (file_exists($viewsPath)) {
|
||||
extract($this->data);
|
|
@ -1,10 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Core\View\Render;
|
||||
namespace Core\Http\View\Engine;
|
||||
|
||||
use Core\View\Render;
|
||||
|
||||
class JsonRender extends Render
|
||||
use Core\Http\View\Render;
|
||||
|
||||
class JsonEngine extends Render
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Core\View;
|
||||
namespace Core\Http\View;
|
||||
|
||||
abstract class Render
|
||||
{
|
|
@ -2,11 +2,10 @@
|
|||
|
||||
namespace Core\Routing;
|
||||
|
||||
use Core\Env\Env;
|
||||
use Core\Exceptions\Exceptions;
|
||||
use Core\Exceptions\ExceptionHandler;
|
||||
use Core\Exceptions\Exceptions\NotFoundHttpException;
|
||||
use Core\Http\Request;
|
||||
use Core\View\Render;
|
||||
use Core\Http\View\Engine;
|
||||
use Exception;
|
||||
|
||||
class RouteDispatcher
|
||||
|
@ -26,10 +25,13 @@ class RouteDispatcher
|
|||
private array $routeCollection;
|
||||
|
||||
/**
|
||||
* Dispatch the router dispatcher
|
||||
*
|
||||
* @param \Core\Http\Request $request
|
||||
* @param array $routeCollection
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Request $request, array $routeCollection)
|
||||
public function dispatch(Request $request, array $routeCollection): void
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->routeCollection = $routeCollection;
|
||||
|
@ -48,18 +50,6 @@ 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.
|
||||
*
|
||||
|
@ -153,8 +143,8 @@ class RouteDispatcher
|
|||
*/
|
||||
private function handleException(Exception $e, int $statusCode, string $message): void
|
||||
{
|
||||
if (Env::get('debug')) {
|
||||
Exceptions::catchOne($e);
|
||||
if (env('debug')) {
|
||||
ExceptionHandler::catchOne($e);
|
||||
}
|
||||
|
||||
http_response_code($statusCode);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue