1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFeature\Provider; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Blade; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
use LaravelFeature\Domain\Repository\FeatureRepositoryInterface; |
8
|
|
|
use LaravelFeature\Console\Command\ScanViewsForFeaturesCommand; |
9
|
|
|
|
10
|
|
|
class FeatureServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Bootstrap any application services. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
public function boot() |
18
|
|
|
{ |
19
|
|
|
$this->loadMigrationsFrom(__DIR__.'/../Migration'); |
20
|
|
|
|
21
|
|
|
$this->publishes([ |
22
|
|
|
__DIR__.'/../Config/features.php' => config_path('features.php'), |
23
|
|
|
]); |
24
|
|
|
|
25
|
|
|
$this->registerBladeDirectives(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Register any application services. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public function register() |
34
|
|
|
{ |
35
|
|
|
$this->mergeConfigFrom(__DIR__.'/../Config/features.php', 'features'); |
36
|
|
|
|
37
|
|
|
$config = $this->app->make('config'); |
38
|
|
|
|
39
|
|
|
$this->app->bind(FeatureRepositoryInterface::class, function () use ($config) { |
40
|
|
|
return app()->make($config->get('features.repository')); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
$this->registerConsoleCommand(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function registerBladeDirectives() |
47
|
|
|
{ |
48
|
|
|
$this->registerBladeFeatureDirective(); |
49
|
|
|
$this->registerBladeFeatureForDirective(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function registerBladeFeatureDirective() |
53
|
|
|
{ |
54
|
|
|
Blade::directive('feature', function ($featureName) { |
55
|
|
|
return "<?php if (app(\\LaravelFeature\\Domain\\FeatureManager::class)->isEnabled($featureName)): ?>"; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
Blade::directive('endfeature', function () { |
59
|
|
|
return '<?php endif; ?>'; |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function registerBladeFeatureForDirective() |
64
|
|
|
{ |
65
|
|
|
Blade::directive('featurefor', function ($args) { |
66
|
|
|
return "<?php if (app(\\LaravelFeature\\Domain\\FeatureManager::class)->isEnabledFor($args)): ?>"; |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
Blade::directive('endfeaturefor', function () { |
70
|
|
|
return '<?php endif; ?>'; |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function registerConsoleCommand() |
75
|
|
|
{ |
76
|
|
|
if ($this->app->runningInConsole()) { |
77
|
|
|
$this->commands([ |
78
|
|
|
ScanViewsForFeaturesCommand::class |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|