Some minor additions.
This commit is contained in:
parent
2ccddd390d
commit
e3722486f9
3 changed files with 74 additions and 77 deletions
|
@ -27,19 +27,58 @@ class Application
|
||||||
return static::getInstance();
|
return static::getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
private $router = null;
|
public static function gitCommitInfo(string $format): string
|
||||||
private $templating = null;
|
{
|
||||||
private $configuration = null;
|
return trim(shell_exec(sprintf('git log --pretty="%s" -n1 HEAD"', $format)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function gitCommitHash(bool $long = false): string
|
||||||
|
{
|
||||||
|
return self::gitCommitInfo($long ? '%H' : '%h');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function gitBranch(): string
|
||||||
|
{
|
||||||
|
return trim(shell_exec('git rev-parse --abbrev-ref HEAD'));
|
||||||
|
}
|
||||||
|
|
||||||
|
private $modules = [];
|
||||||
|
|
||||||
|
public function __get($name)
|
||||||
|
{
|
||||||
|
if (starts_with($name, 'has') && strlen($name) > 3 && ctype_upper($name[3])) {
|
||||||
|
$name = lcfirst(substr($name, 3));
|
||||||
|
return $this->hasModule($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->hasModule($name)) {
|
||||||
|
return $this->modules[$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \Exception('Invalid property.');
|
||||||
|
}
|
||||||
|
|
||||||
protected function __construct($config = null)
|
protected function __construct($config = null)
|
||||||
{
|
{
|
||||||
ExceptionHandler::register();
|
ExceptionHandler::register();
|
||||||
|
|
||||||
$this->router = new RouteCollection;
|
$this->addModule('router', new RouteCollection);
|
||||||
$this->templating = new TemplateEngine;
|
$this->addModule('templating', new TemplateEngine);
|
||||||
$this->configuration = new ConfigManager($config);
|
$this->addModule('config', new ConfigManager($config));
|
||||||
|
|
||||||
echo 'hello!';
|
$this->templating->addFilter('json_decode');
|
||||||
|
$this->templating->addFilter('byte_symbol');
|
||||||
|
$this->templating->addFunction('byte_symbol');
|
||||||
|
$this->templating->addFunction('session_id');
|
||||||
|
$this->templating->addFunction('config', [$this->config, 'get']);
|
||||||
|
$this->templating->addFunction('route', [$this->router, 'url']);
|
||||||
|
|
||||||
|
echo sprintf(
|
||||||
|
'Running on commit <a href="https://github.com/flashwave/misuzu/commit/%s">%s</a> on branch <a href="https://github.com/flashwave/misuzu/tree/%3$s">%3$s</a>!',
|
||||||
|
self::gitCommitHash(true),
|
||||||
|
self::gitCommitHash(),
|
||||||
|
self::gitBranch()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
|
@ -51,50 +90,22 @@ class Application
|
||||||
{
|
{
|
||||||
ExceptionHandler::debug($mode);
|
ExceptionHandler::debug($mode);
|
||||||
|
|
||||||
if ($this->hasTemplating()) {
|
if ($this->hasTemplating) {
|
||||||
$this->getTemplating()->debug($mode);
|
$this->templating->debug($mode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasRouter(): bool
|
public function addModule(string $name, $module): void
|
||||||
{
|
{
|
||||||
return !is_null($this->router) && $this->router instanceof RouteCollection;
|
if ($this->hasModule($name)) {
|
||||||
}
|
throw new \Exception('This module has already been registered.');
|
||||||
|
|
||||||
public function getRouter(): RouteCollection
|
|
||||||
{
|
|
||||||
if (!$this->hasRouter()) {
|
|
||||||
throw new \Exception('No RouteCollection instance is available.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->router;
|
$this->modules[$name] = $module;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasTemplating(): bool
|
public function hasModule(string $name): bool
|
||||||
{
|
{
|
||||||
return !is_null($this->templating) && $this->templating instanceof TemplateEngine;
|
return array_key_exists($name, $this->modules) && !is_null($this->modules[$name]);
|
||||||
}
|
|
||||||
|
|
||||||
public function getTemplating(): TemplateEngine
|
|
||||||
{
|
|
||||||
if (!$this->hasTemplating()) {
|
|
||||||
throw new \Exception('No TemplateEngine instance is available.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->templating;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hasConfig(): bool
|
|
||||||
{
|
|
||||||
return !is_null($this->configuration) && $this->configuration instanceof ConfigManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getConfig(): ConfigManager
|
|
||||||
{
|
|
||||||
if (!$this->hasConfig()) {
|
|
||||||
throw new \Exception('No ConfigManager instance is available.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->configuration;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,23 +13,6 @@ use Twig_SimpleFunction;
|
||||||
*/
|
*/
|
||||||
class TemplateEngine
|
class TemplateEngine
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Utility |filters().
|
|
||||||
*/
|
|
||||||
private const UTILITY_FILTERS = [
|
|
||||||
'json_decode',
|
|
||||||
//'byte_symbol',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Utility functions().
|
|
||||||
*/
|
|
||||||
private const UTILITY_FUNCTIONS = [
|
|
||||||
//'route',
|
|
||||||
//'config',
|
|
||||||
//'session_id',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Template file extension.
|
* Template file extension.
|
||||||
*/
|
*/
|
||||||
|
@ -61,14 +44,6 @@ class TemplateEngine
|
||||||
'auto_reload' => false,
|
'auto_reload' => false,
|
||||||
'debug' => false,
|
'debug' => false,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
foreach (static::UTILITY_FILTERS as $filter) {
|
|
||||||
$this->addFilter($filter, $filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (static::UTILITY_FUNCTIONS as $function) {
|
|
||||||
$this->addFunction($function, $function);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -147,11 +122,11 @@ class TemplateEngine
|
||||||
private function fixPath(string $path): string
|
private function fixPath(string $path): string
|
||||||
{
|
{
|
||||||
// if the .twig extension if already present just assume that the path is already correct
|
// if the .twig extension if already present just assume that the path is already correct
|
||||||
if (substr($path, 0 - strlen(static::FILE_EXTENSION)) === static::FILE_EXTENSION) {
|
if (ends_width($path, self::FILE_EXTENSION)) {
|
||||||
return $path;
|
return $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
return str_replace('.', '/', $path) . static::FILE_EXTENSION;
|
return str_replace('.', '/', $path) . self::FILE_EXTENSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -162,7 +137,7 @@ class TemplateEngine
|
||||||
*/
|
*/
|
||||||
public function render(string $path, array $vars = null): string
|
public function render(string $path, array $vars = null): string
|
||||||
{
|
{
|
||||||
$path = static::fixPath($path);
|
$path = self::fixPath($path);
|
||||||
|
|
||||||
if ($vars !== null) {
|
if ($vars !== null) {
|
||||||
$this->vars($vars);
|
$this->vars($vars);
|
||||||
|
@ -223,6 +198,6 @@ class TemplateEngine
|
||||||
*/
|
*/
|
||||||
public function exists(string $path, string $namespace): bool
|
public function exists(string $path, string $namespace): bool
|
||||||
{
|
{
|
||||||
return $this->loader->exists('@' . $namespace . '/' . static::fixPath($path));
|
return $this->loader->exists('@' . $namespace . '/' . self::fixPath($path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
21
utility.php
21
utility.php
|
@ -35,6 +35,22 @@ function has_flag(int $flags, int $flag): bool
|
||||||
return ($flags & $flag) > 0;
|
return ($flags & $flag) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function byte_symbol($bytes, $decimal = false)
|
||||||
|
{
|
||||||
|
if ($bytes < 1) {
|
||||||
|
return "0 B";
|
||||||
|
}
|
||||||
|
|
||||||
|
$divider = $decimal ? 1000 : 1024;
|
||||||
|
$symbols = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
|
||||||
|
|
||||||
|
$exp = floor(log($bytes) / log($divider));
|
||||||
|
$bytes = $bytes / pow($divider, floor($exp));
|
||||||
|
$symbol = $symbols[$exp];
|
||||||
|
|
||||||
|
return sprintf("%.2f %s%sB", $bytes, $symbol, $symbol !== '' && !$decimal ? 'i' : '');
|
||||||
|
}
|
||||||
|
|
||||||
function is_int_ex($value, int $boundary_low, int $boundary_high): bool
|
function is_int_ex($value, int $boundary_low, int $boundary_high): bool
|
||||||
{
|
{
|
||||||
return is_int($value) && $value >= $boundary_low && $value <= $boundary_high;
|
return is_int($value) && $value >= $boundary_low && $value <= $boundary_high;
|
||||||
|
@ -74,8 +90,3 @@ function is_int64($value): bool
|
||||||
{
|
{
|
||||||
return is_int_ex($value, -0x8000000000000000, 0x7FFFFFFFFFFFFFFF);
|
return is_int_ex($value, -0x8000000000000000, 0x7FFFFFFFFFFFFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_uint64($value): bool
|
|
||||||
{
|
|
||||||
return is_int_ex($value, 0x0, 0xFFFFFFFFFFFFFFFF);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue