addTest() public method

public addTest ( $name, Twig_TestInterface $test )
$test Twig_TestInterface
コード例 #1
0
 protected function getEnvironment()
 {
     $env = new Twig_Environment(new Twig_Loader_Array(array()));
     $env->addTest(new Twig_Test('anonymous', function () {
     }));
     return $env;
 }
コード例 #2
0
ファイル: TestTest.php プロジェクト: d-f-d/100gramm
 public function getTests()
 {
     $environment = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
     $environment->addTest(new Twig_SimpleTest('barbar', 'twig_tests_test_barbar', array('is_variadic' => true, 'need_context' => true)));
     $tests = array();
     $expr = new Twig_Node_Expression_Constant('foo', 1);
     $node = new Twig_Node_Expression_Test_Null($expr, 'null', new Twig_Node(array()), 1);
     $tests[] = array($node, '(null === "foo")');
     // test as an anonymous function
     if (PHP_VERSION_ID >= 50300) {
         $node = $this->createTest(new Twig_Node_Expression_Constant('foo', 1), 'anonymous', array(new Twig_Node_Expression_Constant('foo', 1)));
         $tests[] = array($node, 'call_user_func_array($this->env->getTest(\'anonymous\')->getCallable(), array("foo", "foo"))');
     }
     // arbitrary named arguments
     $string = new Twig_Node_Expression_Constant('abc', 1);
     $node = $this->createTest($string, 'barbar');
     $tests[] = array($node, 'twig_tests_test_barbar("abc")', $environment);
     $node = $this->createTest($string, 'barbar', array('foo' => new Twig_Node_Expression_Constant('bar', 1)));
     $tests[] = array($node, 'twig_tests_test_barbar("abc", null, null, array("foo" => "bar"))', $environment);
     $node = $this->createTest($string, 'barbar', array('arg2' => new Twig_Node_Expression_Constant('bar', 1)));
     $tests[] = array($node, 'twig_tests_test_barbar("abc", null, "bar")', $environment);
     $node = $this->createTest($string, 'barbar', array(new Twig_Node_Expression_Constant('1', 1), new Twig_Node_Expression_Constant('2', 1), new Twig_Node_Expression_Constant('3', 1), 'foo' => new Twig_Node_Expression_Constant('bar', 1)));
     $tests[] = array($node, 'twig_tests_test_barbar("abc", "1", "2", array(0 => "3", "foo" => "bar"))', $environment);
     return $tests;
 }
コード例 #3
0
ファイル: Twig.php プロジェクト: tommcdo/kohana-twig
 /**
  * Create a new Twig environment
  *
  * @return  Twig_Environment  Twig environment
  */
 protected static function env()
 {
     $config = Kohana::$config->load('twig');
     $loader = new Twig_Loader_CFS($config->get('loader'));
     $env = new Twig_Environment($loader, $config->get('environment'));
     foreach ($config->get('functions') as $key => $value) {
         $function = new Twig_SimpleFunction($key, $value);
         $env->addFunction($function);
     }
     foreach ($config->get('filters') as $key => $value) {
         $filter = new Twig_SimpleFilter($key, $value);
         $env->addFilter($filter);
     }
     foreach ($config->get('tests') as $key => $value) {
         $test = new Twig_SimpleTest($key, $value);
         $env->addTest($test);
     }
     return $env;
 }
コード例 #4
0
ファイル: Twig.php プロジェクト: fdcore/yacms
 public function render($template, $data = null, $return = false)
 {
     $app = \Slim\Slim::getInstance();
     if (defined('IS_ADMIN')) {
         $loader = new Twig_Loader_Filesystem(BASE_PATH . '/_app/templates/admin');
     } else {
         $loader = new Twig_Loader_Filesystem(BASE_PATH . '/_app/templates');
     }
     $twig = new Twig_Environment($loader);
     $bundles = directory_map(BASE_PATH . '/_app/bundles');
     foreach ($bundles as $f) {
         $pi = mb_pathinfo($f);
         $type_name = explode('.', $pi['filename']);
         include_once BASE_PATH . '/_app/bundles/' . $f;
         if ($type_name[0] == 'filter') {
             $twig->addFilter(new Twig_SimpleFilter($type_name[1], $type_name[1]));
         }
         if ($type_name[0] == 'func') {
             $twig->addFunction(new Twig_SimpleFunction($type_name[1], $type_name[1]));
         }
         if ($type_name[0] == 'test') {
             $twig->addTest(new Twig_SimpleTest($type_name[1], $type_name[1]));
         }
     }
     $req = $app->request;
     $data['request'] = $req;
     if (isset($_SESSION['phone'])) {
         $data['phone'] = $app->model->get_user($_SESSION['phone']);
     }
     $data['config'] = $app->config;
     $data['user'] = $app->model->get_user();
     if (isset($_SESSION['slim.flash'])) {
         $data['flash'] = $_SESSION['slim.flash'];
     }
     $data = array_merge($this->data->all(), (array) $data);
     if ($return) {
         return $twig->render($template, $data);
     } else {
         echo $twig->render($template, $data);
     }
 }
コード例 #5
0
ファイル: Twig.php プロジェクト: getherbie/herbie
 /**
  * @return void
  */
 public function addTwigPlugins()
 {
     if ($this->config->isEmpty('twig.extend')) {
         return;
     }
     // Functions
     $dir = $this->config->get('twig.extend.functions');
     foreach ($this->readPhpFiles($dir) as $file) {
         $included = $this->includePhpFile($file);
         $this->environment->addFunction($included);
     }
     // Filters
     $dir = $this->config->get('twig.extend.filters');
     foreach ($this->readPhpFiles($dir) as $file) {
         $included = $this->includePhpFile($file);
         $this->environment->addFilter($included);
     }
     // Tests
     $dir = $this->config->get('twig.extend.tests');
     foreach ($this->readPhpFiles($dir) as $file) {
         $included = $this->includePhpFile($file);
         $this->environment->addTest($included);
     }
 }
コード例 #6
0
 protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
 {
     if ($condition) {
         eval('$ret = ' . $condition . ';');
         if (!$ret) {
             $this->markTestSkipped($condition);
         }
     }
     $loader = new Twig_Loader_Array($templates);
     foreach ($outputs as $i => $match) {
         $config = array_merge(array('cache' => false, 'strict_variables' => true), $match[2] ? eval($match[2] . ';') : array());
         $twig = new Twig_Environment($loader, $config);
         $twig->addGlobal('global', 'global');
         foreach ($this->getExtensions() as $extension) {
             $twig->addExtension($extension);
         }
         foreach ($this->getTwigFilters() as $filter) {
             $twig->addFilter($filter);
         }
         foreach ($this->getTwigTests() as $test) {
             $twig->addTest($test);
         }
         foreach ($this->getTwigFunctions() as $function) {
             $twig->addFunction($function);
         }
         // avoid using the same PHP class name for different cases
         // only for PHP 5.2+
         if (PHP_VERSION_ID >= 50300) {
             $p = new ReflectionProperty($twig, 'templateClassPrefix');
             $p->setAccessible(true);
             $p->setValue($twig, '__TwigTemplate_' . hash('sha256', uniqid(mt_rand(), true), false) . '_');
         }
         try {
             $template = $twig->loadTemplate('index.twig');
         } catch (Exception $e) {
             if (false !== $exception) {
                 $message = $e->getMessage();
                 $this->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $message)));
                 $this->assertSame('.', substr($message, strlen($message) - 1), $message, 'Exception message must end with a dot.');
                 return;
             }
             if ($e instanceof Twig_Error_Syntax) {
                 $e->setTemplateFile($file);
                 throw $e;
             }
             throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
         }
         try {
             $output = trim($template->render(eval($match[1] . ';')), "\n ");
         } catch (Exception $e) {
             if (false !== $exception) {
                 $this->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
                 return;
             }
             if ($e instanceof Twig_Error_Syntax) {
                 $e->setTemplateFile($file);
             } else {
                 $e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
             }
             $output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
         }
         if (false !== $exception) {
             list($class) = explode(':', $exception);
             $this->assertThat(null, new PHPUnit_Framework_Constraint_Exception($class));
         }
         $expected = trim($match[3], "\n ");
         if ($expected !== $output) {
             printf("Compiled templates that failed on case %d:\n", $i + 1);
             foreach (array_keys($templates) as $name) {
                 echo "Template: {$name}\n";
                 $source = $loader->getSource($name);
                 echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
             }
         }
         $this->assertEquals($expected, $output, $message . ' (in ' . $file . ')');
     }
 }
コード例 #7
0
ファイル: TestInclude.php プロジェクト: Rayac/search
<?php

$env = new Twig_Environment(new Twig_Loader_Array(array()));
$env->addTest(new Twig_SimpleTest('anonymous', function () {
}));
return $env;
コード例 #8
0
ファイル: index.php プロジェクト: Willemdumee/Sketch
 * @version 0.2
 */
require_once dirname(__FILE__) . '/config.php';
require_once BASE_PATH . '/vendor/autoload.php';
/* TODO add to autoload */
require_once BASE_PATH . '/includes/DrifterTwigExtension.php';
/* TODO create loader for stylesheets */
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array('debug' => true));
$active_test = new Twig_SimpleTest('active', function ($value) {
    if (isset($value) && $value == $_GET['template']) {
        return true;
    }
    return false;
});
$twig->addTest($active_test);
$twig->addExtension(new Twig_Extension_Debug());
$twig->addExtension(new DrifterTwigExtension());
$template = $twig->loadTemplate('layouts/custom.rain');
$templateData = '';
$protocol = 'http://';
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
    $protocol = 'https://';
}
$url = '';
if (isset($_GET['path'])) {
    $uri = $_GET['path'];
}
$templateData = file_get_contents(INDEX_PAGE . $uri . '?format=json');
if (false === $templateData) {
    // redirect to home page
コード例 #9
0
ファイル: Twig.php プロジェクト: JNeutron/kohana-twig
 /**
  * Create a new Twig environment
  *
  * @return Twig_Environment
  */
 protected static function _init_env()
 {
     // Instance of Environment
     $env = new Twig_Environment(new Twig_Loader_CFS(Twig::$config['loader']), Twig::$config['environment']);
     // Functions
     if (!empty(Twig::$config['functions'])) {
         foreach (Twig::$config['functions'] as $params) {
             list($name, $callable, $options) = array_pad($params, 3, array());
             $function = new Twig_SimpleFunction($name, $callable, $options);
             $env->addFunction($function);
         }
     }
     // Filters
     if (!empty(Twig::$config['filters'])) {
         foreach (Twig::$config['filters'] as $params) {
             list($name, $callable, $options) = array_pad($params, 3, array());
             $filter = new Twig_SimpleFilter($name, $callable, $options);
             $env->addFilter($filter);
         }
     }
     // Tests
     if (!empty(Twig::$config['tests'])) {
         foreach (Twig::$config['tests'] as $params) {
             list($name, $callable, $options) = array_pad($params, 3, array());
             $test = new Twig_SimpleTest($name, $callable, $options);
             $env->addTest($test);
         }
     }
     // Debug
     if (Twig::$config['environment']['debug'] === true) {
         $env->addExtension(new Twig_Extension_Debug());
     }
     return $env;
 }