Passed
Push — master ( 86c3d2...1ebd3c )
by Gabor
07:26
created

TwigExtension::getConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Adapter\Renderer\Twig;
15
16
use Twig_Extension;
17
use Twig_SimpleFilter;
18
use Twig_SimpleFunction;
19
use WebHemi\Adapter\DependencyInjection\DependencyInjectionAdapterInterface;
20
use WebHemi\Adapter\Renderer\RendererFilterInterface;
21
use WebHemi\Adapter\Renderer\RendererHelperInterface;
22
use WebHemi\Application\EnvironmentManager;
23
use WebHemi\Config\ConfigInterface;
24
25
/**
26
 * Class TwigExtension
27
 *
28
 * @codeCoverageIgnore - Test helpers and filters individually. It's only the shipped solution
29
 *                       to add them to the renderer.
30
 */
31
class TwigExtension extends Twig_Extension
32
{
33
    /** @var DependencyInjectionAdapterInterface */
34
    private $dependencyInjectionAdapter;
35
    /** @var ConfigInterface */
36
    private $configuration;
37
    /** @var EnvironmentManager */
38
    private $environmentManager;
39
40
    /**
41
     * WebHemiTwigExtension constructor.
42
     */
43
    public function __construct()
44
    {
45
        // Oh, this is a disgusting ugly hack...
46
        global $diAdapter;
1 ignored issue
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
47
        $this->dependencyInjectionAdapter = $diAdapter;
48
        $this->configuration = $diAdapter->get(ConfigInterface::class);
49
        $this->environmentManager = $diAdapter->get(EnvironmentManager::class);
50
    }
51
52
    /**
53
     * Returns extension filters.
54
     *
55
     * @return array<Twig_SimpleFilter>
56
     */
57
    public function getFilters()
58
    {
59
        $filters = [];
60
        $filterConfig = $this->getConfig('filter');
61
62
        foreach ($filterConfig as $className) {
63
            /** @var RendererFilterInterface $callable */
64
            $callable = $this->dependencyInjectionAdapter->get($className);
65
            $filters[] = new Twig_SimpleFilter($callable::getName(), $callable);
66
        }
67
68
        return $filters;
69
    }
70
71
    /**
72
     * Returns extension functions.
73
     *
74
     * @return array<Twig_SimpleFunction>
75
     */
76
    public function getFunctions() : array
77
    {
78
        $functions = [];
79
        $helperConfig = $this->getConfig('helper');
80
81
        foreach ($helperConfig as $className) {
82
            /** @var RendererHelperInterface $callable */
83
            $callable = $this->dependencyInjectionAdapter->get($className);
84
            $functions[] = new Twig_SimpleFunction($callable::getName(), $callable);
85
        }
86
87
        return $functions;
88
    }
89
90
    /**
91
     * Returns the renderer config by type.
92
     *
93
     * @param string $type
94
     * @return array
95
     */
96
    private function getConfig(string $type) : array
97
    {
98
        $module = $this->environmentManager->getSelectedModule();
99
        $config = [];
100
101
        if ($this->configuration->has('renderer/Global/'.$type)) {
102
            $config = $this->configuration->getData('renderer/Global/' . $type);
103
        }
104
105
        if ($this->configuration->has('renderer/'.$module.'/'.$type)) {
106
            $config = merge_array_overwrite(
107
                $config,
108
                $this->configuration->getData('renderer/'.$module.'/'.$type)
109
            );
110
        }
111
112
        return $config;
113
    }
114
}
115