Passed
Push — master ( 382e1a...3fd618 )
by Allan
03:29
created

TemplateOutputGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerChangelogs\Generators;
7
8
use Vaimo\ComposerChangelogs\Loaders\TemplateLoader;
9
10
class TemplateOutputGenerator implements \Vaimo\ComposerChangelogs\Interfaces\TemplateOutputGeneratorInterface
11
{
12
    public function generateOutput(array $data, array $templatePaths, array $escapedValues = array())
13
    {
14
        $options = array(
15
            'loader' => new \Mustache_Loader_FilesystemLoader(DIRECTORY_SEPARATOR),
16
            'partials_loader' => new TemplateLoader(
17
                function ($name) use ($templatePaths) {
18
                    return $templatePaths[$name];
19
                }
20
            ),
21
            'strict_callables' => true,
22
            'helpers' => array(
23
                'line' => function ($template, $renderer) {
24
                    $character = substr($template, -1);
25
                    
26
                    $content = $renderer(substr($template, 0, -1));
27
                    
28
                    return str_pad('', strlen($content), $character);
29
                },
30
                'title' => function ($text) {
31
                    return strtoupper($text);
32
                }
33
            ),
34
            'escape' => function ($value) use ($escapedValues) {
35
                foreach ($escapedValues as $pattern => $replacement) {
36
                    if ($pattern === '*') {
37
                        foreach ($replacement as $replacer) {
38
                            $value = call_user_func($replacer, $value);
39
                        }
40
                        
41
                        continue;
42
                    }
43
                    
44
                    $value = preg_replace(
45
                        sprintf('/%s/', $pattern),
46
                        $replacement,
47
                        $value
48
                    );
49
                }
50
                
51
                return $value;
52
            }
53
        );
54
55
        $mustacheEngine = new \Mustache_Engine($options);
56
        
57
        $templateInstance = $mustacheEngine->loadTemplate($templatePaths['root']);
58
        
59
        return rtrim(
60
            $templateInstance->render($data),
61
            PHP_EOL
62
        );
63
    }
64
}
65