Completed
Push — master ( 0ffa26...ca3451 )
by Vladimir
02:21
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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 70
ccs 22
cts 22
cp 1
rs 10
c 1
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 5
    public static function &getInstance ()
21
    {
22 5
        return self::$twig;
23
    }
24
25
    /**
26
     * @param Configuration $configuration
27
     * @param mixed         $options
28
     */
29 1
    public function configureTwig ($configuration, $options = array())
30
    {
31 1
        $loader   = new Twig_Loader_Filesystem(array(
32 1
            getcwd()
33
        ));
34 1
        $theme    = $configuration->getTheme();
35 1
        $mdEngine = new TwigMarkdownEngine();
36
37
        // Only load a theme if one is specified and actually exists
38 1
        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 1
        self::$twig = new Twig_Environment($loader, array(
54 1
            'autoescape'  => $configuration->getTwigAutoescape(),
55
            'auto_reload' => true,
56 1
            'cache'       => $this->fs->absolutePath('.stakx-cache/twig')
57
        ));
58
59 1
        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 1
        self::$twig->addGlobal('__currentTemplate', '');
66
67 1
        self::$twig->addExtension(new TwigExtension());
68 1
        self::$twig->addExtension(new \Twig_Extensions_Extension_Text());
69 1
        self::$twig->addExtension(new MarkdownExtension($mdEngine));
70
71 1
        if (!$options['safe'])
72
        {
73 1
            self::$twig->addExtension(new FilesystemExtension());
74
        }
75
76 1
        if ($configuration->isDebug())
77
        {
78 1
            self::$twig->addExtension(new \Twig_Extension_Debug());
79 1
            self::$twig->enableDebug();
80
        }
81
    }
82
}