Модули c namespace

This commit is contained in:
CORP\phedor 2018-03-27 17:31:49 +03:00
parent 0f4b2fb722
commit e9f7c23990
11 changed files with 92 additions and 80 deletions

72
src/Tales.php Normal file
View file

@ -0,0 +1,72 @@
<?php
/**
* Расширения для PHPTAL для отображения времени и даты
*/
class DateTime_Tales implements PHPTAL_Tales
{
static public function date($expression, $nothrow = false)
{
return "Tales::phptal_date(".PHPTAL_Php_TalesInternal::path ($expression).")";
}
static public function time($expression, $nothrow = false)
{
return "Tales::phptal_time(".PHPTAL_Php_TalesInternal::path ($expression).")";
}
}
/**
* TALES для подключения компонентов
* component:name?param1=value1&param2=value2
*/
class Component_Tales implements PHPTAL_Tales
{
static public function component($expression, $nothrow = false)
{
$s = PHPTAL_Php_TalesInternal::string($expression);
return "Tales::phptal_component(" . $s . ")";
}
}
class Tales {
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));
/*.Controller_Component.*/$component = null;
if (class_exists("Controller_Site")) { //Если мы в CMS2
$component = Controller_Site::loadComponent($expression);
} else {
global $db, $registry; // Иначе обращаемся к глобальным переменным
$component = Controller_Component::loadComponent($expression, $db, $registry);
}
$req = new HttpRequest();
$result = $component->execute($req);
echo "<!-- ", $expression, ", ", round(floatval(microtime(true)) - $begin, 4), "sec -->";
return $result;
}
static function register() {
/* Регистрация нового префикса для подключения компонента */
$tales = PHPTAL_TalesRegistry::getInstance();
$tales->registerPrefix('component', array('Component_Tales', 'component'));
$tales->registerPrefix('date', array('DateTime_Tales', 'date'));
$tales->registerPrefix('time', array('DateTime_Tales', 'time'));
}
}