reasonableframework/system/base.php

340 lines
6.9 KiB
PHP
Raw Normal View History

2017-12-31 16:15:38 +00:00
<?php
2018-04-13 05:04:25 +00:00
/**
* @file base.php
* @date 2018-04-13
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief Base module
*/
2019-02-26 02:26:51 +00:00
// check invalid function (mixed)
if(!function_exists("check_invalid_function")) {
function check_invalid_function($fn) {
2019-02-26 02:17:44 +00:00
$status = -1;
if(is_array($fn)) {
foreach($fn as $k=>$v) {
2019-02-26 02:31:08 +00:00
if(!function_exists($v)) {
2019-02-26 02:17:44 +00:00
$status = $k;
break;
}
}
2019-02-26 02:17:44 +00:00
} else {
2019-02-26 02:31:08 +00:00
if(!function_exists($fn)) {
2019-02-26 02:17:44 +00:00
$status = 0;
}
}
}
return $status;
2019-02-26 02:17:44 +00:00
}
2019-02-26 02:26:51 +00:00
// check valid function (bool)
if(check_invalid_function("check_valid_function") == -1) {
2019-02-26 02:26:51 +00:00
function check_valid_function($fn) {
return (check_invalid_function($fn) == -1);
2019-02-26 02:17:44 +00:00
}
}
2018-04-13 05:04:25 +00:00
// set scope
2019-02-26 02:39:32 +00:00
if(check_valid_function("set_scope")) {
2018-04-13 05:04:25 +00:00
function set_scope($k, $v) {
global $scope;
$scope[$k] = $v;
}
}
// get scope
2019-02-26 02:39:32 +00:00
if(check_valid_function("get_scope")) {
2018-04-13 05:04:25 +00:00
function get_scope($k) {
global $scope;
return array_key_exists($k, $scope) ? $scope[$k] : null;
}
}
// register loaded resources
2019-02-26 02:39:32 +00:00
if(check_valid_function("register_loaded")) {
2018-04-13 05:04:25 +00:00
function register_loaded($k, $v) {
2019-02-26 02:44:23 +00:00
$loaded = get_scope("loaded");
if(array_key_exists($k, $loaded)) {
if(is_array($loaded[$k]) {
$loaded[$k][] = $v;
}
}
set_scope("loaded", $loaded);
2018-04-13 05:04:25 +00:00
}
}
// sandbox for include function
2019-02-26 02:39:32 +00:00
if(check_valid_function("include_isolate")) {
2018-04-13 05:04:25 +00:00
function include_isolate($file, $data=array()) {
if(count($data) > 0) {
extract($data);
}
return include($file);
}
}
2018-12-29 04:20:20 +00:00
// set autoloader
2019-02-26 02:39:32 +00:00
if(check_valid_function("set_autoloader")) {
2018-12-29 04:20:20 +00:00
function set_autoloader() {
2018-12-29 04:29:46 +00:00
return include('./vendor/autoload.php');
2018-12-29 04:20:20 +00:00
}
}
2018-04-13 05:38:39 +00:00
// load view file
2019-02-26 02:39:32 +00:00
if(check_valid_function("renderView")) {
2017-12-31 18:31:02 +00:00
function renderView($name, $data=array()) {
2018-04-13 17:00:23 +00:00
$flag = true;
2018-04-13 05:04:25 +00:00
$views = explode(';', $name);
foreach($views as $name2) {
$viewfile = './view/' . $name2 . '.php';
if(file_exists($viewfile)) {
2018-06-11 02:50:20 +00:00
register_loaded("view", $name2);
2018-04-13 17:00:23 +00:00
$flag = $flag && !include_isolate($viewfile, $data);
2018-04-13 05:04:25 +00:00
}
2017-12-31 18:31:02 +00:00
}
2018-04-13 17:00:23 +00:00
return !$flag;
2017-12-31 18:31:02 +00:00
}
}
2018-06-11 06:47:22 +00:00
// load view by rules
2019-02-26 02:39:32 +00:00
if(check_valid_function("renderViewByRules")) {
2018-06-11 06:47:22 +00:00
function renderViewByRules($rules, $data=array()) {
foreach($rules as $k=>$v) {
if(in_array($k, get_routes())) {
renderView($v, $data);
}
}
}
}
2018-02-26 10:58:54 +00:00
// load system module
2019-02-26 02:39:32 +00:00
if(check_valid_function("loadModule")) {
2018-02-26 10:58:54 +00:00
function loadModule($name) {
2018-04-13 17:00:23 +00:00
$flag = true;
2018-03-10 16:47:25 +00:00
$modules = explode(';', $name);
foreach($modules as $name2) {
$systemfile = './system/' . $name2 . '.php';
if(file_exists($systemfile)) {
2018-06-11 02:50:20 +00:00
register_loaded("system", $name2);
2018-04-13 17:00:23 +00:00
$flag = $flag && !include_isolate($systemfile);
2018-04-28 15:48:43 +00:00
} else {
set_error("Module " . $name . "dose not exists");
2018-03-10 16:47:25 +00:00
}
2018-02-26 10:58:54 +00:00
}
2018-04-13 17:00:23 +00:00
return !$flag;
2018-02-26 10:58:54 +00:00
}
}
2018-04-13 05:38:39 +00:00
// load helper file
2019-02-26 02:39:32 +00:00
if(check_valid_function("loadHelper")) {
2017-12-31 18:31:02 +00:00
function loadHelper($name) {
2018-04-13 17:00:23 +00:00
$flag = true;
2018-03-10 16:47:25 +00:00
$helpers = explode(';', $name);
foreach($helpers as $name2) {
$helperfile = './helper/' . $name2 . '.php';
if(file_exists($helperfile)) {
2018-06-11 02:50:20 +00:00
register_loaded("helper", $name2);
2018-04-13 17:00:23 +00:00
$flag = $flag && !include_isolate($helperfile);
2018-04-28 15:48:43 +00:00
} else {
set_error("Helper " . $name . "dose not exists");
2018-03-10 16:47:25 +00:00
}
2017-12-31 18:31:02 +00:00
}
2018-04-13 17:01:06 +00:00
return !$flag;
2017-12-31 18:31:02 +00:00
}
}
2018-04-13 05:38:39 +00:00
// load route file
2019-02-26 02:39:32 +00:00
if(check_valid_function("loadRoute")) {
2018-04-13 05:04:25 +00:00
function loadRoute($name, $data=array()) {
2018-04-13 17:00:23 +00:00
$flag = true;
2018-04-13 05:04:25 +00:00
$routes = explode(";", $name);
foreach($routes as $name2) {
2019-02-22 17:53:58 +00:00
$routefile = './route/' . $name2 . '.php';
if(file_exists($routefile)) {
register_loaded("route", $name2);
$flag = $flag && !include_isolate($routefile, $data);
} else {
2018-04-28 15:48:43 +00:00
set_error("Route " . $name . "dose not exists");
2018-04-13 05:04:25 +00:00
}
2018-03-08 10:01:12 +00:00
}
2018-04-13 17:00:23 +00:00
return !$flag;
2018-03-08 10:01:12 +00:00
}
}
2018-09-20 03:24:49 +00:00
// load vendor file
2019-02-26 02:39:32 +00:00
if(check_valid_function("loadVendor")) {
2018-09-20 03:30:02 +00:00
function loadVendor($uses, $data=array()) {
2018-09-20 03:24:49 +00:00
$flag = true;
2018-09-20 03:30:02 +00:00
$usenames = array();
2018-09-20 03:24:49 +00:00
2018-09-20 03:30:02 +00:00
if(is_string($uses) && !empty($uses)) {
$usenames[] = $uses;
} elseif(is_array($uses)) {
$usenames = array_merge($usenames, $uses);
} else {
2018-09-20 03:24:49 +00:00
return !$flag;
}
foreach($usenames as $name) {
$vendorfile = './vendor/' . $name . '.php';
if(file_exists($vendorfile)) {
register_loaded("vendor", $name);
$flag = $flag && !include_isolate($vendorfile, $data);
} else {
set_error("Vendor " . $name . "dose not exists");
}
}
return !$flag;
}
}
2019-02-26 02:39:32 +00:00
if(check_valid_function("array_key_empty")) {
2017-12-31 16:15:38 +00:00
function array_key_empty($key, $array) {
$empty = true;
if(is_array($array)) {
if(array_key_exists($key, $array)) {
2018-09-20 03:24:49 +00:00
$empty = $empty && empty($array[$key]);
2017-12-31 16:15:38 +00:00
}
}
2018-04-13 17:00:23 +00:00
2017-12-31 16:15:38 +00:00
return $empty;
}
}
2017-12-31 17:00:50 +00:00
2019-02-26 02:39:32 +00:00
if(check_valid_function("array_key_equals")) {
2018-06-04 10:13:37 +00:00
function array_key_equals($key, $array, $value) {
$equals = false;
if(is_array($array)) {
2018-06-11 02:50:20 +00:00
if(array_key_exists($key, $array)) {
$equals = ($array[$key] == $value);
2018-06-04 10:13:37 +00:00
}
}
return $equals;
}
}
2019-02-26 02:39:32 +00:00
if(check_valid_function("array_key_is_array")) {
2018-11-26 10:33:43 +00:00
function array_key_is_array($key, $array) {
$flag = false;
if(is_array($array)) {
if(array_key_exists($key, $array)) {
$flag = is_array($array[$key]);
}
}
return $flag;
}
}
2019-02-26 02:39:32 +00:00
if(check_valid_function("array_multikey_empty")) {
2018-02-14 04:45:33 +00:00
function array_multikey_empty($keys, $array) {
2018-05-27 09:38:02 +00:00
$flag = false;
2018-02-14 04:45:33 +00:00
foreach($keys as $key) {
2018-05-27 09:38:02 +00:00
if(array_key_empty($key, $array)) {
$flag = $key;
}
2018-02-14 04:45:33 +00:00
}
2018-04-13 05:38:39 +00:00
return $flag;
2018-02-14 04:45:33 +00:00
}
}
2019-02-26 02:39:32 +00:00
if(check_valid_function("get_value_in_array")) {
2018-12-11 01:47:07 +00:00
function get_value_in_array($name, $arr=array(), $default=false) {
2018-12-11 01:42:55 +00:00
$output = false;
2018-03-23 09:24:05 +00:00
if(is_array($arr)) {
$output = array_key_empty($name, $arr) ? $default : $arr[$name];
} else {
$output = $default;
}
return $output;
}
}
2019-02-26 02:39:32 +00:00
if(check_valid_function("get_value_in_object")) {
2018-04-13 08:02:49 +00:00
function get_value_in_object($name, $obj, $default="") {
$output = $obj->$name;
return $output;
}
}
2018-04-28 15:45:21 +00:00
// error handler
2019-02-26 02:39:32 +00:00
if(check_valid_function("set_error")) {
2018-04-28 15:45:21 +00:00
function set_error($msg, $code="ERROR") {
global $scope;
$scope['errors'][] = $code . ": " . $msg;
}
}
2019-02-26 02:39:32 +00:00
if(check_valid_function("get_errors")) {
2018-04-28 15:45:21 +00:00
function get_errors($d=false, $e=false) { // d: display, e: exit
global $scope;
$errors = $scope['errors'];
if($d === true) {
foreach($errors as $err) {
echo $err . PHP_EOL;
}
}
if($e === true) {
exit;
}
return $errors;
}
}
2019-02-26 02:39:32 +00:00
if(check_valid_function("show_errors")) {
2018-04-28 15:45:21 +00:00
function show_errors($exit=true) {
return get_errors(true, $exit);
}
}
2019-02-26 02:39:32 +00:00
if(check_valid_function("set_error_exit")) {
2018-08-24 14:57:09 +00:00
function set_error_exit($msg, $code="ERROR") {
set_error($msg, $code);
show_errors();
}
}
2018-04-28 16:33:03 +00:00
2019-02-26 02:39:32 +00:00
if(check_valid_function("get_property_value")) {
2018-05-28 06:52:35 +00:00
function get_property_value($prop, $obj, $ac=false) {
2018-05-29 08:30:01 +00:00
$result = false;
if(is_object($obj) && property_exists($obj, $prop)) {
2018-05-28 06:52:35 +00:00
if($ac) {
$reflection = new ReflectionClass($obj);
$property = $reflection->getProperty($prop);
$property->setAccessible($ac);
$result = $property->getValue($obj);
} else {
$result = $obj->{$prop};
}
}
return $result;
}
}
2019-02-26 02:39:32 +00:00
if(check_valid_function("get_routes")) {
2018-06-11 02:50:20 +00:00
function get_routes() {
$loaded = get_scope("loaded");
return $loaded['route'];
}
}
2019-02-26 02:44:23 +00:00
set_scope("loaded", array(
"module" => array(),
"helper" => array(),
"view" => array(),
"route" => array(),
"vendor" => array(),
));
2018-04-28 15:51:59 +00:00
set_scope("errors", array());