Initial commit of LightMVC framework files.
This commit is contained in:
Executable
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Core;
|
||||
|
||||
class Application
|
||||
{
|
||||
public static string $ROOT_DIR;
|
||||
public Router $router;
|
||||
public Request $request;
|
||||
public Response $response;
|
||||
public View $view;
|
||||
public static Application $app;
|
||||
|
||||
public function __construct($rootPath)
|
||||
{
|
||||
self::$ROOT_DIR = $rootPath;
|
||||
self::$app = $this;
|
||||
$this->request = new Request();
|
||||
$this->response = new Response();
|
||||
$this->view = new View();
|
||||
$this->router = new Router($this->request, $this->response);
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
echo $this->router->resolve();
|
||||
}
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Core;
|
||||
|
||||
class Controller
|
||||
{
|
||||
public function render($view, $params = [])
|
||||
{
|
||||
return Application::$app->router->renderView($view, $params);
|
||||
}
|
||||
}
|
||||
Executable
+37
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Core;
|
||||
|
||||
class Request
|
||||
{
|
||||
public function getPath()
|
||||
{
|
||||
$path = $_SERVER['REQUEST_URI'] ?? '/';
|
||||
$position = strpos($path, '?');
|
||||
if ($position === false) {
|
||||
return $path;
|
||||
}
|
||||
return substr($path, 0, $position);
|
||||
}
|
||||
|
||||
public function getMethod()
|
||||
{
|
||||
return strtolower($_SERVER['REQUEST_METHOD']);
|
||||
}
|
||||
|
||||
public function getBody()
|
||||
{
|
||||
$body = [];
|
||||
if ($this->getMethod() === 'get') {
|
||||
foreach ($_GET as $key => $value) {
|
||||
$body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
|
||||
}
|
||||
}
|
||||
if ($this->getMethod() === 'post') {
|
||||
foreach ($_POST as $key => $value) {
|
||||
$body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
|
||||
}
|
||||
}
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Core;
|
||||
|
||||
class Response
|
||||
{
|
||||
public function setStatusCode(int $code)
|
||||
{
|
||||
http_response_code($code);
|
||||
}
|
||||
}
|
||||
Executable
+53
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Core;
|
||||
|
||||
class Router
|
||||
{
|
||||
protected array $routes = [];
|
||||
public Request $request;
|
||||
public Response $response;
|
||||
|
||||
public function __construct(Request $request, Response $response)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
public function get($path, $callback)
|
||||
{
|
||||
$this->routes['get'][$path] = $callback;
|
||||
}
|
||||
|
||||
public function post($path, $callback)
|
||||
{
|
||||
$this->routes['post'][$path] = $callback;
|
||||
}
|
||||
|
||||
public function resolve()
|
||||
{
|
||||
$path = $this->request->getPath();
|
||||
$method = $this->request->getMethod();
|
||||
$callback = $this->routes[$method][$path] ?? false;
|
||||
|
||||
if ($callback === false) {
|
||||
$this->response->setStatusCode(404);
|
||||
return $this->renderView("_404");
|
||||
}
|
||||
|
||||
if (is_string($callback)) {
|
||||
return $this->renderView($callback);
|
||||
}
|
||||
|
||||
if (is_array($callback)) {
|
||||
$callback[0] = new $callback[0]();
|
||||
}
|
||||
|
||||
return call_user_func($callback, $this->request);
|
||||
}
|
||||
|
||||
public function renderView($view, $params = [])
|
||||
{
|
||||
return Application::$app->view->renderView($view, $params);
|
||||
}
|
||||
}
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Core;
|
||||
|
||||
class View
|
||||
{
|
||||
public function renderView($view, $params = [])
|
||||
{
|
||||
$viewContent = $this->renderOnlyView($view, $params);
|
||||
$layoutContent = $this->layoutContent();
|
||||
return str_replace('{{content}}', $viewContent, $layoutContent);
|
||||
}
|
||||
|
||||
protected function layoutContent()
|
||||
{
|
||||
ob_start();
|
||||
include_once Application::$ROOT_DIR . "/app/Views/layouts/main.php";
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
protected function renderOnlyView($view, $params)
|
||||
{
|
||||
foreach ($params as $key => $value) {
|
||||
$$key = $value;
|
||||
}
|
||||
ob_start();
|
||||
include_once Application::$ROOT_DIR . "/app/Views/$view.php";
|
||||
$content = ob_get_clean();
|
||||
|
||||
// Simple {{variable}} replacement
|
||||
foreach ($params as $key => $value) {
|
||||
if (is_scalar($value)) {
|
||||
$content = str_replace('{{'.$key.'}}', $value, $content);
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user