Completed
Push — master ( f5d602...24c80e )
by WEBEWEB
01:41
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\Bundle\CoreBundle\Service\TwigEnvironmentTrait;
19
use WBW\Library\Core\Argument\ObjectHelper;
20
21
/**
22
 * Abstract theme manager.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Bundle\CoreBundle\Manager
26
 * @abstract
27
 */
28
abstract class AbstractThemeManager extends AbstractManager {
29
30
    use TwigEnvironmentTrait;
31
32
    /**
33
     * Index.
34
     *
35
     * @var array
36
     */
37
    private $index;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param Twig_Environment $twigEnvironment The Twig environment.
43
     */
44
    public function __construct(Twig_Environment $twigEnvironment) {
45
        parent::__construct();
46
        $this->setIndex($this->initIndex());
47
        $this->setTwigEnvironment($twigEnvironment);
48
    }
49
50
    /**
51
     * Add the global.
52
     *
53
     * @return void
54
     */
55
    public function addGlobal() {
56
        foreach ($this->getIndex() as $k => $v) {
57
            if (null === $v) {
58
                continue;
59
            }
60
            $this->getTwigEnvironment()->addGlobal(str_replace("Interface", "", $k), $this->getProviders()[$v]);
61
        }
62
    }
63
64
    /**
65
     * Get the index.
66
     *
67
     * @return array Returns the index.
68
     */
69
    protected function getIndex() {
70
        return $this->index;
71
    }
72
73
    /**
74
     * Get a provider.
75
     *
76
     * @param string $name The name.
77
     * @return ThemeProviderInterface Returns the theme provider in case of success, null otherwise.
78
     */
79
    protected function getProvider($name) {
80
        $k = ObjectHelper::getShortName($name);
81
        $v = $this->getIndex()[$k];
82
        if (null === $v) {
83
            return null;
84
        }
85
        return $this->getProviders()[$v];
86
    }
87
88
    /**
89
     * Initialize the index.
90
     *
91
     * @return array Returns the initialized index.
92
     */
93
    abstract protected function initIndex();
94
95
    /**
96
     * Set the index.
97
     *
98
     * @param array $index The index.
99
     * @return ManagerInterface Returns this manager.
100
     */
101
    protected function setIndex(array $index) {
102
        $this->index = $index;
103
        return $this;
104
    }
105
106
    /**
107
     * Set a provider.
108
     *
109
     * @param mixed $name The name.
110
     * @param ThemeProviderInterface $provider The provider.
111
     * @return ManagerInterface Returns this manager.
112
     */
113
    protected function setProvider($name, ThemeProviderInterface $provider) {
114
        $k = ObjectHelper::getShortName($name);
115
        $v = $this->getIndex()[$k];
116
        if (null !== $v) {
117
            $this->getProviders()[$v] = $provider;
118
            return $this;
119
        }
120
        $this->index[$k] = count($this->getProviders());
121
        return $this->registerProvider($provider);
122
    }
123
124
}
125