Пример #1
0
 public function load(IfwPsn_Vendor_Twig_Environment $env)
 {
     require_once dirname(__FILE__) . '/Global/Wp.php';
     require_once dirname(__FILE__) . '/Global/Db.php';
     $env->addGlobal('wp', new IfwPsn_Wp_WunderScript_Extension_Global_Wp());
     $env->addGlobal('db', new IfwPsn_Wp_WunderScript_Extension_Global_Db());
 }
Пример #2
0
 protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
 {
     if ($condition) {
         eval('$ret = ' . $condition . ';');
         if (!$ret) {
             $this->markTestSkipped($condition);
         }
     }
     $loader = new IfwPsn_Vendor_Twig_Loader_Array($templates);
     foreach ($outputs as $match) {
         $config = array_merge(array('cache' => false, 'strict_variables' => true), $match[2] ? eval($match[2] . ';') : array());
         $twig = new IfwPsn_Vendor_Twig_Environment($loader, $config);
         $twig->addGlobal('global', 'global');
         foreach ($this->getExtensions() as $extension) {
             $twig->addExtension($extension);
         }
         try {
             $template = $twig->loadTemplate('index.twig');
         } catch (Exception $e) {
             if (false !== $exception) {
                 $this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
                 return;
             }
             if ($e instanceof IfwPsn_Vendor_Twig_Error_Syntax) {
                 $e->setTemplateFile($file);
                 throw $e;
             }
             throw new IfwPsn_Vendor_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->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
                 return;
             }
             if ($e instanceof IfwPsn_Vendor_Twig_Error_Syntax) {
                 $e->setTemplateFile($file);
             } else {
                 $e = new IfwPsn_Vendor_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) {
             echo 'Compiled template that failed:';
             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 . ')');
     }
 }
Пример #3
0
 public function leaveNode(IfwPsn_Vendor_Twig_NodeInterface $node, IfwPsn_Vendor_Twig_Environment $env)
 {
     if ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Constant) {
         // constants are marked safe for all
         $this->setSafe($node, array('all'));
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_BlockReference) {
         // blocks are safe by definition
         $this->setSafe($node, array('all'));
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Parent) {
         // parent block is safe by definition
         $this->setSafe($node, array('all'));
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Conditional) {
         // intersect safeness of both operands
         $safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
         $this->setSafe($node, $safe);
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Filter) {
         // filter expression is safe when the filter is safe
         $name = $node->getNode('filter')->getAttribute('value');
         $args = $node->getNode('arguments');
         if (false !== ($filter = $env->getFilter($name))) {
             $safe = $filter->getSafe($args);
             if (null === $safe) {
                 $safe = $this->intersectSafe($this->getSafe($node->getNode('node')), $filter->getPreservesSafety());
             }
             $this->setSafe($node, $safe);
         } else {
             $this->setSafe($node, array());
         }
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Function) {
         // function expression is safe when the function is safe
         $name = $node->getAttribute('name');
         $args = $node->getNode('arguments');
         $function = $env->getFunction($name);
         if (false !== $function) {
             $this->setSafe($node, $function->getSafe($args));
         } else {
             $this->setSafe($node, array());
         }
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_MethodCall) {
         if ($node->getAttribute('safe')) {
             $this->setSafe($node, array('all'));
         } else {
             $this->setSafe($node, array());
         }
     } elseif ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof IfwPsn_Vendor_Twig_Node_Expression_Name) {
         $name = $node->getNode('node')->getAttribute('name');
         // attributes on template instances are safe
         if ('_self' == $name || in_array($name, $this->safeVars)) {
             $this->setSafe($node, array('all'));
         } else {
             $this->setSafe($node, array());
         }
     } else {
         $this->setSafe($node, array());
     }
     return $node;
 }
Пример #4
0
 /**
  *
  * @param array $options
  * @throws IfwPsn_Wp_Exception
  * @return IfwPsn_Vendor_Twig_Environment
  */
 public static function factory($options = array())
 {
     if (!isset($options['twig_loader']) || empty($options['twig_loader'])) {
         $options['twig_loader'] = 'Filesystem';
     }
     $twigOptions = array();
     if (isset($options['twig_options']) && is_array($options['twig_options'])) {
         $twigOptions = $options['twig_options'];
     }
     $twigOptions['debug'] = true;
     switch ($options['twig_loader']) {
         case 'String':
             require_once dirname(__FILE__) . '/../Vendor/Twig/Loader/String.php';
             require_once dirname(__FILE__) . '/../Vendor/Twig/Extension/StringLoader.php';
             $tpl = new IfwPsn_Vendor_Twig_Environment(new IfwPsn_Vendor_Twig_Loader_String(), $twigOptions);
             $tpl->addExtension(new IfwPsn_Vendor_Twig_Extension_StringLoader());
             break;
         case 'Filesystem':
         default:
             if (!isset($options['plugin_manager']) || !$options['plugin_manager'] instanceof IfwPsn_Wp_Plugin_Manager) {
                 throw new IfwPsn_Wp_Exception('Filesystem loader requires instance of IfwPsn_Wp_Plugin_Manager');
             }
             $pm = $options['plugin_manager'];
             require_once dirname(__FILE__) . '/../Vendor/Twig/LoaderInterface.php';
             require_once dirname(__FILE__) . '/../Vendor/Twig/ExistsLoaderInterface.php';
             require_once dirname(__FILE__) . '/../Vendor/Twig/Loader/Filesystem.php';
             require_once dirname(__FILE__) . '/../Vendor/Twig/ExtensionInterface.php';
             require_once dirname(__FILE__) . '/../Vendor/Twig/Extension.php';
             require_once dirname(__FILE__) . '/../Vendor/Twig/Extension/Core.php';
             require_once dirname(__FILE__) . '/../Vendor/Twig/Extension/Escaper.php';
             require_once dirname(__FILE__) . '/../Vendor/Twig/Extension/Optimizer.php';
             require_once dirname(__FILE__) . '/../Vendor/Twig/Extension/Staging.php';
             require_once dirname(__FILE__) . '/../Vendor/Twig/Environment.php';
             $loader = new IfwPsn_Vendor_Twig_Loader_Filesystem($pm->getPathinfo()->getRootTpl());
             $loader->addPath(dirname(__FILE__) . '/Tpl/built-in');
             $tpl = new IfwPsn_Vendor_Twig_Environment($loader, $twigOptions);
     }
     // load extensions
     require_once dirname(__FILE__) . '/Tpl/Text.php';
     require_once dirname(__FILE__) . '/Tpl/Extension/DateLocale.php';
     require_once dirname(__FILE__) . '/Tpl/Extension/Text.php';
     require_once dirname(__FILE__) . '/../Vendor/Twig/Extension/Debug.php';
     $tpl->addGlobal('text', new IfwPsn_Wp_Tpl_Text());
     $tpl->addExtension(new IfwPsn_Wp_Tpl_Extension_DateLocale());
     $tpl->addExtension(new IfwPsn_Wp_Tpl_Extension_Text());
     $tpl->addExtension(new IfwPsn_Wp_Tpl_Extension_PrintR());
     $tpl->addExtension(new IfwPsn_Vendor_Twig_Extension_Debug());
     return $tpl;
 }
Пример #5
0
 function ifwpsn_twig_wordwrap_filter(IfwPsn_Vendor_Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
 {
     $sentences = array();
     $previous = mb_regex_encoding();
     mb_regex_encoding($env->getCharset());
     $pieces = mb_split($separator, $value);
     mb_regex_encoding($previous);
     foreach ($pieces as $piece) {
         while (!$preserve && mb_strlen($piece, $env->getCharset()) > $length) {
             $sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
             $piece = mb_substr($piece, $length, 2048, $env->getCharset());
         }
         $sentences[] = $piece;
     }
     return implode($separator, $sentences);
 }
Пример #6
0
function ifwpsn_twig_var_dump(IfwPsn_Vendor_Twig_Environment $env, $context)
{
    if (!$env->isDebug()) {
        return;
    }
    ob_start();
    $count = func_num_args();
    if (2 === $count) {
        $vars = array();
        foreach ($context as $key => $value) {
            if (!$value instanceof IfwPsn_Vendor_Twig_Template) {
                $vars[$key] = $value;
            }
        }
        var_dump($vars);
    } else {
        for ($i = 2; $i < $count; $i++) {
            var_dump(func_get_arg($i));
        }
    }
    return ob_get_clean();
}
Пример #7
0
/**
 * Loads a template from a string.
 *
 * <pre>
 * {{ include(template_from_string("Hello {{ name }}")) }}
 * </pre>
 *
 * @param IfwPsn_Vendor_Twig_Environment $env      A IfwPsn_Vendor_Twig_Environment instance
 * @param string           $template A template as a string
 *
 * @return IfwPsn_Vendor_Twig_Template A IfwPsn_Vendor_Twig_Template instance
 */
function ifwpsn_twig_template_from_string(IfwPsn_Vendor_Twig_Environment $env, $template)
{
    $name = sprintf('__string_template__%s', hash('sha256', uniqid(mt_rand(), true), false));
    $loader = new IfwPsn_Vendor_Twig_Loader_Chain(array(new IfwPsn_Vendor_Twig_Loader_Array(array($name => $template)), $current = $env->getLoader()));
    $env->setLoader($loader);
    try {
        $template = $env->loadTemplate($name);
    } catch (Exception $e) {
        $env->setLoader($current);
        throw $e;
    }
    $env->setLoader($current);
    return $template;
}
Пример #8
0
 /**
  * @param $string
  * @param array $context
  * @param null|callable $exceptionHandler
  * @return string
  */
 public function parse($string, $context = array(), $exceptionHandler = null)
 {
     if (!empty($string)) {
         try {
             if ($this->_env->getLoader() instanceof IfwPsn_Vendor_Twig_Loader_String) {
                 $string = $this->_sanitzeString($string);
             }
             $string = $this->_env->render($string, $context);
         } catch (Exception $e) {
             // invalid filter handling
             if (is_callable($exceptionHandler)) {
                 call_user_func_array($exceptionHandler, array($e));
             } else {
                 if ($this->_logger instanceof IfwPsn_Wp_Plugin_Logger) {
                     $this->_logger->err($e->getMessage());
                 }
             }
         }
     }
     return $string;
 }
Пример #9
0
 protected function preEscapeFilterNode(IfwPsn_Vendor_Twig_Node_Expression_Filter $filter, IfwPsn_Vendor_Twig_Environment $env)
 {
     $name = $filter->getNode('filter')->getAttribute('value');
     $type = $env->getFilter($name)->getPreEscape();
     if (null === $type) {
         return $filter;
     }
     $node = $filter->getNode('node');
     if ($this->isSafeFor($type, $node, $env)) {
         return $filter;
     }
     $filter->setNode('node', $this->getEscaperFilter($type, $node));
     return $filter;
 }
Пример #10
0
/**
 * Returns a template content without rendering it.
 *
 * @param string $name The template name
 *
 * @return string The template source
 */
function ifwpsn_twig_source(IfwPsn_Vendor_Twig_Environment $env, $name)
{
    return $env->getLoader()->getSource($name);
}
Пример #11
0
 public function load(IfwPsn_Vendor_Twig_Environment $env)
 {
     require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Twig/SimpleFunction.php';
     $env->addFunction(new IfwPsn_Vendor_Twig_SimpleFunction('get_post', array($this, 'getPost')));
 }
Пример #12
0
 public function load(IfwPsn_Vendor_Twig_Environment $env)
 {
     require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Twig/SimpleFilter.php';
     $env->addFilter(new IfwPsn_Vendor_Twig_SimpleFilter('unserialize', array($this, 'filterUnserialize')));
 }
Пример #13
0
 public function load(IfwPsn_Vendor_Twig_Environment $env)
 {
     require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Twig/SimpleTest.php';
     $env->addTest(new IfwPsn_Vendor_Twig_SimpleTest('serialized', 'is_serialized'));
 }
Пример #14
0
 public function load(IfwPsn_Vendor_Twig_Environment $env)
 {
     require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Twig/SimpleFilter.php';
     $env->addFilter(new IfwPsn_Vendor_Twig_SimpleFilter('get_key', array($this, 'getKey')));
     $env->addFilter(new IfwPsn_Vendor_Twig_SimpleFilter('divide', array($this, 'divide')));
 }