|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Pheature\Community\Laravel; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Illuminate\Support\Facades\Route; |
|
9
|
|
|
use Illuminate\Support\ServiceProvider; |
|
10
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
|
11
|
|
|
use Pheature\Core\Toggle\Read\ChainToggleStrategyFactory; |
|
12
|
|
|
use Pheature\Core\Toggle\Read\FeatureFinder; |
|
13
|
|
|
use Pheature\Core\Toggle\Write\FeatureRepository; |
|
14
|
|
|
use Pheature\Crud\Psr11\Toggle\ChainToggleStrategyFactoryFactory; |
|
15
|
|
|
use Pheature\Crud\Psr11\Toggle\FeatureFinderFactory; |
|
16
|
|
|
use Pheature\Crud\Psr11\Toggle\FeatureRepositoryFactory; |
|
17
|
|
|
use Pheature\Crud\Psr11\Toggle\ToggleConfig; |
|
18
|
|
|
use Pheature\Crud\Psr7\Toggle\DeleteFeature; |
|
19
|
|
|
use Pheature\Crud\Psr7\Toggle\DeleteFeatureFactory; |
|
20
|
|
|
use Pheature\Crud\Psr7\Toggle\GetFeature; |
|
21
|
|
|
use Pheature\Crud\Psr7\Toggle\GetFeatureFactory; |
|
22
|
|
|
use Pheature\Crud\Psr7\Toggle\GetFeatures; |
|
23
|
|
|
use Pheature\Crud\Psr7\Toggle\GetFeaturesFactory; |
|
24
|
|
|
use Pheature\Crud\Psr7\Toggle\PatchFeature; |
|
25
|
|
|
use Pheature\Crud\Psr7\Toggle\PatchFeatureFactory; |
|
26
|
|
|
use Pheature\Crud\Psr7\Toggle\PostFeature; |
|
27
|
|
|
use Pheature\Crud\Psr7\Toggle\PostFeatureFactory; |
|
28
|
|
|
use Pheature\Dbal\Toggle\Cli\InitSchema; |
|
29
|
|
|
use Pheature\Model\Toggle\SegmentFactory; |
|
30
|
|
|
use Pheature\Model\Toggle\SegmentFactoryFactory; |
|
31
|
|
|
use Pheature\Model\Toggle\StrategyFactory; |
|
32
|
|
|
use Pheature\Model\Toggle\StrategyFactoryFactory; |
|
33
|
|
|
use Pheature\Sdk\CommandRunner; |
|
34
|
|
|
use Pheature\Sdk\CommandRunnerFactory; |
|
35
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
36
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
37
|
|
|
|
|
38
|
|
|
final class ToggleProvider extends ServiceProvider |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* Register services. |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
4 |
|
public function register(): void |
|
46
|
|
|
{ |
|
47
|
|
|
/** @var null|array<string, mixed> $configItem */ |
|
48
|
4 |
|
$configItem = config('pheature_flags'); |
|
49
|
4 |
|
if (null === $configItem) { |
|
50
|
1 |
|
return; |
|
51
|
|
|
} |
|
52
|
3 |
|
$toggleConfig = new ToggleConfig($configItem); |
|
53
|
3 |
|
$this->app->bind(ToggleConfig::class, fn() => $toggleConfig); |
|
54
|
3 |
|
$this->app->bind(StrategyFactory::class, Closure::fromCallable(new StrategyFactoryFactory())); |
|
55
|
3 |
|
$this->app->bind(SegmentFactory::class, Closure::fromCallable(new SegmentFactoryFactory())); |
|
56
|
3 |
|
foreach ($toggleConfig->strategyTypes() as $strategyType) { |
|
57
|
3 |
|
$this->app->bind($strategyType['type'], $strategyType['factory_id']); |
|
58
|
|
|
} |
|
59
|
3 |
|
foreach ($toggleConfig->segmentTypes() as $segmentType) { |
|
60
|
3 |
|
$this->app->bind($segmentType['type'], $segmentType['factory_id']); |
|
61
|
|
|
} |
|
62
|
3 |
|
$this->app->bind( |
|
63
|
3 |
|
ChainToggleStrategyFactory::class, |
|
64
|
3 |
|
Closure::fromCallable(new ChainToggleStrategyFactoryFactory()) |
|
65
|
|
|
); |
|
66
|
3 |
|
$this->app->bind(ResponseFactoryInterface::class, static fn() => new Psr17Factory()); |
|
67
|
3 |
|
$this->app->bind(FeatureRepository::class, Closure::fromCallable(new FeatureRepositoryFactory())); |
|
68
|
3 |
|
$this->app->bind(FeatureFinder::class, Closure::fromCallable(new FeatureFinderFactory())); |
|
69
|
3 |
|
$this->app->extend( |
|
70
|
3 |
|
ServerRequestInterface::class, |
|
71
|
3 |
|
Closure::fromCallable(new RouteParameterAsPsr7RequestAttribute($this->app)) |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
3 |
|
$this->enableSDK(); |
|
75
|
3 |
|
$this->enableAPI($toggleConfig); |
|
76
|
3 |
|
$this->enableDBAL(); |
|
77
|
3 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Bootstrap services. |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function boot(): void |
|
85
|
|
|
{ |
|
86
|
1 |
|
$this->publishes( |
|
87
|
|
|
[ |
|
88
|
1 |
|
__DIR__ . '/../config/pheature_flags.php' => config_path('pheature_flags.php'), |
|
89
|
|
|
], |
|
90
|
1 |
|
'config' |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
1 |
|
Route::group( |
|
94
|
1 |
|
$this->routeConfiguration(), |
|
95
|
1 |
|
function () { |
|
96
|
1 |
|
$this->loadRoutesFrom(__DIR__ . '/../routes/pheature_flags.php'); |
|
97
|
1 |
|
} |
|
98
|
|
|
); |
|
99
|
1 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return array<string, mixed> |
|
103
|
|
|
*/ |
|
104
|
1 |
|
private function routeConfiguration(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return [ |
|
107
|
1 |
|
'prefix' => config('pheature_flags.api_prefix') ?? '', |
|
108
|
1 |
|
'middleware' => config('pheature_flags.middleware') ?? ['api'], |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
3 |
|
private function enableAPI(ToggleConfig $toggleConfig): void |
|
113
|
|
|
{ |
|
114
|
3 |
|
if ($toggleConfig->apiEnabled()) { |
|
115
|
1 |
|
$this->app->bind(GetFeature::class, Closure::fromCallable(new GetFeatureFactory())); |
|
116
|
1 |
|
$this->app->bind(GetFeatures::class, Closure::fromCallable(new GetFeaturesFactory())); |
|
117
|
1 |
|
$this->app->bind(PostFeature::class, Closure::fromCallable(new PostFeatureFactory())); |
|
118
|
1 |
|
$this->app->bind(PatchFeature::class, Closure::fromCallable(new PatchFeatureFactory())); |
|
119
|
1 |
|
$this->app->bind(DeleteFeature::class, Closure::fromCallable(new DeleteFeatureFactory())); |
|
120
|
|
|
} |
|
121
|
3 |
|
} |
|
122
|
|
|
|
|
123
|
3 |
|
private function enableSDK(): void |
|
124
|
|
|
{ |
|
125
|
3 |
|
if (class_exists(CommandRunner::class)) { |
|
126
|
3 |
|
$this->app->bind(CommandRunner::class, Closure::fromCallable(new CommandRunnerFactory())); |
|
127
|
|
|
} |
|
128
|
3 |
|
} |
|
129
|
|
|
|
|
130
|
3 |
|
private function enableDBAL(): void |
|
131
|
|
|
{ |
|
132
|
3 |
|
if ('dbal' === config('pheature_flags.driver')) { |
|
133
|
2 |
|
$this->commands([InitSchema::class]); |
|
134
|
|
|
} |
|
135
|
3 |
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|