Completed
Push — master ( fd13ab...fb3368 )
by WEBEWEB
07:01 queued 05:35
created

AbstractThemeManager::setTwig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Manager;
13
14
use Twig_Environment;
15
use WBW\Bundle\CoreBundle\Manager\AbstractManager;
16
use WBW\Bundle\CoreBundle\Manager\ManagerInterface;
17
use WBW\Bundle\CoreBundle\Provider\ThemeProviderInterface;
18
use WBW\Library\Core\Argument\ObjectHelper;
19
20
/**
21
 * Abstract theme manager.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\CoreBundle\Manager
25
 * @abstract
26
 */
27
abstract class AbstractThemeManager extends AbstractManager {
28
29
    /**
30
     * Index.
31
     *
32
     * @var array
33
     */
34
    private $index;
35
36
    /**
37
     * Twig service.
38
     *
39
     * @var Twig_Environment
40
     */
41
    private $twig;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param Twig_Environment $twig The Twig service.
47
     */
48
    public function __construct(Twig_Environment $twig) {
49
        parent::__construct();
50
        $this->setIndex($this->initIndex());
51
        $this->setTwig($twig);
52
    }
53
54
    /**
55
     * Add the global.
56
     *
57
     * @return void
58
     */
59
    public function addGlobal() {
60
        foreach ($this->getIndex() as $k => $v) {
61
            if (null === $v) {
62
                continue;
63
            }
64
            $this->getTwig()->addGlobal(str_replace("Interface", "", $k), $this->getProviders()[$v]);
65
        }
66
    }
67
68
    /**
69
     * Get the index.
70
     *
71
     * @return array Returns the index.
72
     */
73
    protected function getIndex() {
74
        return $this->index;
75
    }
76
77
    /**
78
     * Get a provider.
79
     *
80
     * @param string $name The name.
81
     * @return ThemeProviderInterface Returns the theme provider in case of success, null otherwise.
82
     */
83
    protected function getProvider($name) {
84
        $k = ObjectHelper::getShortName($name);
85
        $v = $this->getIndex()[$k];
86
        if (null === $v) {
87
            return null;
88
        }
89
        return $this->getProviders()[$v];
90
    }
91
92
    /**
93
     * Get the Twig.
94
     *
95
     * @return Twig_Environment Returns the Twig.
96
     */
97
    public function getTwig() {
98
        return $this->twig;
99
    }
100
101
    /**
102
     * Initialize the index.
103
     *
104
     * @return array Returns the initialized index.
105
     */
106
    abstract protected function initIndex();
107
108
    /**
109
     * Set the index.
110
     *
111
     * @param array $index The index.
112
     * @return ManagerInterface Returns this manager.
113
     */
114
    protected function setIndex(array $index) {
115
        $this->index = $index;
116
        return $this;
117
    }
118
119
    /**
120
     * Set a provider.
121
     *
122
     * @param mixed $name The name.
123
     * @param ThemeProviderInterface $provider The provider.
124
     * @return ManagerInterface Returns this manager.
125
     */
126
    protected function setProvider($name, ThemeProviderInterface $provider) {
127
        $k = ObjectHelper::getShortName($name);
128
        $v = $this->getIndex()[$k];
129
        if (null !== $v) {
130
            $this->getProviders()[$v] = $provider;
131
            return;
132
        }
133
        $this->index[$k] = count($this->getProviders());
134
        return $this->registerProvider($provider);
135
    }
136
137
    /**
138
     * Set the Twig.
139
     *
140
     * @param Twig_Environment $twig The Twig.
141
     * @return ManagerInterface Returns this manager.
142
     */
143
    protected function setTwig(Twig_Environment $twig) {
144
        $this->twig = $twig;
145
        return $this;
146
    }
147
148
}
149