Passed
Pull Request — master (#190)
by Arman
04:05
created

TwigAdapter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLoader() 0 14 3
A __construct() 0 5 1
A addFunctionsToTwig() 0 8 3
A render() 0 9 1
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Renderer\Adapters;
16
17
use Quantum\Libraries\Storage\Factories\FileSystemFactory;
18
use Quantum\Renderer\Contracts\TemplateRendererInterface;
19
use Quantum\Renderer\Exceptions\RendererException;
20
use Quantum\Libraries\Storage\FileSystem;
21
use Quantum\Exceptions\BaseException;
22
use Twig\Loader\FilesystemLoader;
23
use Twig\Error\RuntimeError;
24
use Twig\Error\LoaderError;
25
use Twig\Error\SyntaxError;
26
use Twig\TwigFunction;
27
use Twig\Environment;
28
29
/**
30
 * Class TwigAdapter
31
 * @package Quantum\Renderer
32
 */
33
class TwigAdapter implements TemplateRendererInterface
34
{
35
36
    /**
37
     * @var FileSystem
38
     */
39
    protected $fs;
40
41
    /**
42
     * @var array|null
43
     */
44
    protected $configs;
45
46
    /**
47
     * @param array|null $configs
48
     * @throws BaseException
49
     */
50
    public function __construct(?array $configs = [])
51
    {
52
        $this->configs = $configs;
53
54
        $this->fs = FileSystemFactory::get();
55
    }
56
57
    /**
58
     * Renders the view
59
     * @param string $view
60
     * @param array $params
61
     * @return string
62
     * @throws BaseException
63
     * @throws LoaderError
64
     * @throws RuntimeError
65
     * @throws SyntaxError
66
     */
67
    public function render(string $view, array $params = []): string
68
    {
69
        $loader = $this->getLoader($view);
70
71
        $twig = new Environment($loader, $this->configs);
72
73
        $this->addFunctionsToTwig($twig);
74
75
        return $twig->render($view . '.php', $params);
76
    }
77
78
    /**
79
     * @param string $view
80
     * @return FilesystemLoader
81
     * @throws BaseException
82
     */
83
    private function getLoader(string $view): FilesystemLoader
84
    {
85
        $moduleViewPath = modules_dir() . DS . current_module() . DS . 'Views' . DS . $view . '.php';
86
        $sharedViewPath = base_dir() . DS . 'shared' . DS . 'views' . DS . $view . '.php';
87
88
        if ($this->fs->exists($moduleViewPath)) {
89
            return new FilesystemLoader(dirname($moduleViewPath));
90
        }
91
92
        if ($this->fs->exists($sharedViewPath)) {
93
            return new FilesystemLoader(dirname($sharedViewPath));
94
        }
95
96
        throw RendererException::fileNotFound($view);
97
    }
98
99
    /**
100
     * @param Environment $twig
101
     */
102
    private function addFunctionsToTwig(Environment $twig)
103
    {
104
        $definedFunctions = get_defined_functions();
105
        $allDefinedFunctions = array_merge($definedFunctions['internal'], $definedFunctions['user']);
106
107
        foreach ($allDefinedFunctions as $function) {
108
            if (function_exists($function)) {
109
                $twig->addFunction(new TwigFunction($function, $function));
110
            }
111
        }
112
    }
113
}