Exemplo n.º 1
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;
 }
Exemplo n.º 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 . ')');
     }
 }