-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
123 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -19,7 +19,7 @@ class Response | |
/** | ||
* @var string[] | ||
*/ | ||
private static $statusCodes = [ | ||
public static $statusCodes = [ | ||
100 => 'Continue', | ||
101 => 'Switching Protocols', | ||
200 => 'OK', | ||
|
@@ -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"); | ||
} | ||
|
@@ -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]); | ||
} | ||
|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -18,6 +18,11 @@ | |
interface IRoute | ||
{ | ||
|
||
/** | ||
* @param array|null $args | ||
*/ | ||
public static function args(array $args = null); | ||
|
||
/** | ||
* @param array|null $return | ||
* @return mixed | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
*/ | ||
|
@@ -43,6 +39,16 @@ class Route implements IRoute, IMethod | |
*/ | ||
private static $middlewares = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $errors = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $args = []; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) { | ||
|
@@ -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; | ||
|
||
|
@@ -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']; | ||
} | ||
} |