85 lines
2.5 KiB
PHP
85 lines
2.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Расширения для PHPTAL для отображения времени и даты
|
|
*/
|
|
namespace ctiso;
|
|
use PHPTAL_Php_TalesInternal,
|
|
ctiso\Controller\SiteInterface,
|
|
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_Assets implements PHPTAL_Tales
|
|
{
|
|
static public function assets($expression, $nothrow = false)
|
|
{
|
|
$s = PHPTAL_Php_TalesInternal::string($expression);
|
|
return "ctiso\\Tales::phptal_asset(" . $s . ")";
|
|
}
|
|
}
|
|
|
|
class Tales {
|
|
/** @var SiteInterface */
|
|
static $site;
|
|
|
|
static function phptal_date ($e) {
|
|
return date("d.m.Y", $e);
|
|
}
|
|
|
|
static function phptal_time ($e) {
|
|
return date("H:i", $e);
|
|
}
|
|
|
|
static function phptal_asset($s) {
|
|
self::$site->addStyleSheet($s);
|
|
return "";
|
|
}
|
|
|
|
/**
|
|
* Функция подключения компонента
|
|
*/
|
|
static function phptal_component($expression) {
|
|
$begin = floatval(microtime(true));
|
|
/** @var Component */
|
|
$component = self::$site->loadComponent($expression);
|
|
$req = new HttpRequest();
|
|
$result = $component->execute($req);
|
|
|
|
echo "<!-- ", $expression, ", ", round(floatval(microtime(true)) - $begin, 4), "sec -->";
|
|
return $result;
|
|
}
|
|
|
|
|
|
static function register($site) {
|
|
self::$site = $site;
|
|
|
|
/* Регистрация нового префикса для подключения компонента */
|
|
$tales = PHPTAL_TalesRegistry::getInstance();
|
|
$tales->registerPrefix('component', ['ctiso\\Tales_Component', 'component']);
|
|
$tales->registerPrefix('date', ['ctiso\\Tales_DateTime', 'date']);
|
|
$tales->registerPrefix('time', ['ctiso\\Tales_DateTime', 'time']);
|
|
$tales->registerPrefix('assets', ['ctiso\\Tales_Assets', 'assets']);
|
|
}
|
|
}
|