reasonableframework/system/base.php

292 lines
5.8 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
*/
2018-05-24 06:10:17 +00:00
// get all scope
if(!function_exists("get_scope_all")) {
function get_scope_all() {
global $scope;
return $scope;
}
}
2018-04-13 05:04:25 +00:00
// set scope
if(!function_exists("set_scope")) {
function set_scope($k, $v) {
global $scope;
$scope[$k] = $v;
}
}
// get scope
if(!function_exists("get_scope")) {
function get_scope($k) {
global $scope;
return array_key_exists($k, $scope) ? $scope[$k] : null;
}
}
// register loaded resources
if(!function_exists("register_loaded")) {
function register_loaded($k, $v) {
global $scope;
2018-06-11 02:50:20 +00:00
$scope['loaded'][$k][] = $v;
2018-04-13 05:04:25 +00:00
}
}
// sandbox for include function
if(!function_exists("include_isolate")) {
function include_isolate($file, $data=array()) {
if(count($data) > 0) {
extract($data);
}
return include($file);
}
}
2018-04-13 05:38:39 +00:00
// load view file
if(!function_exists("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
if(!function_exists("renderViewByRules")) {
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
2018-04-13 05:38:39 +00:00
if(!function_exists("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
if(!function_exists("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
if(!function_exists("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) {
2018-06-11 02:50:20 +00:00
$routefile = './route/' . $name2 . '.php';
2018-04-13 05:04:25 +00:00
if(file_exists($routefile)) {
2018-06-11 02:50:20 +00:00
register_loaded("route", $name2);
2018-04-13 17:00:23 +00:00
$flag = $flag && !include_isolate($routefile, $data);
2018-04-28 15:49:22 +00:00
} 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-04-13 05:38:39 +00:00
if(!function_exists("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-06-11 02:50:20 +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
2018-06-04 10:13:37 +00:00
if(!function_exists("array_key_equals")) {
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;
}
}
2018-04-13 05:38:39 +00:00
if(!function_exists("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
}
}
2018-03-23 09:24:05 +00:00
if(!function_exists("get_value_in_array")) {
function get_value_in_array($name, $arr=array(), $default=0) {
$output = 0;
if(is_array($arr)) {
$output = array_key_empty($name, $arr) ? $default : $arr[$name];
} else {
$output = $default;
}
return $output;
}
}
2018-04-13 08:02:49 +00:00
if(!function_exists("get_value_in_object")) {
function get_value_in_object($name, $obj, $default="") {
$output = $obj->$name;
return $output;
}
}
2018-04-28 15:45:21 +00:00
// error handler
if(!function_exists("set_error")) {
function set_error($msg, $code="ERROR") {
global $scope;
$scope['errors'][] = $code . ": " . $msg;
}
}
if(!function_exists("get_errors")) {
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;
}
}
if(!function_exists("show_errors")) {
function show_errors($exit=true) {
return get_errors(true, $exit);
}
}
2018-08-24 14:57:09 +00:00
if(!function_exists("set_error_exit")) {
function set_error_exit($msg, $code="ERROR") {
set_error($msg, $code);
show_errors();
}
}
2018-04-28 16:33:03 +00:00
// check function exists
if(!function_exists("check_function_exists")) {
function check_function_exists($rules) {
$flag = true;
if(is_string($rules)) {
$rules = explode(";", $rules);
}
foreach($rules as $k=>$v) {
$exists = function_exists($k);
$flag = $flag && !$exists;
if($exists === false) {
2018-04-28 16:34:48 +00:00
if(empty($v)) {
2018-04-28 16:33:03 +00:00
set_error("Function " . $k . " dose not exists");
} else {
set_error($v);
}
}
}
return !$flag;
}
}
2018-05-28 06:52:35 +00:00
if(!function_exists("get_property_value")) {
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;
}
}
2018-06-11 02:50:20 +00:00
if(!function_exists("get_routes")) {
function get_routes() {
$loaded = get_scope("loaded");
return $loaded['route'];
}
}
2018-05-29 08:30:01 +00:00
2018-04-13 05:04:25 +00:00
$scope = array();
set_scope("loaded", array(
"module" => array(),
"helper" => array(),
"view" => array(),
"route" => array(),
));
2018-04-28 15:51:59 +00:00
set_scope("errors", array());