|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2017 Michael Giesler |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Dembelo. |
|
5
|
|
|
* |
|
6
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
|
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU Affero General Public License 3 for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
|
17
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
namespace DembeloMain\Twig; |
|
20
|
|
|
|
|
21
|
|
|
use DembeloMain\Model\FeatureToggle; |
|
22
|
|
|
use Twig_Extension; |
|
23
|
|
|
use Twig_Function; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class VersionExtension |
|
27
|
|
|
* This twig extensions adds a function "hasFeature()" to check the feature toggle |
|
28
|
|
|
*/ |
|
29
|
|
|
class FeatureToggleExtension extends Twig_Extension |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var FeatureToggle |
|
33
|
|
|
*/ |
|
34
|
|
|
private $featureToggle; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param FeatureToggle $featureToggle |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(FeatureToggle $featureToggle) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->featureToggle = $featureToggle; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getFunctions(): array |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
new Twig_Function('has_feature', array($this, 'hasFeature')), |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* checks the featureToggle Service for feature existance |
|
56
|
|
|
* @param string $featureKey name of feature |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public function hasFeature($featureKey): bool |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->featureToggle->hasFeature($featureKey); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getName(): string |
|
69
|
|
|
{ |
|
70
|
|
|
return 'has_feature'; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|