Skip to content

Commit

Permalink
Tune Gate
Browse files Browse the repository at this point in the history
  • Loading branch information
energy-coresky committed Jan 18, 2024
1 parent a782f15 commit b6d00f5
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 113 deletions.
39 changes: 21 additions & 18 deletions assets/sky.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
var sky = {
id: 0,
tz: null,
home: '/',
scrf: '',
home: '',
tune: '',
csrf: '',
err: function(s) {
alert(s);
},
Expand Down Expand Up @@ -32,7 +33,6 @@ var sky = {
},
a: { // ajax
body: null,
div: 'main',
_0: 'main',
_1: '',
x_el: false,
Expand All @@ -57,17 +57,17 @@ var sky = {
toggle: function(el) {
$(el).html('&gt;&gt;&gt;' == $(el).html() ? '<<<' : '>>>').next().slideToggle();
},
json: function(j_, obj, func, c_, jact) {
ajax(j_, JSON.stringify(obj), func, c_, jact, 'application/json; charset=UTF-8');
json: function(j_, obj, func, c_, tune) {
ajax(j_, JSON.stringify(obj), func, c_, tune, 'application/json; charset=UTF-8');
},
post: function(url, data, func, jact, contentType) {
post: function(url, data, func, tune, contentType) {
$.ajax({
type: "POST",
url: url,
data: data,
success: func,
contentType: contentType || sky.a.contentType,
headers: {'X-Action-J': jact || 'main'}
headers: {'X-Action-J': tune || sky.tune}
});
},
f: { // forms
Expand Down Expand Up @@ -200,15 +200,15 @@ function box(html) {
sky.show();
}

function ajax(j_, postfields, func, c_, jact, contentType) {
function ajax(j_, postfields, func, c_, tune, contentType) {
if ('function' == typeof postfields) {
jact = c_;
tune = c_;
c_ = func;
func = postfields;
postfields = '';
}
if ($.isArray(j_)) {
c_ = sky.a._0 = sky.a.div = j_[0];
c_ = sky.a._0 = j_[0];
j_ = sky.a._1 = 1 == j_.length ? '' : j_[1];
}
var mem_x, to, ctrl = c_ || sky.a._0;//, ctrl0 = c_ || sky.a.div
Expand Down Expand Up @@ -241,20 +241,23 @@ function ajax(j_, postfields, func, c_, jact, contentType) {
case 'object': return func ? func.html(r) : null; // null is object
default: return r ? sky.err(r) : null;
}
}, jact, contentType);
}, tune, contentType);
}

(function($) {
sky.home = $('meta[name="sky.home"]').attr('content');
sky.scrf = $('meta[name="csrf-token"]').attr('content');
sky.home = $('meta[name="sky-home"]').attr('content');
sky.tune = $('meta[name="sky-tune"]').attr('content');
sky.csrf = $('meta[name="csrf-token"]').attr('content');

var path = sky.home.replace(/\//g, "\\/");
var m = location.href.match(new RegExp('^.+?' + path + '(\\w*)[^\\?]*(\\?(\\w+).*?)?(#.*)?$', ''));
sky.a.div = m && m[1] ? m[1] : 'main';
sky.a._0 = 'adm' == sky.a.div && m[3] ? m[3] : sky.a.div;
let s0, qs = location.href.substr(sky.home.length), p = qs.split('?', 2);
'' === sky.tune || (p[0] = p[0].substr(1 + sky.tune.length));
s0 = p[0].split('/')[0];
sky.a._0 = '' === s0 ? (p.length == 2 ? p[1].split('#')[0].split('=')[0] : 'main') : s0;
p = sky.tune.split('/');
sky.tune = p[p.length - 1];

$.ajaxSetup({
headers: {'X-Csrf-Token': sky.scrf}
headers: {'X-Csrf-Token': sky.csrf}
});
})(jQuery);

Expand Down
6 changes: 3 additions & 3 deletions sky.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ function __construct() {
define('DOMAIN', $_SERVER['SERVER_NAME']);
define('PORT', 80 == $_SERVER['SERVER_PORT'] ? '' : ':' . $_SERVER['SERVER_PORT']);
define('PATH', preg_replace("|[^/]*$|", '', $_SERVER['SCRIPT_NAME']));
define('LINK', PROTO . '://' . DOMAIN . PORT . PATH);
define('HOME', PROTO . '://' . DOMAIN . PORT . PATH);
define('URI', (string)substr($_SERVER['REQUEST_URI'], strlen(PATH))); # (string) required!
header('Content-Type: text/html; charset=UTF-8');

Expand Down Expand Up @@ -398,8 +398,8 @@ function __construct() {
if (1 == $cnt && '' === $this->surl[0]) {
$cnt = 0;
$this->surl = [];
if (0 == $this->method && ($jact = $_SERVER['HTTP_X_ACTION_J'] ?? false)) { // INPUT_POST 2do: delete the checks from gate
'main' == $jact or common_c::$tune = $jact;
if (0 == $this->method && isset($_SERVER['HTTP_X_ACTION_J'])) { // INPUT_POST 2do: delete the checks from gate
common_c::$tune = $_SERVER['HTTP_X_ACTION_J'];
$mvc->return = $this->fly = HEAVEN::J_FLY;
}
}
Expand Down
2 changes: 1 addition & 1 deletion w2/__dev.jet
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ $(function() {
@if($y_ware_dir)@use(`Ware` as bottom)~if
<!doctype html><html>
<head>@head<script>
sky.a._0 = sky.a.div = '{{$sky->_0}}'; sky.a._1 = '{{$sky->_1}}'; sky.a.uri = '{{URI}}'; sky.d.dev = {{(int)$sky->d_dev}};
sky.a._0 = '{{$sky->_0}}'; sky.a._1 = '{{$sky->_1}}'; sky.a.uri = '{{URI}}'; sky.d.dev = {{(int)$sky->d_dev}};
</script>
</head>
<body>
Expand Down
8 changes: 3 additions & 5 deletions w2/boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Boot
private static $dir;

private $at;
private $array = [];
private $array;

static function auto($v, $more = '', $func = false) {
$array = var_export($v, true);
Expand All @@ -20,6 +20,7 @@ static function auto($v, $more = '', $func = false) {
}

function __construct($dc = false) {
$this->array = [];
if (!$dc)
return;
self::$boot = true;
Expand Down Expand Up @@ -66,8 +67,7 @@ static function lint(string $in, $is_file = true) : bool {

static function yml(string $in, $is_file = true) {
self::$dir = $is_file ? dirname(realpath($in)) : '???';
if (defined('DEV'))
self::$dev = DEV;
defined('DEV') && (self::$dev = DEV);
$yml = new Boot;
$yml->at = [$is_file ? $in : false, 0];
$yml->yml_text($is_file ? file_get_contents($in) : $in);
Expand Down Expand Up @@ -311,8 +311,6 @@ static function wares($fn, &$ctrl, &$class) {
$class[$cls] = $ware;
}
}
if ($ary['tune'])
$ctrl["$ary[tune]/*"] = $ware;
$app =& $plan['app'];
unset($cfg['plans'], $app['require'], $app['class']);
$app['cfg'] = $cfg;
Expand Down
9 changes: 2 additions & 7 deletions w2/core.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

function e() {
$top = MVC::instance();
DEV && !SKY::s('gate_404') ? DEV::e($top) : $top->set(404);
}

function sqlf(...$in) { # just more quick parsing, using printf syntax. No SQL injection!
$sql = new SQL($in, 'parseF');
return $sql->exec();
Expand Down Expand Up @@ -99,7 +94,7 @@ function jump($uri = '', $code = 302, $exit = true) {
$sky->is_front or '' === $uri or '?' != $uri[0] or $uri = "adm$uri";
if (common_c::$tune && $code)
$uri = common_c::$tune . ('' === $uri || '?' == $uri[0] ? '' : '/') . $uri;
$sky->jump = LINK . $uri;
$sky->jump = HOME . $uri;
}

header("Location: $sky->jump", true, $code ?: 302);
Expand Down Expand Up @@ -345,7 +340,7 @@ function a($anchor, $href = null, $x = '') {
$x .= ' onclick="' . $href[0] . '"';
$href = 'javascript:;';
}
$href or $href = $href === null ? "http://$anchor/" : LINK;
$href or $href = $href === null ? "http://$anchor/" : HOME;
return sprintf('<a href="%s"%s>%s</a>', $href, $x ? ' ' . trim($x) : '', $anchor);
}

Expand Down
2 changes: 1 addition & 1 deletion w2/debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ static function pdaxt($plus = '') {
echo DEV ? a('P', PROTO . '://' . _PUBLIC) : L::r('P');
if (DEV) {
echo '_venus' == $sky->d_last_page
? a('V', PATH . '_venus?page=' . rawurlencode(LINK . URI))
? a('V', PATH . '_venus?page=' . rawurlencode(HOME . URI))
: a('D', ["dev('" . ($sky->d_last_page ?: '_dev') . "')"]);
}
echo a('A', PATH . $link);
Expand Down
24 changes: 12 additions & 12 deletions w2/dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ static function jet($fn) {
trace("$fn, used cached", 'JET');
}

static function gate(&$ctrl, $in, $func) {
$ware = ($set = $func($x)) ?: 'main';
$class = $set && '*' !== $in && !$x ? "c_$in" : 'default_c';
static function gate(&$p, &$in, $ware) {
$ware = $ware($x);
$class = $x ? 'default_c' : "c_$in";
$msrc = Plan::app_mq([$ware, "mvc/$class.php"]);
if ('main' != $ware) {
trace($ware, 'WARE');
} elseif (!$set && ($mt = Plan::app_mq("mvc/c_$in.php"))) {
$msrc = $mt;
$ctrl[$in] = 'main'; # added new Controller
} elseif ($x && ($_m = Plan::app_mq("mvc/c_$in.php"))) {
$msrc = $_m;
$p[$in] = 'main'; # added new Controller
$class = "c_$in";
Plan::cache_d('sky_plan.php');
} elseif (!$msrc) {
if ('default_c' == $class)
throw new Error('Controller `main::default_c` is mandatory');
unset($ctrl[$in]); # Controller deleted
unset($p[$in]); # Controller deleted
$msrc = Plan::app_m('mvc/' . ($class = 'default_c') . '.php');
Plan::cache_d('sky_plan.php');
}
Expand Down Expand Up @@ -272,7 +272,7 @@ function j_attach() {
'opt' => $class ? (new $class) : false,
'classes' => Globals::def($dir),
'name' => $name,
'dir' => $dir,
'dir' => str_replace('\\', '/', $dir),
'flags' => $flags,
'md' => $doc,
];
Expand All @@ -282,7 +282,7 @@ function j_attach() {
return $this->error('Must select at least one class');
}
$wares[$name] = [
'path' => $dir,
'path' => str_replace('\\', '/', $dir),
'class' => $cls,
'tune' => $_POST['tune'] ?? '',
];
Expand Down Expand Up @@ -347,7 +347,7 @@ function c_ware() {
'class' => $wares[$name]['class'],
'cnt' => count($wares[$name]['class']),
'desc' => $this->desc($path = Plan::_obj([$name])->path),
'path' => $path,
'path' => str_replace('\\', '/', $path),
];
},
'e_dir' => function ($row) use (&$dir) {
Expand All @@ -361,7 +361,7 @@ function c_ware() {
return [
'name' => ucfirst($ware),
'type' => $conf['app']['type'],
'path' => $path,
'path' => str_replace('\\', '/', $path),
'desc' => $this->desc($path),
];
},
Expand Down Expand Up @@ -421,7 +421,7 @@ function j_readme() {
$html = is_file($fn = "$dir/README.md") ? Display::md(file_get_contents($fn)) : '';
if (is_file($fn = "$dir/LICENSE"))
$html .= Display::bash(file_get_contents($fn));
return ['html' => $html, 'dir' => $dir];
return ['html' => $html, 'dir' => str_replace('\\', '/', $dir)];
}

function j_inet() {
Expand Down
3 changes: 2 additions & 1 deletion w2/dev_c.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function tail_y() {
return;
if (1 == $this->method && '_trace' != $this->_0 && 'view' != $this->_1)
$this->d_last_page = URI;
$this->_static = [[], ["~/m/dev.js"], ["~/m/dev.css"]];
Plan::$head = [[], ['~/m/dev.js'], ['~/m/dev.css']];

return [
'y_log' => $this->log_dt = SKY::$dd->sqlf('@select id, dt from $_memory where id>3 order by id limit 4'),
'y_tx' => '_trace' == $this->_0 ? $this->_1 : 0,
Expand Down
4 changes: 2 additions & 2 deletions w2/jet.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,11 @@ private function statements($tag, $arg, $end, &$str, &$br) {
case 'csrf':
return '<?php echo hidden() ?>';
case 'head':
return "<?php MVC::head($arg) ?>";
return "<?php Plan::echoHead($arg) ?>";
case 'tail':
$vars = DEV ? ' Debug::vars(get_defined_vars(), $sky->no);' : '';
$this->tailed = true;
return "<?php$vars HEAVEN::tail_t() ?>";
return "<?php$vars Plan::echoTail($arg) ?>";
case 'continue':
return $arg ? "<?php if ($arg): continue; endif ?>" : '<?php continue ?>';
case 'break':
Expand Down
72 changes: 20 additions & 52 deletions w2/mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function tail_y() {
function a_crash() {
global $sky;
$sky->open();
SKY::$debug = $this->_static = 0;
SKY::$debug = Plan::$head = 0;
$tracing = '';
if (DEV) {
$x = (int)('_' != $sky->_0[0] && SKY::d('tracing_toggle'));
Expand Down Expand Up @@ -388,36 +388,6 @@ static function last_modified($time, $use_site_ts = true, $func = null) {
header('Last-Modified: ' . substr(gmdate('r', $time), 0, -5) . 'GMT');
}

static function head($plus = '') {
global $sky;

if (!$sky->_title) {
$v =& MVC::instance()->_v;
if (isset($v['y_h1']))
$sky->_title = $v['y_h1'];
}
if (!$sky->_tkd)
$sky->_tkd = [$sky->s_title, $sky->s_keywords, $sky->s_description];
$sky->_title = html($sky->_title ? "$sky->_title - {$sky->_tkd[0]}" : $sky->_tkd[0]);
if ($sky->_refresh) {
list($secs, $link) = explode('!', $sky->_refresh);
$sky->_head = $sky->_head . sprintf('<meta http-equiv="refresh" content="%d%s">', $secs, $link ? ";url=$link" : '');
}
if ('' === $sky->_static) {
$fn = $sky->is_mobile ? 'mobile' : 'desktop';
$sky->_static = [[], ["~/m/$fn.js"], ["~/m/$fn.css"]]; # default app meta_tags, js, css files
} elseif (!$sky->_static) {
$sky->_static = [[], [], []];
}
echo "<title>$sky->_title</title>$plus";
echo tag($sky->_static[0] + ['csrf-token' => $sky->csrf, 'sky.home' => LINK]); # meta tags
echo js([-2 => '~/m/jquery.min.js', -1 => '~/m/sky.js'] + $sky->_static[1]);
echo css($sky->_static[2] + [-1 => '~/m/sky.css']);
if (!$sky->eview && 'crash' != $sky->_0)
echo js(common_c::head_h());
echo '<link href="' . PATH . 'm/etc/favicon.ico" rel="shortcut icon" type="image/x-icon" />' . $sky->_head;
}

static function handle($method, &$param = null, $mandatory = false) {
if (MVC::$ctrl = method_exists(MVC::$mc, $method) ? get_class(MVC::$mc) : false)
return MVC::$mc->$method($param);
Expand Down Expand Up @@ -502,34 +472,32 @@ static function jet($mvc, $layout, $vars = []) {
return Plan::jet_r($fn, $vars);
}

private function gate(&$class, &$action, &$gate) {
$ctrl =& SKY::$plans['main']['ctrl'];
private function gate(&$class, &$a, &$g) {
$p =& SKY::$plans['main']['ctrl'];
is_string($in = $this->_0) or $in = '*';
$func = function (&$x) use (&$ctrl, $in) {
$ware = function (&$x) use (&$p, &$in) {
$x = false;
if (!$y = common_c::$tune)
return $ctrl[$in] ?? false;
return $ctrl["$y/$in"] ?? $ctrl[$x = "$y/*"] ?? false;
if ($tune = common_c::$tune) {
if ($ware = $p["$tune/$in"] ?? $p[$x = "$tune/*"] ?? false)
return $ware;
global $sky;
array_unshift($sky->surl, $in = $tune);
common_c::$tune = $x = false;
}
return $p[$in] ?? ($x = 'main');
};
$vars = DEV ? (object)DEV::gate($p, $in, $ware) : false;
Plan::$view = Plan::$ware = $ware = $ware($x);
$class = $x ? (MVC::$tpl = 'default') . '_c' : 'c_' . (MVC::$tpl = $in);
$x or is_string($in = $this->_1) or $in = '*';

$vars = DEV ? (object)DEV::gate($ctrl, $in, $func) : false;
if ($set = $func($x)) {
Plan::$ware = Plan::$view = $set;
if ($set = '*' !== $in && !$x) {
$class = 'c_' . (MVC::$tpl = $in);
is_string($in = $this->_1) or $in = '*';
}
}
if (!$set)
$class = (MVC::$tpl = 'default') . '_c';
if (!Plan::gate_rq($fn = Plan::$ware . "-$class.php", $vars)) {
Plan::gate_p($fn, Gate::instance()->parse(Plan::$ware, "mvc/$class.php", false));
if (!Plan::gate_rq($fn = "$ware-$class.php", $vars, false)) {
Plan::gate_p($fn, Gate::instance()->parse($ware, "mvc/$class.php", false));
Plan::gate_r($fn, $vars);
}
$x = $this->return ? 'j' : 'a';
$action = '' === $in ? "empty_$x" : $x . '_' . $in;
$cls_g = $class . '_G';
method_exists($gate = new $cls_g, $action) or $action = "default_$x";
$gate = $class . '_G';
method_exists($g = new $gate, $a = '' === $in ? "empty_$x" : "{$x}_$in") or $a = "default_$x";
}

function top() {
Expand Down
Loading

0 comments on commit b6d00f5

Please sign in to comment.