refactor Web to App; namespace Web
This commit is contained in:
parent
ce1d13e862
commit
8a94183081
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/Methods.php';
|
||||
namespace Web;
|
||||
|
||||
require_once __DIR__ . '/HTTPMethod.php';
|
||||
require_once __DIR__ . '/Request.php';
|
||||
require_once __DIR__ . '/Response.php';
|
||||
require_once __DIR__ . '/Context.php';
|
||||
@ -9,9 +11,9 @@ require_once __DIR__ . '/ErrorHandler.php';
|
||||
require_once __DIR__ . '/Cookies.php';
|
||||
|
||||
/**
|
||||
* Web is the application controller itself
|
||||
* App is the application controller itself
|
||||
*/
|
||||
class Web
|
||||
class App
|
||||
{
|
||||
private Router $router;
|
||||
private array $middleware = [];
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
require_once __DIR__ . '/Session.php';
|
||||
require_once __DIR__ . '/Validator.php';
|
||||
require_once __DIR__ . '/Cookies.php';
|
||||
@ -18,7 +20,7 @@ class Context
|
||||
/**
|
||||
* __construct creates a new Context with request and response
|
||||
*/
|
||||
public function __construct(?Cookie $cookie = null)
|
||||
public function __construct(?Cookies $cookie = null)
|
||||
{
|
||||
$this->request = new Request();
|
||||
$this->response = new Response();
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
/**
|
||||
* Cookie manager for consistent cookie handling
|
||||
*/
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
/**
|
||||
* ErrorHandler manages error pages and exception handling
|
||||
*/
|
||||
@ -37,7 +39,7 @@ class ErrorHandler
|
||||
/**
|
||||
* handle handles an error with the appropriate handler
|
||||
*/
|
||||
public function handle(Context $context, int $status, string $message = '', ?Exception $exception = null): void
|
||||
public function handle(Context $context, int $status, string $message = '', ?\Exception $exception = null): void
|
||||
{
|
||||
if (isset($this->handlers[$status])) {
|
||||
$handler = $this->handlers[$status];
|
||||
@ -56,7 +58,7 @@ class ErrorHandler
|
||||
/**
|
||||
* handleException handles uncaught exceptions
|
||||
*/
|
||||
public function handleException(Context $context, Exception $exception): void
|
||||
public function handleException(Context $context, \Exception $exception): void
|
||||
{
|
||||
$status = $this->getStatusFromException($exception);
|
||||
$message = $this->debug ? $exception->getMessage() : $this->getDefaultMessage($status);
|
||||
@ -71,7 +73,7 @@ class ErrorHandler
|
||||
/**
|
||||
* getStatusFromException determines HTTP status from exception type
|
||||
*/
|
||||
private function getStatusFromException(Exception $exception): int
|
||||
private function getStatusFromException(\Exception $exception): int
|
||||
{
|
||||
if ($exception instanceof HttpException) {
|
||||
return $exception->getStatusCode();
|
||||
@ -106,7 +108,7 @@ class ErrorHandler
|
||||
});
|
||||
|
||||
// 500 Internal Server Error
|
||||
$this->register(500, function(Context $context, int $status, string $message, ?Exception $exception) {
|
||||
$this->register(500, function(Context $context, int $status, string $message, ?\Exception $exception) {
|
||||
$accept = $context->request->header('accept') ?? '';
|
||||
|
||||
if (str_contains($accept, 'application/json')) {
|
||||
@ -125,7 +127,7 @@ class ErrorHandler
|
||||
/**
|
||||
* renderDefaultError renders a generic error response
|
||||
*/
|
||||
private function renderDefaultError(Context $context, int $status, string $message, ?Exception $exception): void
|
||||
private function renderDefaultError(Context $context, int $status, string $message, ?\Exception $exception): void
|
||||
{
|
||||
$message = $message ?: $this->getDefaultMessage($status);
|
||||
$accept = $context->request->header('accept') ?? '';
|
||||
@ -228,7 +230,7 @@ HTML;
|
||||
/**
|
||||
* render500Page renders a 500 error page
|
||||
*/
|
||||
private function render500Page(string $message, ?Exception $exception): string
|
||||
private function render500Page(string $message, ?\Exception $exception): string
|
||||
{
|
||||
$message = htmlspecialchars($message ?: 'Internal Server Error');
|
||||
$debugInfo = '';
|
||||
@ -320,7 +322,7 @@ HTML;
|
||||
/**
|
||||
* renderErrorPage renders a generic error page
|
||||
*/
|
||||
private function renderErrorPage(int $status, string $message, ?Exception $exception): string
|
||||
private function renderErrorPage(int $status, string $message, ?\Exception $exception): string
|
||||
{
|
||||
$message = htmlspecialchars($message);
|
||||
$debugInfo = '';
|
||||
@ -408,11 +410,11 @@ HTML;
|
||||
/**
|
||||
* HttpException base class for HTTP exceptions
|
||||
*/
|
||||
class HttpException extends Exception
|
||||
class HttpException extends \Exception
|
||||
{
|
||||
protected int $statusCode;
|
||||
|
||||
public function __construct(int $statusCode, string $message = '', int $code = 0, Exception $previous = null)
|
||||
public function __construct(int $statusCode, string $message = '', int $code = 0, \Exception $previous = null)
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
parent::__construct($message, $code, $previous);
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
/**
|
||||
* HTTPMethod represents an HTTP method
|
||||
*/
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
/**
|
||||
* Request represents an incoming HTTP request
|
||||
*/
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
require_once __DIR__ . '/Cookies.php';
|
||||
|
||||
/**
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
class Router
|
||||
{
|
||||
private array $routes = [];
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
/**
|
||||
* Session provides session management with flash messages and CSRF protection
|
||||
*/
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
/**
|
||||
* Validator provides input validation with chainable rules
|
||||
*/
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
require_once __DIR__ . '/User.php';
|
||||
require_once __DIR__ . '/../Session.php';
|
||||
require_once __DIR__ . '/../Cookies.php';
|
||||
@ -18,7 +20,7 @@ class Auth
|
||||
const REMEMBER_COOKIE = 'remember_token';
|
||||
const REMEMBER_DURATION = 2592000; // 30 days in seconds
|
||||
|
||||
public function __construct(Session $session, ?Cookie $cookie = null, array $config = [])
|
||||
public function __construct(Session $session, ?Cookies $cookie = null, array $config = [])
|
||||
{
|
||||
$this->session = $session;
|
||||
$this->cookie = $cookie ?: new Cookies([
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
require_once __DIR__ . '/Auth.php';
|
||||
|
||||
/**
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Web;
|
||||
|
||||
/**
|
||||
* User model represents an authenticated user for auth functionality only
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user