Compiler example


    public function getCompilerPassConfig(): PassConfig
    {
        return $this->getCompiler()->getPassConfig();
    }

    /** * Returns the compiler. */
    public function getCompiler(): Compiler
    {
        return $this->compiler ??= new Compiler();
    }

    /** * Sets a service. * * @return void * * @throws BadMethodCallException When this ContainerBuilder is compiled */
    public function set(string $id, ?object $service)
    {
        
public function setCompiler(Compiler $compiler)
    {
        $this->compiler = $compiler;
    }

    /** * Compiles a node and returns the PHP code. */
    public function compile(Node $node): string
    {
        if (null === $this->compiler) {
            $this->compiler = new Compiler($this);
        }

        return $this->compiler->compile($node)->getSource();
    }

    /** * Compiles a template source code. * * @throws SyntaxError When there was an error during tokenizing, parsing or compiling */
    public function compileSource(Source $source): string
    {

    public function getCompilerPassConfig(): PassConfig
    {
        return $this->getCompiler()->getPassConfig();
    }

    /** * Returns the compiler. */
    public function getCompiler(): Compiler
    {
        return $this->compiler ??= new Compiler();
    }

    /** * Sets a service. * * @return void * * @throws BadMethodCallException When this ContainerBuilder is compiled */
    public function set(string $id, ?object $service)
    {
        

        $this->assertSame($expected$node->evaluate($functions$variables));
    }

    abstract public static function getEvaluateData();

    /** * @dataProvider getCompileData */
    public function testCompile($expected$node$functions = [])
    {
        $compiler = new Compiler($functions);
        $node->compile($compiler);
        $this->assertSame($expected$compiler->getSource());
    }

    abstract public static function getCompileData();

    /** * @dataProvider getDumpData */
    public function testDump($expected$node)
    {
        
$this->expectException(SyntaxError::class);
        $this->expectExceptionMessage('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric');
        $node->evaluate([]['regexp' => 'this is not a regexp']);
    }

    public function testCompileMatchesWithInvalidRegexp()
    {
        $node = new BinaryNode('matches', new ConstantNode('abc')new ConstantNode('this is not a regexp'));

        $this->expectException(SyntaxError::class);
        $this->expectExceptionMessage('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric');
        $compiler = new Compiler([]);
        $node->compile($compiler);
    }

    public function testCompileMatchesWithInvalidRegexpAsExpression()
    {
        $node = new BinaryNode('matches', new ConstantNode('abc')new NameNode('regexp'));

        $this->expectException(SyntaxError::class);
        $this->expectExceptionMessage('Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric');
        $compiler = new Compiler([]);
        $node->compile($compiler);
        
class SearchAndRenderBlockNodeTest extends TestCase
{
    public function testCompileWidget()
    {
        $arguments = new Node([
            new NameExpression('form', 0),
        ]);

        $node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);

        $compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));

        $this->assertEquals(
            sprintf(
                '$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'widget\')',
                $this->getVariableGetter('form')
            ),
            trim($compiler->compile($node)->getSource())
        );
    }

    public function testCompileWidgetWithVariables()
    {
$compiler->compile($node);

        if ($isPattern) {
            $this->assertStringMatchesFormat($sourcetrim($compiler->getSource()));
        } else {
            $this->assertEquals($sourcetrim($compiler->getSource()));
        }
    }

    protected function getCompiler(Environment $environment = null)
    {
        return new Compiler(null === $environment ? $this->getEnvironment() : $environment);
    }

    protected function getEnvironment()
    {
        return new Environment(new ArrayLoader([]));
    }

    protected function getVariableGetter($name$line = false)
    {
        $line = $line > 0 ? "// line $line\n" : '';

        

        return $this->lexer ??= new Lexer();
    }

    private function getParser(): Parser
    {
        return $this->parser ??= new Parser($this->functions);
    }

    private function getCompiler(): Compiler
    {
        $this->compiler ??= new Compiler($this->functions);

        return $this->compiler->reset();
    }
}
$container->register('a')->addArgument(new Reference('b'));
        $container->register('b')->addArgument(new IteratorArgument([new Reference('a')]));

        $this->process($container);

        // just make sure that an IteratorArgument does not trigger a CircularReferenceException         $this->addToAssertionCount(1);
    }

    protected function process(ContainerBuilder $container)
    {
        $compiler = new Compiler();
        $passConfig = $compiler->getPassConfig();
        $passConfig->setOptimizationPasses([
            new AnalyzeServiceReferencesPass(true),
            new CheckCircularReferencesPass(),
        ]);
        $passConfig->setRemovingPasses([]);
        $passConfig->setAfterRemovingPasses([]);

        $compiler->compile($container);
    }
}

class TransNodeTest extends TestCase
{
    public function testCompileStrict()
    {
        $body = new TextNode('trans %var%', 0);
        $vars = new NameExpression('foo', 0);
        $node = new TransNode($body, null, null, $vars);

        $env = new Environment($this->createMock(LoaderInterface::class)['strict_variables' => true]);
        $compiler = new Compiler($env);

        $this->assertEquals(
            sprintf(
                'echo $this->env->getExtension(\'Symfony\Bridge\Twig\Extension\TranslationExtension\')->trans("trans %%var%%", array_merge(["%%var%%" => %s], %s), "messages");',
                $this->getVariableGetterWithoutStrictCheck('var'),
                $this->getVariableGetterWithStrictCheck('foo')
            ),
            trim($compiler->compile($node)->getSource())
        );
    }

    
new ConstantExpression(1, 0),
            new ConstantExpression('tpl1', 0),
            new ConstantExpression(0, 0),
            new ConstantExpression('tpl2', 0),
        ], 0);

        $node = new FormThemeNode($form$resources, 0);

        $environment = new Environment($this->createMock(LoaderInterface::class));
        $formRenderer = new FormRenderer($this->createMock(FormRendererEngineInterface::class));
        $this->registerTwigRuntimeLoader($environment$formRenderer);
        $compiler = new Compiler($environment);

        $this->assertEquals(
            sprintf(
                '$this->env->getRuntime("Symfony\\\\Component\\\\Form\\\\FormRenderer")->setTheme(%s, [1 => "tpl1", 0 => "tpl2"], true);',
                $this->getVariableGetter('form')
            ),
            trim($compiler->compile($node)->getSource())
        );

        $node = new FormThemeNode($form$resources, 0, null, true);

        
use Twig\Loader\LoaderInterface;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;

class DumpNodeTest extends TestCase
{
    public function testNoVar()
    {
        $node = new DumpNode('bar', null, 7);

        $env = new Environment($this->createMock(LoaderInterface::class));
        $compiler = new Compiler($env);

        $expected = <<<'EOTXT' if ($this->env->isDebug()) { $barvars = []; foreach ($context as $barkey => $barval) { if (!$barval instanceof \Twig\Template) { $barvars[$barkey] = $barval; } } // line 7 \Symfony\Component\VarDumper\VarDumper::dump($barvars); }
Home | Imprint | This part of the site doesn't use cookies.