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 Psr\Log\LoggerInterface; |
15
|
|
|
use Throwable; |
16
|
|
|
use Twig\Environment; |
17
|
|
|
use WBW\Bundle\CoreBundle\Twig\Environment\TwigEnvironmentTrait; |
18
|
|
|
use WBW\Library\Symfony\Manager\AbstractManager; |
19
|
|
|
use WBW\Library\Symfony\Manager\ManagerInterface; |
20
|
|
|
use WBW\Library\Symfony\Provider\ThemeProviderInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Abstract theme manager. |
24
|
|
|
* |
25
|
|
|
* @author webeweb <https://github.com/webeweb> |
26
|
|
|
* @package WBW\Bundle\CoreBundle\Manager |
27
|
|
|
* @abstract |
28
|
1 |
|
*/ |
29
|
|
|
abstract class AbstractThemeManager extends AbstractManager { |
30
|
|
|
|
31
|
|
|
use TwigEnvironmentTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Index. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $index; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor. |
42
|
|
|
* |
43
|
330 |
|
* @param LoggerInterface $logger The logger. |
44
|
330 |
|
* @param Environment $twigEnvironment The Twig environment. |
45
|
330 |
|
*/ |
46
|
330 |
|
public function __construct(LoggerInterface $logger, Environment $twigEnvironment) { |
47
|
330 |
|
parent::__construct($logger); |
48
|
|
|
$this->setIndex($this->initIndex()); |
49
|
|
|
$this->setTwigEnvironment($twigEnvironment); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Add the global. |
54
|
70 |
|
* |
55
|
|
|
* @return void |
56
|
70 |
|
*/ |
57
|
|
|
public function addGlobal(): void { |
58
|
70 |
|
|
59
|
50 |
|
foreach ($this->getIndex() as $k => $v) { |
60
|
|
|
|
61
|
|
|
if (null === $v) { |
62
|
50 |
|
continue; |
63
|
50 |
|
} |
64
|
|
|
|
65
|
70 |
|
$parts = explode("\\", $k); |
66
|
|
|
$this->getTwigEnvironment()->addGlobal(str_replace("Interface", "", end($parts)), $this->getProviders()[$v]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the index. |
72
|
230 |
|
* |
73
|
230 |
|
* @return array Returns the index. |
74
|
|
|
*/ |
75
|
|
|
protected function getIndex(): array { |
76
|
|
|
return $this->index; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get a provider. |
81
|
|
|
* |
82
|
140 |
|
* @param string $name The name. |
83
|
|
|
* @return ThemeProviderInterface|null Returns the theme provider in case of success, null otherwise. |
84
|
140 |
|
*/ |
85
|
140 |
|
protected function getProvider(string $name): ?ThemeProviderInterface { |
86
|
20 |
|
|
87
|
|
|
$v = $this->getIndex()[$name]; |
88
|
|
|
if (null === $v) { |
89
|
120 |
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->getProviders()[$v]; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Initialize the index. |
97
|
|
|
* |
98
|
|
|
* @return array Returns the initialized index. |
99
|
|
|
*/ |
100
|
|
|
abstract protected function initIndex(): array; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the index. |
104
|
|
|
* |
105
|
330 |
|
* @param array $index The index. |
106
|
330 |
|
* @return ManagerInterface Returns this manager. |
107
|
330 |
|
*/ |
108
|
|
|
protected function setIndex(array $index): ManagerInterface { |
109
|
|
|
$this->index = $index; |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set a provider. |
115
|
|
|
* |
116
|
|
|
* @param string $name The name. |
117
|
190 |
|
* @param ThemeProviderInterface $provider The provider. |
118
|
|
|
* @return ManagerInterface Returns this manager. |
119
|
190 |
|
* @throws Throwable Throws an exception if an error occurs. |
120
|
190 |
|
*/ |
121
|
20 |
|
protected function setProvider(string $name, ThemeProviderInterface $provider): ManagerInterface { |
122
|
20 |
|
|
123
|
|
|
$v = $this->getIndex()[$name]; |
124
|
|
|
if (null !== $v) { |
125
|
190 |
|
$this->getProviders()[$v] = $provider; |
126
|
|
|
return $this; |
127
|
190 |
|
} |
128
|
|
|
|
129
|
|
|
$this->index[$name] = count($this->getProviders()); |
130
|
|
|
|
131
|
|
|
return $this->addProvider($provider); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|