Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
mirarus committed Nov 19, 2022
1 parent 96c5d59 commit 5111ca3
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 58 deletions.
11 changes: 5 additions & 6 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Ali Güçlü (Mirarus) <[email protected]>
* @link https://github.com/mirarus/bmvc-libs
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version 0.2
* @version 0.3
*/

namespace BMVC\Libs\Response;
Expand All @@ -19,7 +19,7 @@ class Response
/**
* @var string[]
*/
private static $statusCodes = [
public static $statusCodes = [
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
Expand Down Expand Up @@ -69,6 +69,7 @@ class Response
*/
public static function setHeader(int $code)
{
@http_response_code($code);
@header("HTTP/1.1 " . $code . " " . self::setStatusCode($code));
@header("Content-Type: application/json; charset=utf-8");
}
Expand Down Expand Up @@ -111,9 +112,8 @@ public static function getStatusMessage(int $code = null): string
*/
public static function json($data = null, bool $status = true, int $code = 200, bool $cache = true)
{
self::setStatusCode($code);
self::setHeader($code);
if ($cache == true) @header("Cache-Control: no-transform,public,max-age=300,s-maxage=900");
@header('Content-type: application/json');
//@header('Status: ' . self::$statusCodes[$code]);
return json_encode(['status' => $status, 'message' => $data]);
}
Expand All @@ -127,9 +127,8 @@ public static function json($data = null, bool $status = true, int $code = 200,
public static function _json(array $data = null, int $code = 200, bool $cache = true)
{
if ($data == null) $data = [];
self::setStatusCode($code);
self::setHeader($code);
if ($cache == true) @header("Cache-Control: no-transform,public,max-age=300,s-maxage=900");
@header('Content-type: application/json');
return json_encode($data);
}
}
8 changes: 7 additions & 1 deletion src/Route/IMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Ali Güçlü (Mirarus) <[email protected]>
* @link https://github.com/mirarus/bmvc-libs
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version 0.3
* @version 0.4
*/

namespace BMVC\Libs\Route;
Expand Down Expand Up @@ -103,4 +103,10 @@ public static function match(array $methods, string $pattern = null, $callback):
* @return mixed
*/
public static function any(string $pattern = null, $callback);

/**
* @param int|null $code
* @param $callback
*/
public static function error(int $code = null, $callback);
}
14 changes: 6 additions & 8 deletions src/Route/IRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Ali Güçlü (Mirarus) <[email protected]>
* @link https://github.com/mirarus/bmvc-libs
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version 0.5
* @version 0.6
*/

namespace BMVC\Libs\Route;
Expand All @@ -18,6 +18,11 @@
interface IRoute
{

/**
* @param array|null $args
*/
public static function args(array $args = null);

/**
* @param array|null $return
* @return mixed
Expand Down Expand Up @@ -55,13 +60,6 @@ public static function url(string $name, array $params = null): string;
*/
public static function routes(): array;

/**
* @param int $code
* @param Closure $callback
* @return mixed
*/
public static function setErrors(int $code, Closure $callback);

/**
* @param int|null $code
* @return mixed
Expand Down
11 changes: 10 additions & 1 deletion src/Route/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Ali Güçlü (Mirarus) <[email protected]>
* @link https://github.com/mirarus/bmvc-libs
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version 0.4
* @version 0.5
*/

namespace BMVC\Libs\Route;
Expand Down Expand Up @@ -192,4 +192,13 @@ public static function any(string $pattern = null, $callback): self
}
return new self;
}

/**
* @param int|null $code
* @param $callback
*/
public static function error(int $code = null, $callback)
{
self::setError($code, $callback);
}
}
100 changes: 61 additions & 39 deletions src/Route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Ali Güçlü (Mirarus) <[email protected]>
* @link https://github.com/mirarus/bmvc-core
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version 0.12
* @version 0.13
*/

namespace BMVC\Libs\Route;
Expand All @@ -18,16 +18,12 @@
use BMVC\Libs\Request;
use BMVC\Libs\Response;
use BMVC\Libs\MError;
use BMVC\Libs\_classCall;

class Route implements IRoute, IMethod
{
use Method;

/**
* @var
*/
public static $errors;

/**
* @var array
*/
Expand All @@ -43,6 +39,16 @@ class Route implements IRoute, IMethod
*/
private static $middlewares = [];

/**
* @var array
*/
private static $errors = [];

/**
* @var array
*/
private static $args = [];

/**
* @var bool
*/
Expand Down Expand Up @@ -99,6 +105,14 @@ class Route implements IRoute, IMethod
'{uppercase}' => '([A-Z]+)',
];

/**
* @param array|null $args
*/
public static function args(array $args = null)
{
self::$args = $args;
}

/**
* @param array|null $return
* @return array|null
Expand Down Expand Up @@ -298,13 +312,35 @@ public static function routes(): array
}

/**
* @param int $code
* @param Closure $callback
* @return mixed|void
* @param int|null $code
* @param $callback
* @return void
*/
public static function setErrors(int $code, Closure $callback)
private static function setError(int $code = null, $callback): void
{
self::$errors[$code] = $callback;
$closure = null;

if (is_callable($callback)) {
$closure = $callback;
} elseif (is_string($callback)) {
if (stripos($callback, '@') !== false) {
$closure = $callback;
} elseif (stripos($callback, '/') !== false) {
$closure = $callback;
} elseif (stripos($callback, '.') !== false) {
$closure = $callback;
} elseif (stripos($callback, '::') !== false) {
$closure = $callback;
} elseif (stripos($callback, ':') !== false) {
$closure = $callback;
}
} elseif (is_array($callback)) {
$closure = $callback[0] . ':' . $callback[1];
}

if ($closure) {
self::$errors[$code] = @_classCall::namespace(self::$args['namespace'], true)->call($closure);
}
}

/**
Expand All @@ -313,40 +349,26 @@ public static function setErrors(int $code, Closure $callback)
*/
public static function getErrors(int $code = null)
{
$url = Util::get_url();

$error_404 = function ($stop = true) use ($url) {
Response::setStatusCode(404);
$msg = Response::getStatusMessage();
$res = (Response::getStatusCode() . ' ' . $msg);
if (Request::isGet()) {
MError::p($msg, $res, null, true, $stop, 'danger', 404);
self::$errors[404] = self::$errors[404] ?: function () {
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
MError::p('Not Found', '404 Not Found', null, true, true, 'danger', 404);
} else {
echo Response::_json(($url ? ['message' => $res, 'page' => $url] : ['message' => $res]), 404);
http_response_code(404);
@header("Content-Type: application/json; charset=utf-8");
return json_encode(['message' => '404 Not Found', 'page' => Util::get_url()]);
}
if ($stop) die();
};

$error_500 = function ($stop = true) use ($url) {
Response::setStatusCode(500);
$msg = Response::getStatusMessage();
$res = (Response::getStatusCode() . ' ' . $msg);
if (Request::isGet()) {
MError::p($msg, $res, null, true, $stop, 'danger', 500);
self::$errors[500] = self::$errors[500] ?: function () {
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
MError::p('Internal Server Error', '404 Internal Server Error', null, true, true, 'danger', 500);
} else {
echo Response::_json(($url ? ['message' => $res, 'page' => $url] : ['message' => $res]), 500);
http_response_code(500);
@header("Content-Type: application/json; charset=utf-8");
return json_encode(['message' => '404 Internal Server Error', 'page' => Util::get_url()]);
}
if ($stop) die();
};

self::$errors = [
'404' => self::$errors[404] ?: $error_404,
'500' => self::$errors[500] ?: $error_500
];

return $code ? self::$errors[$code]() : array_map(function ($error) {
$error(false);
}, self::$errors);
return $code ? self::$errors[$code]() : self::$errors;
}

/**
Expand All @@ -359,7 +381,7 @@ public static function redirect($origin, $destination, $permanent = true)
{
if (Util::get_url() == $origin) {
if (headers_sent() == false) {
header('Location: ' . Util::url($destination), true, ($permanent == true) ? 301 : 302);
header('Location: ' . Util::url($destination), true, ($permanent ? 301 : 302));
}
exit();
}
Expand Down
18 changes: 18 additions & 0 deletions src/_classCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* _classCall
*
* Mirarus BMVC
* @package BMVC\Libs
* @author Ali Güçlü (Mirarus) <[email protected]>
* @link https://github.com/mirarus/bmvc-libs
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version 0.0
*/

namespace BMVC\Libs;

class _classCall {
use classCall;
}
19 changes: 16 additions & 3 deletions src/classCall/classCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Ali Güçlü (Mirarus) <[email protected]>
* @link https://github.com/mirarus/bmvc-libs
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version 0.4
* @version 0.5
*/

namespace BMVC\Libs\classCall;
Expand Down Expand Up @@ -79,8 +79,7 @@ public static function params(array $params): self
*/
public static function call($action, array $params = null, object &$return = null)
{
$action = CL::replace($action);


if (is_callable($action)) {

if ($params == null) {
Expand All @@ -92,6 +91,7 @@ public static function call($action, array $params = null, object &$return = nul

$method = null;
$class = null;
$action = CL::replace($action);

if ($action == null) return;

Expand Down Expand Up @@ -168,10 +168,23 @@ public static function get(string $action, object &$return = null): array
$class = ($_ns != null) ? CL::implode([$_ns, $class]) : $class;
$class = CL::replace($class);
$cls = (new $class((is_array(self::$params) && !empty(self::$params))));
@header("Last-Modified: " . date("D, d M Y H:i:s") . " GMT");

return $return = [
'class' => $class,
'cls' => $cls
];
}

/**
* @param string $class
* @param object|null $return
* @return mixed
*/
public static function import(string $class, object &$return = null)
{
self::get($class, $get);

return $return = @$get['cls'];
}
}

0 comments on commit 5111ca3

Please sign in to comment.