Completed
Pull Request — master (#61)
by Vladimir
09:45
created

Node::compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
1
<?php
2
3
namespace allejo\stakx\Templating\Twig\MarkupBlock;
4
5
/**
6
 * @author Gunnar Lium <[email protected]>
7
 * @author Joris Berthelot <[email protected]>
8
 *
9
 * @link https://github.com/aptoma/twig-markdown/blob/master/src/Aptoma/Twig/Node/MarkdownNode.php
10
 */
11
class Node extends \Twig_Node
12
{
13
    public function __construct(\Twig_Node $body, $lineno, $tag)
14
    {
15
        parent::__construct(['body' => $body], [], $lineno, $tag);
16
    }
17
18
    /**
19
     * Compiles the node to PHP.
20
     *
21
     * @param \Twig_Compiler A Twig_Compiler instance
22
     */
23
    public function compile(\Twig_Compiler $compiler)
24
    {
25
        $compiler
26
            ->addDebugInfo($this)
27
            ->write('ob_start();' . PHP_EOL)
28
            ->subcompile($this->getNode('body'))
29
            ->write('$content = ob_get_clean();' . PHP_EOL)
30
            ->write('preg_match("/^\s*/", $content, $matches);' . PHP_EOL)
31
            ->write('$lines = explode("\n", $content);' . PHP_EOL)
32
            ->write('$content = preg_replace(\'/^\' . $matches[0]. \'/\', "", $lines);' . PHP_EOL)
33
            ->write('$content = join("\n", $content);' . PHP_EOL)
34
            ->write('echo $this->env->getExtension(\'allejo\stakx\Templating\Twig\TwigExtension\')->parseMarkup($content, "' . $this->tag . '");' . PHP_EOL)
35
        ;
36
    }
37
}
38