1
|
|
|
<?php |
2
|
|
|
namespace Aoe\FeatureFlag\Service; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2021 AOE GmbH <[email protected]> |
8
|
|
|
* |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
12
|
|
|
* free software; you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* The GNU General Public License can be found at |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
19
|
|
|
* |
20
|
|
|
* This script is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
26
|
|
|
***************************************************************/ |
27
|
|
|
|
28
|
|
|
use Aoe\FeatureFlag\Domain\Model\FeatureFlag; |
29
|
|
|
use Aoe\FeatureFlag\Domain\Repository\FeatureFlagRepository; |
30
|
|
|
use Aoe\FeatureFlag\Service\Exception\FeatureNotFoundException; |
31
|
|
|
use Aoe\FeatureFlag\System\Typo3\Configuration; |
32
|
|
|
use RuntimeException; |
33
|
|
|
use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException; |
34
|
|
|
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface; |
35
|
|
|
|
36
|
|
|
class FeatureFlagService |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
const BEHAVIOR_HIDE = 0; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
const BEHAVIOR_SHOW = 1; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var FeatureFlagRepository |
50
|
|
|
*/ |
51
|
|
|
private $featureFlagRepository; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var PersistenceManagerInterface |
55
|
|
|
*/ |
56
|
|
|
private $persistenceManager; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Configuration |
60
|
|
|
*/ |
61
|
|
|
private $configuration; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
private $cachedFlags = []; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param FeatureFlagRepository $featureFlagRepository |
70
|
|
|
* @param PersistenceManagerInterface $persistenceManager |
71
|
|
|
* @param Configuration $configuration |
72
|
|
|
*/ |
73
|
|
|
public function __construct( |
74
|
|
|
FeatureFlagRepository $featureFlagRepository, |
75
|
|
|
PersistenceManagerInterface $persistenceManager, |
76
|
|
|
Configuration $configuration |
77
|
|
|
) { |
78
|
|
|
$this->featureFlagRepository = $featureFlagRepository; |
79
|
|
|
$this->persistenceManager = $persistenceManager; |
80
|
|
|
$this->configuration = $configuration; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $flag |
85
|
|
|
* @return FeatureFlag |
86
|
|
|
* @throws FeatureNotFoundException |
87
|
|
|
* @throws RuntimeException |
88
|
|
|
* @return boolean |
89
|
|
|
*/ |
90
|
|
|
protected function getFeatureFlag($flag) |
91
|
|
|
{ |
92
|
|
|
if (false === array_key_exists($flag, $this->cachedFlags)) { |
93
|
|
|
$flagModel = $this->featureFlagRepository->findByFlag($flag); |
94
|
|
|
if (false === $flagModel instanceof FeatureFlag) { |
95
|
|
|
throw new FeatureNotFoundException('Feature Flag not found: "' . $flag . '"', 1383842028); |
96
|
|
|
} |
97
|
|
|
$this->cachedFlags[$flag] = $flagModel; |
98
|
|
|
} |
99
|
|
|
return $this->cachedFlags[$flag]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $flag |
104
|
|
|
* @return bool |
105
|
|
|
* @throws FeatureNotFoundException |
106
|
|
|
*/ |
107
|
|
|
public function isFeatureEnabled($flag) |
108
|
|
|
{ |
109
|
|
|
return $this->getFeatureFlag($flag)->isEnabled(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $flag |
114
|
|
|
* @param $enabled |
115
|
|
|
* @throws FeatureNotFoundException |
116
|
|
|
* @throws IllegalObjectTypeException |
117
|
|
|
*/ |
118
|
|
|
public function updateFeatureFlag($flag, $enabled) |
119
|
|
|
{ |
120
|
|
|
$flagModel = $this->getFeatureFlag($flag); |
121
|
|
|
$flagModel->setEnabled($enabled); |
122
|
|
|
|
123
|
|
|
$this->featureFlagRepository->update($flagModel); |
124
|
|
|
$this->persistenceManager->persistAll(); |
125
|
|
|
|
126
|
|
|
$this->cachedFlags[$flag] = $flagModel; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Flags entries in database |
131
|
|
|
*/ |
132
|
|
|
public function flagEntries() |
133
|
|
|
{ |
134
|
|
|
foreach ($this->configuration->getTables() as $table) { |
135
|
|
|
$this->featureFlagRepository->updateFeatureFlagStatusForTable($table); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |