Skip to content

Commit

Permalink
Merge branch 'master' into v5-demo
Browse files Browse the repository at this point in the history
  • Loading branch information
playadmin committed Oct 24, 2023
2 parents d0a4a1a + 7917283 commit 7e665cf
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 49 deletions.
43 changes: 30 additions & 13 deletions core.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ function AppRun(string $entry, array $nec = null): void

if (!defined('ROUTE')) {
$router = [
'mod'=>0,
'ctrl' => empty($_GET['c']) ? 'index' : $_GET['c'],
'act' => empty($_GET['a']) ? 'index' : $_GET['a'],
'uri' => $_SERVER['REQUEST_URI'],
'query'=> $_GET,
];
empty($GLOBALS['ZPHP_CONFIG']['ROUTER']['module']) || $router['module'] = empty($_GET['m']) ? 'index' : $_GET['m'];
define('ROUTE', $router);
Expand Down Expand Up @@ -145,40 +148,54 @@ function ArrayIsList (&$arr) {
}
return $isList;
}
function ExportArray ($arr, $quoteValStr = true, $quoteKeyStr = true) {
if (!is_array($arr) && !is_object($arr)) {
return var_export($arr, true);
}
if (!$arr || (!is_array($arr) && !$arr = (array)$arr)) {
function ExportArray (array $arr, bool $escape = false, string $indent = '', $prefix = '', $indentNum = 0, $forceStringValue = true) {
if (!$arr) {
return '[]';
}

if ($escape) {
$search = ["'", "\n","\t","\r","\f","\\","\v"];
$replace = ["\'", '\n','\t','\r','\f','\\','\v'];
} else {
$search = "'";
$replace = "\'";
}

if (ArrayIsList($arr)) {
foreach($arr as $v) {
if (is_array($v)) {
$slice[] = ExportArray($v);
$slice[] = ExportArray($v, $escape, $indent, $prefix, 1 + $indentNum, $forceStringValue);
} elseif (null === $v) {
$slice[] = 'null';
} else {
$quoteValStr && is_string($v) && $v = "'" . str_replace("'", "\'", $v) . "'";
($forceStringValue || is_string($v)) && $v = "'" . str_replace($search, $replace, $v) . "'";
$slice[] = $v;
}
}
} else {
foreach($arr as $k=>$v) {
is_string($k) && $k = str_replace(["'", '"'], ["\'", '\"'], $k);
$key = $quoteKeyStr && is_string($k) ? "'{$k}'=>" : "{$k}=>";
if (is_string($k)) {
$k = str_replace($search, $replace, $k);
$key = "'{$k}'=>";
} else {
$key = "{$k}=>";
}
if (is_array($v)) {
$slice[] = $key . ExportArray($v);
$slice[] = $key . ExportArray($v, $escape, $indent, $prefix, 1 + $indentNum, $forceStringValue);
} elseif (null === $v) {
$slice[] = "{$key}null";
} else {
$quoteValStr && is_string($v) && $v = "'" . str_replace("'", "\'", $v) . "'";
($forceStringValue || is_string($v)) && $v = "'" . str_replace($search, $replace, $v) . "'";
$slice[] = $key . $v;
}
}
}
return '[' . implode(',', $slice) . ']';
if ($indent) {
$pre = $prefix . str_repeat($indent, $indentNum);
return "[\n{$pre}{$indent}" . implode(",\n{$pre}{$indent}", $slice) . "\n{$pre}]";
} else {
return '[' . implode(',', $slice) . ']';
}
}
function P($var, bool $echo = true): string
{
Expand Down Expand Up @@ -271,7 +288,7 @@ function Page($cfg, $return = false): array
$var = $cfg['var'] ?? 'p';
$mod = $cfg['mod'] ?? null;
$nourl = $cfg['nourl'] ?? 'javascript:;';
$params = ROUTE['params'] ?? false;
$params = ROUTE['params'] ?? [];
$query = $_GET;
foreach ($return as $v) {
switch ($v) {
Expand Down
8 changes: 4 additions & 4 deletions lib/z/dbc/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function cacheDir (string $dbname = '', string $table = '')
$dbname || $dbname = $this->DB_CONFIG['dbname'];
return P_CACHE . "{$this->DB_CONFIG['cache_dir']}/{$dbname}/{$table}";
}
function CleanCache (string $mod = '', string $dbname, string $table = '')
function CleanCache (string $mod = '', string $dbname, string $table = ''): int
{
$mod || $mod = $this->DB_CONFIG['cache_mod'];
switch ($mod) {
Expand All @@ -44,11 +44,11 @@ function CleanCache (string $mod = '', string $dbname, string $table = '')
return $this->delMemcached($dbname, $table);
break;
default:
return $this->delFile($dbname, $table);
return (int)($this->delFile($dbname, $table));
break;
}
}
private function delFile(string $dbname = '', string $table = '')
private function delFile(string $dbname = '', string $table = ''): int | false
{
$dbname || $dbname = $this->DB_CONFIG['dbname'];
$dir = $this->cacheDir($dbname, $table);
Expand All @@ -72,7 +72,7 @@ protected function delRedis (string $dbname = '', string $table = ''): int
}
return $result;
}
protected function delMemcached($dbname = '', $table)
protected function delMemcached($dbname = '', $table): int
{
$dbname || $dbname = $this->DB_CONFIG['dbname'];
$path = "DB:{$dbname}:";
Expand Down
33 changes: 23 additions & 10 deletions lib/z/dbc/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ public function Wrap (string|array $key, string $alias = '')
return is_array($key) ? $alias . implode(",{$alias}", $key) : $alias . $key;
}
if (is_array($key)) {
$keys = array_map(function ($k) {
return $this->WrapSql($k);
}, $key);
$keys = [];
foreach ($key as $k=>$v) {
if (is_int($k)) {
$keys[] = $this->WrapSql($v);
} else {
$keys[] = $this->WrapSql($k) . " AS {$this->DB_PREG_WRAP_L}{$v}{$this->DB_PREG_WRAP_R}";
}
}
return implode(',', $keys);
}
return $this->WrapSql($key);
Expand Down Expand Up @@ -185,7 +190,7 @@ public function Commit(): bool
}
public function Tmp(string $sql, string $alias = 'z'): static
{
$this->DB_TMP = "({$sql}) AS {$alias}";
$this->DB_TMP = "({$sql}) AS {$this->DB_PREG_WRAP_L}{$alias}{$this->DB_PREG_WRAP_R}";
return $this;
}
public function Table(string $table): static
Expand Down Expand Up @@ -423,10 +428,14 @@ public function Fetch(bool $lock = false): \PDOStatement
$this->DB_done();
return $stmt;
}
public function Find(string $field = '', bool $lock = false)
public function Find(string | bool $field = false, bool $lock = false)
{
$fetch = \PDO::FETCH_ASSOC;
$field && ($this->DB_FIELD = $this->WrapSql($field)) && $fetch = \PDO::FETCH_COLUMN;
if ($field) {
$fetch = \PDO::FETCH_COLUMN;
is_bool($field) || $this->DB_FIELD = $this->WrapSql($field);
} else {
$fetch = \PDO::FETCH_ASSOC;
}
$sql = $this->DB_sql();
$field = $this->DB_field();
$sql = "SELECT {$field} FROM {$sql}";
Expand Down Expand Up @@ -499,10 +508,14 @@ public function CacheSelect(int $fetch): array
}
return $result;
}
public function Select(string $field = null, bool $lock = false): array
public function Select(string | bool $field = false, bool $lock = false): array
{
$fetch = \PDO::FETCH_ASSOC;
$field && ($this->DB_FIELD = $this->WrapSql($field)) && $fetch = \PDO::FETCH_COLUMN;
if ($field) {
$fetch = \PDO::FETCH_COLUMN;
is_bool($field) || $this->DB_FIELD = $this->WrapSql($field);
} else {
$fetch = \PDO::FETCH_ASSOC;
}
if ($lock || !$this->Z_CACHE) {
$field = $this->DB_field();
$this->DB_PAGE && $this->DB_page();
Expand Down
31 changes: 13 additions & 18 deletions lib/z/input.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,9 @@ public static function Input (string $key, string $dim = '', callable $filter =
if ($key) {
$data = match (METHOD) {
'GET'=>$_GET[$key] ?? null,
'POST'=>$_POST[$key] ?? null,
default=> INPUT[$key],
} ?? $_GET[$key] ?? $_POST[$key] ?? INPUT[$key] ?? null;
'POST'=>$_POST[$key] ?? $_GET[$key] ?? null,
default=> INPUT[$key] ?? $_GET[$key] ?? null,
};
if (null === $data) {
return null;
}
Expand Down Expand Up @@ -670,50 +670,45 @@ public static function InputInt (string $key, string $dim = '', callable $filter
{
$data = match (METHOD) {
'GET'=>$_GET[$key] ?? null,
'POST'=>$_POST[$key] ?? null,
default=> INPUT,
'POST'=>$_POST[$key] ?? $_GET[$key] ?? null,
default=> INPUT[$key] ?? $_GET[$key] ?? null,
};
null === $data && $data = $_GET[$key] ?? null;
return null === $data ? null : self::ParseInt($data, $dim, $filter);
}
public static function InputFloat (string $key, string $dim = '', callable $filter = null): float|array|null
{
$data = match (METHOD) {
'GET'=>$_GET[$key] ?? null,
'POST'=>$_POST[$key] ?? null,
default=> INPUT,
'POST'=>$_POST[$key] ?? $_GET[$key] ?? null,
default=> INPUT[$key] ?? $_GET[$key] ?? null,
};
null === $data && $data = $_GET[$key] ?? null;
return null === $data ? null : self::ParseFloat($data, $dim, $filter);
}
public static function InputFixed (string $key, string $dim = '', callable $filter = null): float|array|null
{
$data = match (METHOD) {
'GET'=>$_GET[$key] ?? null,
'POST'=>$_POST[$key] ?? null,
default=> INPUT,
'POST'=>$_POST[$key] ?? $_GET[$key] ?? null,
default=> INPUT[$key] ?? $_GET[$key] ?? null,
};
null === $data && $data = $_GET[$key] ?? null;
return null === $data ? null : self::ParseFixed($data, $dim, $filter);
}
public static function InputMoney (string $key, string $dim = '', callable $filter = null)
{
$data = match (METHOD) {
'GET'=>$_GET[$key] ?? null,
'POST'=>$_POST[$key] ?? null,
default=> INPUT,
'POST'=>$_POST[$key] ?? $_GET[$key] ?? null,
default=> INPUT[$key] ?? $_GET[$key] ?? null,
};
null === $data && $data = $_GET[$key] ?? null;
return null === $data ? null : self::ParseMoney($data, $dim, $filter);
}
public static function InputBool (string $key, bool $toInt = false): bool|int|null
{
$data = match (METHOD) {
'GET'=>$_GET[$key] ?? null,
'POST'=>$_POST[$key] ?? null,
default=> INPUT,
'POST'=>$_POST[$key] ?? $_GET[$key] ?? null,
default=> INPUT[$key] ?? $_GET[$key] ?? null,
};
null === $data && $data = $_GET[$key] ?? null;
null === $data || $data = filter_var($data, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE);
if (null === $data) {
return null;
Expand Down
1 change: 1 addition & 0 deletions nec/z/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static function setup(): void {
$route['query'] = isset($route['params']) ? $route['params'] + $_GET : $_GET;
$route['uri'] = $_SERVER['REQUEST_URI'];
$route['app'] = APP_NAME;
$route['mod'] = self::$MOD;
define('ROUTE', $route);
});
}
Expand Down
25 changes: 21 additions & 4 deletions nec/z/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,30 @@ private static function replaceTemplate(\DOMDocument $dom, string $file = ''): v
}
}

private static function checkTplParams (&$params)
{
foreach ($params as &$v) {
if (is_array($v)) {
self::checkTplParams($v);
} elseif (is_string($v)) {
$v = str_replace(['<?', '?>'], ['&lt;?', '?&gt;'], $v);
}
}
}

public static function GetRun (string $tpl): array
{
$arr = explode('/', $tpl);
$len = count($arr);
$name = $arr[$len - 1];
return [P_RUN . APP_NAME . '/' . THEME, $arr[$len - 2] . '/' . explode('.', $name)[0] . '.php'];
}
public static function Fetch(string $filename = ''): string
public static function Fetch(string $filename = '', bool $isDisplay = false): string
{
self::$PARAMS && extract(self::$PARAMS);
if (self::$PARAMS) {
$isDisplay || self::checkTplParams(self::$PARAMS);
extract(self::$PARAMS);
}
ob_start() && require self::GetCompile($filename);
return ob_get_clean();
}
Expand Down Expand Up @@ -427,7 +441,7 @@ private static function replaceCustomTags(string $name, array $cfg): void
$var = '$var';
}
$code = str_contains($var, ',') ? "list({$var})=" : "{$var}=";
$code .= $call . '(' . ExportArray($args, false) . ');?';
$code .= $call . '(' . ExportArray($args) . ');?';
} else {
list($pre, $dd) = $call($attrs, $args);
$code = $pre . '?';
Expand Down Expand Up @@ -480,7 +494,10 @@ public static function Display(string $name = ''): void
{
if (self::$CACHE) {
$ret = cache::SetFileCache(self::$CACHE[1], function (): string {
self::$PARAMS && extract(self::$PARAMS);
if (self::$PARAMS) {
self::checkTplParams(self::$PARAMS);
extract(self::$PARAMS);
}
ob_start() && require self::GetCompile(self::$CACHE[0]);
return ob_get_clean();
});
Expand Down

0 comments on commit 7e665cf

Please sign in to comment.