1
|
|
|
<?php namespace Neomerx\CorsIlluminate\Providers; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use \Closure; |
20
|
|
|
use \Neomerx\Cors\Analyzer; |
21
|
|
|
use \Psr\Log\LoggerInterface; |
22
|
|
|
use \Illuminate\Support\ServiceProvider; |
23
|
|
|
use \Illuminate\Contracts\Config\Repository; |
24
|
|
|
use \Neomerx\Cors\Contracts\AnalyzerInterface; |
25
|
|
|
use \Neomerx\CorsIlluminate\Settings\Settings; |
26
|
|
|
use \Neomerx\Cors\Contracts\AnalysisStrategyInterface; |
27
|
|
|
use \Illuminate\Contracts\Foundation\Application as ApplicationInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @package Neomerx\CorsIlluminate |
31
|
|
|
*/ |
32
|
|
|
class LaravelServiceProvider extends ServiceProvider |
33
|
|
|
{ |
34
|
|
|
/** Config file name without extension */ |
35
|
|
|
const CONFIG_FILE_NAME_WO_EXT = 'cors-illuminate'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
protected $defer = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool|null|LoggerInterface |
44
|
|
|
*/ |
45
|
|
|
private $logger = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool|array |
49
|
|
|
*/ |
50
|
|
|
private $settings = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
1 |
|
public function register() |
56
|
|
|
{ |
57
|
1 |
|
$this->mergeConfigs(); |
58
|
1 |
|
$this->configureCorsAnalyzer(); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Perform post-registration booting of services. |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
1 |
|
public function boot() |
67
|
|
|
{ |
68
|
1 |
|
$this->registerPublishConfig(); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Merge default config and config from application `config` folder. |
73
|
|
|
*/ |
74
|
1 |
|
protected function mergeConfigs() |
75
|
|
|
{ |
76
|
1 |
|
$repo = $this->getConfigRepository(); |
77
|
1 |
|
$config = $repo->get(static::CONFIG_FILE_NAME_WO_EXT, []); |
78
|
1 |
|
$base = $this->getBaseConfig(); |
79
|
1 |
|
$result = $config + $base; |
80
|
1 |
|
$repo->set(static::CONFIG_FILE_NAME_WO_EXT, $result); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
1 |
|
protected function registerPublishConfig() |
87
|
|
|
{ |
88
|
1 |
|
$publishPath = $this->app['path.config'] . DIRECTORY_SEPARATOR . static::CONFIG_FILE_NAME_WO_EXT . '.php'; |
89
|
1 |
|
$this->publishes([ |
90
|
1 |
|
$this->getConfigPath() => $publishPath, |
91
|
1 |
|
]); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
2 |
|
protected function configureCorsAnalyzer() |
98
|
|
|
{ |
99
|
2 |
|
$this->app->bind(AnalysisStrategyInterface::class, $this->getCreateAnalysisStrategyClosure()); |
100
|
2 |
|
$this->app->bind(AnalyzerInterface::class, $this->getCreateAnalyzerClosure()); |
101
|
2 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Closure |
105
|
|
|
*/ |
106
|
3 |
|
protected function getCreateAnalysisStrategyClosure() |
107
|
|
|
{ |
108
|
|
|
return function () { |
109
|
1 |
|
$settings = $this->getSettings(); |
110
|
1 |
|
$strategy = new Settings($settings); |
111
|
|
|
|
112
|
1 |
|
return $strategy; |
113
|
3 |
|
}; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return Closure |
118
|
|
|
*/ |
119
|
|
|
protected function getCreateAnalyzerClosure() |
120
|
|
|
{ |
121
|
3 |
|
return function ($app) { |
122
|
|
|
/** @var AnalysisStrategyInterface $strategy */ |
123
|
1 |
|
$strategy = $app[AnalysisStrategyInterface::class]; |
124
|
1 |
|
$analyzer = Analyzer::instance($strategy); |
125
|
|
|
|
126
|
1 |
|
$logger = $this->getLoggerIfEnabled($app); |
127
|
1 |
|
$logger === null ?: $analyzer->setLogger($logger); |
128
|
|
|
|
129
|
1 |
|
return $analyzer; |
130
|
3 |
|
}; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
2 |
|
protected function getConfigPath() |
137
|
|
|
{ |
138
|
2 |
|
$root = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR; |
139
|
2 |
|
$path = $root . 'config' . DIRECTORY_SEPARATOR . static::CONFIG_FILE_NAME_WO_EXT . '.php'; |
140
|
|
|
|
141
|
2 |
|
return $path; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param ApplicationInterface $app |
146
|
|
|
* |
147
|
|
|
* @return null|LoggerInterface |
148
|
|
|
*/ |
149
|
1 |
|
protected function getLoggerIfEnabled($app) |
150
|
|
|
{ |
151
|
|
|
/** @var ApplicationInterface $app */ |
152
|
|
|
|
153
|
1 |
|
if ($this->logger === false) { |
154
|
1 |
|
$settings = $this->getSettings(); |
155
|
|
|
$loggingEnabled = |
156
|
1 |
|
array_key_exists(Settings::KEY_LOGS_ENABLED, $settings) === true && |
157
|
1 |
|
$settings[Settings::KEY_LOGS_ENABLED] === true; |
158
|
|
|
|
159
|
1 |
|
$this->logger = $loggingEnabled === true ? $app[LoggerInterface::class] : null; |
160
|
1 |
|
} |
161
|
|
|
|
162
|
1 |
|
return $this->logger; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
2 |
|
protected function getSettings() |
169
|
|
|
{ |
170
|
|
|
/** @var ApplicationInterface $app */ |
171
|
|
|
|
172
|
2 |
|
if ($this->settings === false) { |
173
|
2 |
|
$this->settings = $this->getConfigRepository()->get(static::CONFIG_FILE_NAME_WO_EXT, []); |
174
|
2 |
|
} |
175
|
|
|
|
176
|
2 |
|
return $this->settings; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return Repository |
181
|
|
|
*/ |
182
|
3 |
|
protected function getConfigRepository() |
183
|
|
|
{ |
184
|
|
|
/** @var Repository $config */ |
185
|
3 |
|
$config = $this->app['config']; |
186
|
|
|
|
187
|
3 |
|
return $config; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
1 |
|
protected function getBaseConfig() |
194
|
|
|
{ |
195
|
1 |
|
$path = $this->getConfigPath(); |
196
|
|
|
/** @noinspection PhpIncludeInspection */ |
197
|
1 |
|
$base = require $path; |
198
|
|
|
|
199
|
1 |
|
return $base; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|