reasonableframework/system/base.php

382 lines
9.4 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")) {
2019-05-20 08:19:05 +00:00
function check_invalid_function($fn) {
$status = -1;
if(is_array($fn)) {
foreach($fn as $k=>$v) {
if(!function_exists($v)) {
$status = $k;
break;
}
}
} else {
if(!function_exists($fn)) {
$status = 0;
}
}
return $status;
}
2019-02-26 02:17:44 +00:00
}
2019-02-26 05:36:04 +00:00
// check function exists (bool)
2019-03-05 01:06:58 +00:00
if(!(check_invalid_function("check_function_exists") < 0)) {
2019-05-20 08:19:05 +00:00
function check_function_exists($fn) {
return (check_invalid_function($fn) < 0);
}
2019-02-26 02:17:44 +00:00
}
2018-04-13 05:04:25 +00:00
// set scope
2019-02-26 05:40:03 +00:00
if(!check_function_exists("set_scope")) {
2019-05-20 08:19:05 +00:00
function set_scope($k, $v) {
global $scope;
$scope[$k] = $v;
}
2018-04-13 05:04:25 +00:00
}
// get scope
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_scope")) {
2019-05-20 08:19:05 +00:00
function get_scope($k) {
global $scope;
return array_key_exists($k, $scope) ? $scope[$k] : null;
}
2018-04-13 05:04:25 +00:00
}
// register loaded resources
2019-02-26 05:40:03 +00:00
if(!check_function_exists("register_loaded")) {
2019-05-20 08:19:05 +00:00
function register_loaded($k, $v) {
$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 05:40:03 +00:00
if(!check_function_exists("include_isolate")) {
2019-05-20 08:19:05 +00:00
function include_isolate($file, $data=array()) {
if(count($data) > 0) {
extract($data);
}
return include($file);
}
2018-04-13 05:04:25 +00:00
}
2018-12-29 04:20:20 +00:00
// set autoloader
2019-02-26 05:40:03 +00:00
if(!check_function_exists("set_autoloader")) {
2019-05-20 08:19:05 +00:00
function set_autoloader() {
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 05:40:03 +00:00
if(!check_function_exists("renderView")) {
2019-05-20 08:19:05 +00:00
function renderView($name, $data=array()) {
$flag = true;
$views = explode(';', $name);
foreach($views as $name2) {
$viewfile = './view/' . $name2 . '.php';
if(file_exists($viewfile)) {
register_loaded("view", $name2);
$flag = $flag && !include_isolate($viewfile, $data);
}
}
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 05:40:03 +00:00
if(!check_function_exists("renderViewByRules")) {
2019-05-20 08:19:05 +00:00
function renderViewByRules($rules, $data=array()) {
foreach($rules as $k=>$v) {
if(in_array($k, get_routes())) {
renderView($v, $data);
}
}
}
2018-06-11 06:47:22 +00:00
}
2018-02-26 10:58:54 +00:00
// load system module
2019-02-26 05:40:03 +00:00
if(!check_function_exists("loadModule")) {
2019-05-20 08:19:05 +00:00
function loadModule($name) {
$flag = true;
$modules = explode(';', $name);
foreach($modules as $name2) {
$systemfile = './system/' . $name2 . '.php';
if(file_exists($systemfile)) {
register_loaded("system", $name2);
$flag = $flag && !include_isolate($systemfile);
} else {
set_error("Module " . $name . "dose not exists");
}
}
return !$flag;
}
2018-02-26 10:58:54 +00:00
}
2018-04-13 05:38:39 +00:00
// load helper file
2019-02-26 05:40:03 +00:00
if(!check_function_exists("loadHelper")) {
2019-05-20 08:19:05 +00:00
function loadHelper($name) {
$flag = true;
$helpers = explode(';', $name);
foreach($helpers as $name2) {
$helperfile = './helper/' . $name2 . '.php';
if(file_exists($helperfile)) {
register_loaded("helper", $name2);
$flag = $flag && !include_isolate($helperfile);
} else {
set_error("Helper " . $name . "dose not exists");
}
}
return !$flag;
}
2017-12-31 18:31:02 +00:00
}
2018-04-13 05:38:39 +00:00
// load route file
2019-02-26 05:40:03 +00:00
if(!check_function_exists("loadRoute")) {
2019-05-20 08:19:05 +00:00
function loadRoute($name, $data=array()) {
$flag = true;
$routes = explode(";", $name);
foreach($routes as $name2) {
$routefile = './route/' . $name2 . '.php';
if(file_exists($routefile)) {
register_loaded("route", $name2);
$flag = $flag && !include_isolate($routefile, $data);
} else {
set_error("Route " . $name . "dose not exists");
}
}
return !$flag;
}
2018-03-08 10:01:12 +00:00
}
2018-09-20 03:24:49 +00:00
// load vendor file
2019-02-26 05:40:03 +00:00
if(!check_function_exists("loadVendor")) {
2019-05-20 08:19:05 +00:00
function loadVendor($uses, $data=array()) {
$flag = true;
$usenames = array();
if(is_string($uses) && !empty($uses)) {
$usenames[] = $uses;
} elseif(is_array($uses)) {
$usenames = array_merge($usenames, $uses);
} else {
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;
}
2018-09-20 03:24:49 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("array_key_empty")) {
2019-05-20 08:19:05 +00:00
function array_key_empty($key, $array) {
$flag = true;
if(is_array($array)) {
if(array_key_exists($key, $array)) {
$flag = $flag && empty($array[$key]);
}
}
return $flag;
}
2017-12-31 16:15:38 +00:00
}
2017-12-31 17:00:50 +00:00
2019-02-26 05:40:03 +00:00
if(!check_function_exists("array_key_equals")) {
2019-05-20 08:19:05 +00:00
function array_key_equals($key, $array, $value) {
$flag = false;
2018-06-04 10:13:37 +00:00
2019-05-20 08:19:05 +00:00
if(is_array($array)) {
if(array_key_exists($key, $array)) {
$flag = ($array[$key] == $value);
}
}
2018-06-04 10:13:37 +00:00
2019-05-20 08:19:05 +00:00
return $flag;
}
2018-06-04 10:13:37 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("array_key_is_array")) {
2019-05-20 08:19:05 +00:00
function array_key_is_array($key, $array) {
$flag = false;
2018-11-26 10:33:43 +00:00
2019-05-20 08:19:05 +00:00
if(is_array($array)) {
if(array_key_exists($key, $array)) {
$flag = is_array($array[$key]);
}
}
2018-11-26 10:33:43 +00:00
2019-05-20 08:19:05 +00:00
return $flag;
}
2018-11-26 10:33:43 +00:00
}
2019-04-01 08:07:30 +00:00
if(!check_function_exists("array_keys_empty")) {
2019-05-20 08:19:05 +00:00
function array_keys_empty($keys, $array) {
$flag = false;
foreach($keys as $key) {
if(array_key_empty($key, $array)) {
$flag = $key;
}
}
return $flag;
}
2018-02-14 04:45:33 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_value_in_array")) {
2019-05-20 08:19:05 +00:00
function get_value_in_array($name, $arr=array(), $default=false) {
$output = false;
$_name = "";
if(is_array($name)) {
foreach($name as $w) {
if(!empty($w)) {
$_name = $w;
break;
}
}
} else {
$_name = $name;
}
if(is_array($arr)) {
$output = array_key_empty($_name, $arr) ? $default : $arr[$_name];
} else {
$output = $default;
}
return $output;
}
2018-03-23 09:24:05 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_value_in_object")) {
2019-05-20 08:19:05 +00:00
function get_value_in_object($name, $obj, $default="") {
$output = $obj->$name;
return $output;
}
2018-04-13 08:02:49 +00:00
}
2019-04-10 11:08:27 +00:00
if(!check_function_exists("check_array_length")) {
2019-05-20 08:19:05 +00:00
function check_array_length($arr, $len) {
2019-06-01 10:32:01 +00:00
return ((!is_array($arr) ? -1 : count($arr)) - $len);
2019-05-20 08:19:05 +00:00
}
2019-04-10 11:08:27 +00:00
}
2019-05-23 07:38:26 +00:00
if(!check_function_exists("check_is_empty")) {
function check_is_empty($v, $d=true) {
return (empty($v) ? $d : false);
}
}
2019-04-10 11:08:27 +00:00
2018-04-28 15:45:21 +00:00
// error handler
2019-02-26 05:40:03 +00:00
if(!check_function_exists("set_error")) {
2019-05-20 08:19:05 +00:00
function set_error($msg, $code="ERROR") {
global $scope;
$scope['errors'][] = $code . ": " . $msg;
}
2018-04-28 15:45:21 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_errors")) {
2019-05-20 08:19:05 +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;
}
2018-04-28 15:45:21 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("show_errors")) {
2019-05-20 08:19:05 +00:00
function show_errors($exit=true) {
return get_errors(true, $exit);
}
2018-04-28 15:45:21 +00:00
}
2019-06-12 11:39:09 +00:00
if(!check_function_exists("do_error")) {
function do_error($msg, $code="ERROR") {
2019-05-20 08:19:05 +00:00
set_error($msg, $code);
show_errors();
}
2019-06-12 11:39:33 +00:00
}
2018-04-28 16:33:03 +00:00
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_property_value")) {
2019-05-20 08:19:05 +00:00
function get_property_value($prop, $obj, $ac=false) {
$result = false;
if(is_object($obj) && property_exists($obj, $prop)) {
if($ac) {
$reflection = new ReflectionClass($obj);
$property = $reflection->getProperty($prop);
$property->setAccessible($ac);
$result = $property->getValue($obj);
} else {
$result = $obj->{$prop};
}
}
return $result;
}
2018-05-28 06:52:35 +00:00
}
2019-02-26 05:40:03 +00:00
if(!check_function_exists("get_routes")) {
2019-05-20 08:19:05 +00:00
function get_routes() {
$loaded = get_scope("loaded");
return $loaded['route'];
}
2018-06-11 02:50:20 +00:00
}
2019-04-01 08:07:30 +00:00
2019-06-12 11:39:09 +00:00
// Deprecated: array_multikey_empty() is changed to array_keys_empty(), since version 1.2
2019-04-01 08:07:30 +00:00
if(!check_function_exists("array_multikey_empty")) {
2019-05-20 08:19:05 +00:00
function array_multikey_empty($keys, $array) {
return array_keys_empty($keys, $array);
}
2019-04-01 08:07:30 +00:00
}
2019-06-12 11:39:09 +00:00
// Deprecated: set_error_exit() is changed to do_error()
if(!check_function_exists("set_error_exit")) {
function set_error_exit($msg, $code="ERROR") {
do_error($msg, $code);
}
}
2019-03-28 05:06:59 +00:00
$loaded = array(
2019-05-20 08:19:05 +00:00
"module" => array(),
"helper" => array(),
"view" => array(),
"route" => array(),
"vendor" => array(),
2019-03-28 05:06:59 +00:00
);
$errors = array();
2018-04-28 15:51:59 +00:00
2019-03-28 05:06:59 +00:00
set_scope("loaded", $loaded);
set_scope("errors", $errors);