'/helpers/',
'IMAGES' => '/images/',
'LAYOUTS' => '/layouts/',
'LOCALES' => '/locales/',
'PAGES' => '/pages/',
'SCRIPTS' => '/scripts/',
'STYLES' => '/styles/'
];
$define['SET'] = [
'ERRORS' => false,
'INDEX' => 'index',
'LAYOUT' => null,
'LOCALE' => null,
'MINIFY' => true
];
// -.-. --- -. .--- ..- .-. .. -. --.
(function () use (&$define) {
$app = [
'DIR' => dirname($_SERVER['SCRIPT_FILENAME'] ?? __FILE__) . '/',
'ROOT' => rtrim($_SERVER['DOCUMENT_ROOT'] ?? '', '/') . '/',
'START' => $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true),
'QUERY' => $_SERVER['QUERY_STRING'] ?? '',
'URI' => urldecode(strtok($_SERVER['REQUEST_URI'] ?? '/', '?'))
];
$app['ROOT'] = rtrim(realpath($app['ROOT']) ?: $app['ROOT'], '/') . '/';
define('APP', array_merge($app, [
'DIR' => strtr($app['DIR'], '\\', '/'),
'ROOT' => substr(strtr($app['DIR'], '\\', '/'), strlen($app['ROOT']) - 1)
]));
if (file_exists('.env') || file_exists('.env.example')) {
$envs = file_exists('.env') ? file('.env') : file('.env.example');
foreach (array_filter(array_map('trim', $envs)) as $env) {
if (substr($env, 0, 1) != '#') {
putenv($env);
}
}
if (file_exists('.gitignore')) {
$gitignore = array_filter(array_map('trim', file('.gitignore')));
if (!in_array('.env', $gitignore) && !in_array('*', $gitignore)) {
file_put_contents('.gitignore', "\n.env", FILE_APPEND);
}
}
}
if (str_contains($_SERVER['SERVER_SOFTWARE'] ?? '', 'Apache')) {
if (!file_exists('.htaccess')) {
$htaccess = implode("\n", [
'
[\'"])(.*?)(?P=q)', '\$[a-z_]\w*(->\w+(\s*\(([^()]|\([^()]*\))*\))?)*', '(?:php|scribe|relay|path|env)', '\(((?:[^()]|\([^()]*\))*)\)' ], [ "/<--\s.*?-->/s", "/<(if|elseif|foreach)\s+{attribute}\s*>/s", "/<\/(if|foreach)>/", "/<(else)\s*\/?>/", "/<(break|continue)\s*\/?>/", "//s", "/<({allow})\s+{attribute}\s*\/?>/s", "/(?: )?/s", "/:\s*=?\s*({allow}\s*{argument})/si", "/:\s*=?\s*({variable})/i" ]), [ '', '', '', '', '', '', '= $1($3); ?>', '= php($2, true); ?>', '= $1; ?>', '= php($1, true); ?>' ], file_get_contents(VIEWFILE))); } }, true); } if (defined('REDIRECT')) { if (!parse_url(REDIRECT, PHP_URL_HOST)) { $redirect = path(REDIRECT); } exit(header('Location: ' . ($redirect ?? REDIRECT))); } elseif (defined('ROUTES')) { $facade = array_diff_assoc(URI, $path); foreach (ROUTES as $route) { if (!is_array($route)) { $route = explode('/', trim($route, '/')); } if (count($route) === count($facade)) { foreach (array_values($facade) as $increment => $segment) { if (is_array($route[$increment])) { if (!in_array($segment, $route[$increment])) { break; } } elseif (!in_array($route[$increment], ['*', $segment])) { break; } if ($increment === count($facade) - 1) { $path = $path + $facade; break 2; } } } } } if (array_diff(URI, $path)) { exit(header('Location: ' . path(implode('/', $path)))); } else { if (defined('LAYOUT') || !empty(SET['LAYOUT'])) { $layout = defined('LAYOUT') ? LAYOUT : SET['LAYOUT']; $layout = path(DIR['LAYOUTS'] . "/{$layout}.php", true); if (file_exists($layout)) { define('LAYOUTFILE', $layout); foreach ([ 'js' => 'SCRIPTS', 'css' => 'STYLES' ] as $extension => $constant) { $assets = array_merge([ trim(DIR['LAYOUTS'], '/') . ".{$extension}", (defined('LAYOUT') ? LAYOUT : SET['LAYOUT']) . ".{$extension}" ], preg_filter("/$/", ".{$extension}", PATHS)); relay($constant, function () use ($assets, $constant) { $html = [ 'SCRIPTS' => '', 'STYLES' => '' ]; foreach ($assets as $asset) { $asset = path([$constant, $asset], true); if (file_exists($asset)) { $asset = "/{$asset}?m=" . filemtime($asset); $asset = path(str_replace(APP['DIR'], '', $asset)); echo sprintf($html[$constant], $asset); } } }, true); } } } } })(); (function () { ob_start(function ($content) { if (SET['MINIFY']) { return preg_replace(array_keys($minify = [ "/\>\h+$/m" => ">", "/\>[^\S ]+/m" => ">", "/^\h+\ "<", "/[^\S ]+\ "<", "/\>\s{2,}\" => "><" ]), $minify, $content); } else { return $content; } }); if (defined('LAYOUTFILE')) { extract(HELPERS, EXTR_SKIP); require LAYOUTFILE; } else { echo CONTENT; } ob_end_flush(); })(); })(); function env($variable, $default = null) { $variable = getenv($variable) ?: $default; if (in_array($variable, ['true', 'false', 'null'], true)) { return json_decode($variable); } return $variable; } function path($locator = null, $actual = false) { if (is_null($locator)) { return str_replace('//', '/', '/' . implode('/', URI)); } elseif (is_int($locator)) { return URI[$locator] ?? null; } else { $prepend = $actual ? APP['DIR'] : APP['ROOT']; if (is_array($locator)) { list($define, $locator) = [$locator[0], $locator[1] ?? null]; if (!is_null($define)) { $define = DIR[strtoupper($define)]; if (isset($define) && !empty($define)) { $locator = "{$define}/{$locator}"; } } } if (!$actual && !str_contains($locator, '.')) { if (defined('LOCALE')) { $prepend = LOCALE['URI']; } if (!str_contains($locator, '?')) { $locator = rtrim("{$locator}", '/'); } } $locator = "{$prepend}/{$locator}"; $locator = preg_replace("#(^|[^:])//+#", "\\1/", $locator); return $locator; } } function relay($name, $content = null, $define = false) { $name = $define ? strtoupper($name) : strtolower($name); static $bag = []; if (is_null($content)) { return $bag[$name] ?? null; } else { if ($content instanceof closure) { ob_start(); $content(); $content = ob_get_clean(); } if ($define === true && !defined($name)) { define($name, $content); } else { $bag[$name] = $content; } } } function scribe($string, $replace = []) { if (is_array($string)) { list($string, $return) = [$string[0], $string[1] ?? '']; } if (defined('TRANSCRIPT')) { if (array_key_exists($string, TRANSCRIPT)) { list($string, $return) = [TRANSCRIPT[$string], null]; } } if (isset($return)) { $string = $return !== '' ? $return : null; } elseif (!empty($replace)) { $string = strtr($string, $replace); } return $string; }