fenom/src/Fenom/Accessor.php

155 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of Fenom.
*
* (c) 2013 Ivan Shalganov
*
* For the full copyright and license information, please view the license.md
* file that was distributed with this source code.
*/
namespace Fenom;
use Fenom\Error\UnexpectedTokenException;
/**
* Class Accessor
* @package Fenom
*/
class Accessor {
public static $vars = array(
'get' => '$_GET',
'post' => '$_POST',
'session' => '$_SESSION',
'cookie' => '$_COOKIE',
'request' => '$_REQUEST',
'files' => '$_FILES',
'globals' => '$GLOBALS',
'server' => '$_SERVER',
'env' => '$_ENV'
);
2015-06-01 23:42:47 +03:00
public static function parserVar($var, Tokenizer $tokens, Template $tpl, &$is_var) {
$is_var = true;
return $tpl->parseVariable($tokens, $var);
}
2015-06-03 11:31:53 +03:00
public static function parserCall($call, Tokenizer $tokens, Template $tpl) {
return $call.$tpl->parseArgs($tokens);
2015-06-01 23:42:47 +03:00
}
/**
* Accessor for global variables
* @param Tokenizer $tokens
* @param Template $tpl
2015-06-01 23:42:47 +03:00
* @return string
*/
2014-10-15 01:01:55 +04:00
public static function getVar(Tokenizer $tokens, Template $tpl)
{
$name = $tokens->prev[Tokenizer::TEXT];
if(isset(self::$vars[$name])) {
$var = $tpl->parseVariable($tokens, self::$vars[$name]);
return "(isset($var) ? $var : null)";
} else {
throw new UnexpectedTokenException($tokens->back());
}
}
/**
* Accessor for template information
* @param Tokenizer $tokens
2015-06-01 23:42:47 +03:00
* @return string
*/
2014-10-15 01:01:55 +04:00
public static function tpl(Tokenizer $tokens)
{
$method = $tokens->skip('.')->need(T_STRING)->getAndNext();
if(method_exists('Fenom\Render', 'get'.$method)) {
return '$tpl->get'.ucfirst($method).'()';
} else {
throw new UnexpectedTokenException($tokens->back());
}
}
2014-10-15 01:01:55 +04:00
public static function version()
{
return 'Fenom::VERSION';
}
/**
* @param Tokenizer $tokens
* @return string
*/
2014-10-15 01:01:55 +04:00
public static function constant(Tokenizer $tokens)
{
2014-10-05 22:01:11 +04:00
$const = array($tokens->skip('.')->need(Tokenizer::MACRO_STRING)->getAndNext());
while($tokens->is('.')) {
$const[] = $tokens->next()->need(Tokenizer::MACRO_STRING)->getAndNext();
}
$const = implode('\\', $const);
if($tokens->is(T_DOUBLE_COLON)) {
$const .= '::'.$tokens->next()->need(Tokenizer::MACRO_STRING)->getAndNext();
}
return '@constant('.var_export($const, true).')';
}
/**
* @param Tokenizer $tokens
* @param Template $tpl
* @return string
*/
2014-10-15 01:01:55 +04:00
public static function php(Tokenizer $tokens, Template $tpl)
{
2014-10-05 22:01:11 +04:00
$callable = array($tokens->skip('.')->need(Tokenizer::MACRO_STRING)->getAndNext());
while($tokens->is('.')) {
$callable[] = $tokens->next()->need(Tokenizer::MACRO_STRING)->getAndNext();
}
$callable = implode('\\', $callable);
if($tokens->is(T_DOUBLE_COLON)) {
$callable .= '::'.$tokens->next()->need(Tokenizer::MACRO_STRING)->getAndNext();
}
2014-10-15 01:01:55 +04:00
$call_filter = $tpl->getStorage()->call_filters;
if($call_filter) {
foreach($call_filter as $filter) {
if(!fnmatch(addslashes($filter), $callable)) {
throw new \LogicException("Callback ".str_replace('\\', '.', $callable)." is not available by settings");
}
}
}
if(!is_callable($callable)) {
2014-10-15 01:01:55 +04:00
throw new \RuntimeException("PHP method ".str_replace('\\', '.', $callable).' does not exists.');
}
if($tokens->is('(')) {
$arguments = 'array'.$tpl->parseArgs($tokens).'';
} else {
$arguments = 'array()';
}
return 'call_user_func_array('.var_export($callable, true).', '.$arguments.')';
}
2014-10-15 01:01:55 +04:00
/**
* Accessor {$.fetch(...)}
* @param Tokenizer $tokens
* @param Template $tpl
* @return string
*/
public static function fetch(Tokenizer $tokens, Template $tpl)
{
$tokens->skip('(');
$name = $tpl->parsePlainArg($tokens, $static);
if($static) {
if(!$tpl->getStorage()->templateExists($static)) {
throw new \RuntimeException("Template $static not found");
}
}
if($tokens->is(',')) {
$tokens->skip()->need('[');
$vars = $tpl->parseArray($tokens) . ' + $var';
} else {
$vars = '$var';
}
2014-10-15 01:01:55 +04:00
$tokens->skip(')');
return '$tpl->getStorage()->fetch('.$name.', '.$vars.')';
}
}