compileSource() public method

public compileSource ( $source, $name )
示例#1
0
 /**
  * @dataProvider getGenerationTests
  */
 public function testGenerate($inputFile, $outputFile)
 {
     $env = new \Twig_Environment();
     $env->addExtension(new \Twig_Extension_Core());
     $env->addExtension(new TwigJsExtension());
     $env->setLoader(new \Twig_Loader_Filesystem(__DIR__ . '/Fixture/templates'));
     $env->setCompiler(new JsCompiler($env));
     $source = file_get_contents($inputFile);
     $this->assertEquals(file_get_contents($outputFile), $env->compileSource($source, $inputFile));
 }
示例#2
0
 public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()
 {
     $options = array('cache' => sys_get_temp_dir() . '/twig', 'auto_reload' => false, 'debug' => false);
     // force compilation
     $twig = new Twig_Environment($loader = new Twig_Loader_Array(array('index' => '{{ foo }}')), $options);
     $cache = $twig->getCacheFilename('index');
     if (!is_dir(dirname($cache))) {
         mkdir(dirname($cache), 0777, true);
     }
     file_put_contents($cache, $twig->compileSource('{{ foo }}', 'index'));
     // check that extensions won't be initialized when rendering a template that is already in the cache
     $twig = $this->getMockBuilder('Twig_Environment')->setConstructorArgs(array($loader, $options))->setMethods(array('initExtensions'))->getMock();
     $twig->expects($this->never())->method('initExtensions');
     // render template
     $output = $twig->render('index', array('foo' => 'bar'));
     $this->assertEquals('bar', $output);
     unlink($cache);
 }
示例#3
0
require_once __DIR__ . '/../tests/bootstrap.php';
$_SERVER['TWIG_LIB'] = __DIR__ . '/../vendor/twig/twig/lib';
if (!isset($_SERVER['TWIG_LIB'])) {
    throw new \RuntimeException('$_SERVER["TWIG_LIB"] must be set.');
}
$env = new Twig_Environment();
$env->setLoader(new Twig_Loader_Filesystem(array(__DIR__ . '/../src-js/templates/twig')));
$env->addExtension(new Twig_Extension_Core());
$handler = new TwigJs\CompileRequestHandler($env, new TwigJs\JsCompiler($env));
foreach (new RecursiveDirectoryIterator(__DIR__ . '/../src-js/templates/twig', RecursiveDirectoryIterator::SKIP_DOTS) as $file) {
    if ('.twig' !== substr($file, -5)) {
        continue;
    }
    $request = new TwigJs\CompileRequest(basename($file), file_get_contents($file));
    file_put_contents(__DIR__ . '/../src-js/templates/js/' . basename($file, '.twig') . '.js', $handler->process($request));
    file_put_contents(__DIR__ . '/../src-js/templates/php/' . basename($file, '.twig') . '.php', $env->compileSource(file_get_contents($file), basename($file)));
}
// port over selected twig integration tests
$testsToPort = array('expressions/array');
$pathToFixtures = realpath($_SERVER['TWIG_LIB'] . '/../test/Twig/Tests/Fixtures');
$targetDir = realpath(__DIR__ . '/../src-js/templates/integration');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathToFixtures, RecursiveDirectoryIterator::SKIP_DOTS)) as $file) {
    if ('.test' !== substr($file, -5)) {
        continue;
    }
    $testName = str_replace(DIRECTORY_SEPARATOR, '/', substr($file, strlen($pathToFixtures) + 1, -5));
    if (!in_array($testName, $testsToPort, true)) {
        continue;
    }
    // parse test (copy&paste from twig's integrationTest.php)
    $test = file_get_contents($file->getRealpath());
 public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()
 {
     $cache = new Twig_Cache_Filesystem($dir = sys_get_temp_dir() . '/twig');
     $options = array('cache' => $cache, 'auto_reload' => false, 'debug' => false);
     // force compilation
     $twig = new Twig_Environment($loader = new Twig_Loader_Array(array('index' => '{{ foo }}')), $options);
     $key = $cache->generateKey('index', $twig->getTemplateClass('index'));
     $cache->write($key, $twig->compileSource(new Twig_Source('{{ foo }}', 'index')));
     // check that extensions won't be initialized when rendering a template that is already in the cache
     $twig = $this->getMockBuilder('Twig_Environment')->setConstructorArgs(array($loader, $options))->setMethods(array('initExtensions'))->getMock();
     $twig->expects($this->never())->method('initExtensions');
     // render template
     $output = $twig->render('index', array('foo' => 'bar'));
     $this->assertEquals('bar', $output);
     Twig_Tests_FilesystemHelper::removeDir($dir);
 }
 /**
  * Only for debugging, dump the compiled template file to console and die
  *
  * @param string $template the name of the template in __DIR__/templates/
  */
 private function dumpCompiledTemplate($template)
 {
     $source = file_get_contents(__DIR__ . '/templates/' . $template);
     var_dump($this->twig->compileSource($source, $template));
     die;
 }