Completed
Push — master ( a1590e...0af16c )
by Vladimir
02:39
created

TwigManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 4 1
B configureTwig() 0 53 6
1
<?php
2
3
namespace allejo\stakx\Manager;
4
5
use allejo\stakx\Engines\TwigMarkdownEngine;
6
use allejo\stakx\Object\Configuration;
7
use allejo\stakx\Twig\FilesystemExtension;
8
use allejo\stakx\Twig\TwigExtension;
9
use Aptoma\Twig\Extension\MarkdownExtension;
10
use Twig_Environment;
11
use Twig_Loader_Filesystem;
12
13
class TwigManager extends BaseManager
14
{
15
    /**
16
     * @var Twig_Environment
17
     */
18
    private static $twig;
19
20 10
    public static function &getInstance ()
21
    {
22 10
        return self::$twig;
23
    }
24
25
    /**
26
     * @param Configuration $configuration
27
     * @param mixed         $options
28
     */
29 3
    public function configureTwig ($configuration, $options = array())
30
    {
31 3
        $loader   = new Twig_Loader_Filesystem(array(
32 3
            getcwd()
33
        ));
34 3
        $theme    = $configuration->getTheme();
35 3
        $mdEngine = new TwigMarkdownEngine();
36
37
        // Only load a theme if one is specified and actually exists
38 3
        if (!is_null($theme))
39
        {
40
            try
41
            {
42
                $loader->addPath($this->fs->absolutePath('_themes', $configuration->getTheme()), 'theme');
43
            }
44
            catch (\Twig_Error_Loader $e)
45
            {
46
                $this->output->error("The following theme could not be loaded: {theme}", array(
47
                    "theme" => $theme
48
                ));
49
                $this->output->error($e->getMessage());
50
            }
51
        }
52
53 3
        self::$twig = new Twig_Environment($loader, array(
54 3
            'autoescape'  => $configuration->getTwigAutoescape(),
55
            'auto_reload' => true,
56 3
            'cache'       => $this->fs->absolutePath('.stakx-cache/twig')
57
        ));
58
59 3
        foreach ($options['globals'] as $global)
60
        {
61
            self::$twig->addGlobal($global['name'], $global['value']);
62
        }
63
64
        // We'll use this to access the current file in our Twig filters
65 3
        self::$twig->addGlobal('__currentTemplate', '');
66
67 3
        self::$twig->addExtension(new TwigExtension());
68 3
        self::$twig->addExtension(new \Twig_Extensions_Extension_Text());
69 3
        self::$twig->addExtension(new MarkdownExtension($mdEngine));
70
71 3
        if (!$options['safe'])
72
        {
73 3
            self::$twig->addExtension(new FilesystemExtension());
74
        }
75
76 3
        if ($configuration->isDebug())
77
        {
78 3
            self::$twig->addExtension(new \Twig_Extension_Debug());
79 3
            self::$twig->enableDebug();
80
        }
81
    }
82
}