1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jlis\Judge; |
4
|
|
|
|
5
|
|
|
use Jlis\Judge\Judges\ValueJudge; |
6
|
|
|
use Jlis\Judge\Judges\FeatureJudge; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Julius Ehrlich <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class JudgeServiceProvider extends ServiceProvider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Indicates if loading of the provider is deferred. |
16
|
|
|
* |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
protected $defer = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Register the service provider. |
23
|
|
|
*/ |
24
|
2 |
|
public function register() |
25
|
|
|
{ |
26
|
2 |
|
$app = $this->app; |
27
|
2 |
|
$this->loadConfigs(); |
28
|
2 |
|
$config = $app->make('config')->get('judge'); |
29
|
2 |
|
$adapter = $this->app->make($this->getAdapterClass($config)); |
30
|
2 |
|
$voters = isset($config['voters']) ? $config['voters'] : []; |
31
|
|
|
|
32
|
2 |
|
$app->bind( |
33
|
2 |
|
'feature', |
34
|
|
|
function () use ($adapter, $voters) { |
35
|
2 |
|
return new FeatureJudge($adapter, $voters); |
36
|
|
|
} |
37
|
2 |
|
); |
38
|
|
|
|
39
|
2 |
|
$app->bind( |
40
|
2 |
|
'value', |
41
|
2 |
|
function () use ($adapter, $voters) { |
42
|
2 |
|
return new ValueJudge($adapter, $voters); |
43
|
|
|
} |
44
|
2 |
|
); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Bootstrap the application events. |
49
|
|
|
*/ |
50
|
3 |
|
public function boot() |
51
|
|
|
{ |
52
|
1 |
|
$publishes = []; |
53
|
1 |
|
$app = $this->app; |
54
|
1 |
|
foreach ($this->getConfigs() as $key => $path) { |
55
|
|
|
/* @var $app array */ |
56
|
1 |
|
$publishes[$path] = $app['path.config'].DIRECTORY_SEPARATOR.$key.'.php'; |
57
|
3 |
|
} |
58
|
1 |
|
$this->publishes($publishes, 'config'); |
59
|
3 |
|
} |
60
|
|
|
|
61
|
2 |
|
private function loadConfigs() |
62
|
|
|
{ |
63
|
2 |
|
foreach ($this->getConfigs() as $key => $path) { |
64
|
2 |
|
$this->mergeConfigFrom($path, $key); |
65
|
2 |
|
} |
66
|
2 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
3 |
|
private function getConfigs() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
3 |
|
'features' => __DIR__.'/../config/features.php', |
75
|
3 |
|
'values' => __DIR__.'/../config/values.php', |
76
|
3 |
|
'judge' => __DIR__.'/../config/judge.php', |
77
|
3 |
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $config |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
2 |
|
private function getAdapterClass(array $config) |
86
|
|
|
{ |
87
|
|
|
$map = [ |
88
|
2 |
|
'config' => Adapters\ConfigAdapter::class, |
89
|
2 |
|
'redis' => Adapters\RedisAdapter::class, |
90
|
2 |
|
'cache' => Adapters\CacheAdapter::class, |
91
|
2 |
|
]; |
92
|
|
|
|
93
|
2 |
|
$className = isset($config['adapter']) ? $config['adapter'] : 'config'; |
94
|
2 |
|
if (isset($map[$className])) { |
95
|
1 |
|
return $map[$className]; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return $className; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|