phplibrary/src/Tales.php
2018-04-13 17:59:57 +03:00

76 lines
2.3 KiB
PHP

<?php
/**
* Расширения для PHPTAL для отображения времени и даты
*/
namespace ctiso;
use PHPTAL_Php_TalesInternal,
App\Controller\Site,
ctiso\Controller\Component,
ctiso\HttpRequest,
PHPTAL_Tales,
PHPTAL_TalesRegistry;
class Tales_DateTime implements PHPTAL_Tales
{
static public function date($expression, $nothrow = false) {
return "ctiso\\Tales::phptal_date(".PHPTAL_Php_TalesInternal::path ($expression).")";
}
static public function time($expression, $nothrow = false) {
return "ctiso\\Tales::phptal_time(".PHPTAL_Php_TalesInternal::path ($expression).")";
}
}
class Tales_Component implements PHPTAL_Tales
{
static public function component($expression, $nothrow = false)
{
$s = PHPTAL_Php_TalesInternal::string($expression);
return "ctiso\\Tales::phptal_component(" . $s . ")";
}
}
class Tales {
static $db;
static $registry;
static function phptal_date ($e) {
return date("d.m.Y", $e);
}
static function phptal_time ($e) {
return date("H:i", $e);
}
/**
* Функция подключения компонента
*/
static function phptal_component ($expression) {
$begin = floatval(microtime(true));
/*.Component.*/$component = null;
if (class_exists("ctiso\\Controller\\Site")) { //Если мы в CMS2
$component = Site::loadComponent($expression);
} else {
$component = Component::loadComponent($expression, self::$db, self::$registry);
}
$req = new HttpRequest();
$result = $component->execute($req);
echo "<!-- ", $expression, ", ", round(floatval(microtime(true)) - $begin, 4), "sec -->";
return $result;
}
static function register($db, $registry) {
self::$db = $db;
self::$registry = $registry;
/* Регистрация нового префикса для подключения компонента */
$tales = PHPTAL_TalesRegistry::getInstance();
$tales->registerPrefix('component', array('ctiso\\Tales_Component', 'component'));
$tales->registerPrefix('date', array('ctiso\\Tales_DateTime', 'date'));
$tales->registerPrefix('time', array('ctiso\\Tales_DateTime', 'time'));
}
}