1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSlant\Blog\Api\Providers; |
4
|
|
|
|
5
|
|
|
use CSlant\Blog\Api\Http\Middlewares\ApiActionRateLimiter; |
6
|
|
|
use Illuminate\Routing\Router; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
|
9
|
|
|
class BlogApiServiceProvider extends ServiceProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Bootstrap services. |
13
|
|
|
* |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
|
|
public function boot(): void |
17
|
|
|
{ |
18
|
|
|
$routePath = __DIR__.'/../../routes/blog-api.php'; |
19
|
|
|
if (file_exists($routePath)) { |
20
|
|
|
$this->loadRoutesFrom($routePath); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../../lang', 'blog-api'); |
24
|
|
|
|
25
|
|
|
$this->loadMigrationsFrom(__DIR__.'/../../database/migrations'); |
26
|
|
|
|
27
|
|
|
$this->registerCommands(); |
28
|
|
|
|
29
|
|
|
$this->registerAssetPublishing(); |
30
|
|
|
|
31
|
|
|
$this->resourceOverride(); |
32
|
|
|
|
33
|
|
|
$this->registerMiddlewares(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Register services. |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function register(): void |
42
|
|
|
{ |
43
|
|
|
$this->registerConfigs(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Register configs. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
protected function registerConfigs(): void |
52
|
|
|
{ |
53
|
|
|
$configDir = __DIR__.'/../../config'; |
54
|
|
|
$files = scandir($configDir); |
55
|
|
|
|
56
|
|
|
if ($files === false) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
foreach ($files as $file) { |
61
|
|
|
if (pathinfo($file, PATHINFO_EXTENSION) === 'php') { |
62
|
|
|
$configName = pathinfo($file, PATHINFO_FILENAME); |
63
|
|
|
$configPath = $configDir.'/'.$file; |
64
|
|
|
|
65
|
|
|
if (file_exists(config_path($configName.'.php'))) { |
|
|
|
|
66
|
|
|
config()->set($configName, array_merge( |
|
|
|
|
67
|
|
|
is_array(config($configName)) ? config($configName) : [], |
|
|
|
|
68
|
|
|
require $configPath |
69
|
|
|
)); |
70
|
|
|
} else { |
71
|
|
|
$this->mergeConfigFrom($configPath, $configName); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the services provided by the provider. |
79
|
|
|
* |
80
|
|
|
* @return null|array<string> |
81
|
|
|
*/ |
82
|
|
|
public function provides(): ?array |
83
|
|
|
{ |
84
|
|
|
return ['blog-api']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
protected function registerCommands(): void |
91
|
|
|
{ |
92
|
|
|
$this->commands([ |
93
|
|
|
// |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function registerAssetPublishing(): void |
101
|
|
|
{ |
102
|
|
|
$configPath = __DIR__.'/../../config/blog-api.php'; |
103
|
|
|
$this->publishes([ |
104
|
|
|
$configPath => config_path('blog-api.php'), |
105
|
|
|
], 'config'); |
106
|
|
|
|
107
|
|
|
$this->publishes([ |
108
|
|
|
__DIR__.'/../../lang' => resource_path('lang/packages/blog-api'), |
109
|
|
|
], 'lang'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Override resource of the package. |
114
|
|
|
*/ |
115
|
|
|
public function resourceOverride(): void |
116
|
|
|
{ |
117
|
|
|
if (!class_exists(\Botble\Blog\Http\Resources\TagResource::class, false)) { |
118
|
|
|
class_alias( |
119
|
|
|
\CSlant\Blog\Api\Http\Resources\Tag\TagResource::class, |
120
|
|
|
\Botble\Blog\Http\Resources\TagResource::class |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Register middlewares for the package. |
127
|
|
|
*/ |
128
|
|
|
protected function registerMiddlewares(): void |
129
|
|
|
{ |
130
|
|
|
/** @var Router $router */ |
131
|
|
|
$router = $this->app->make('router'); |
132
|
|
|
|
133
|
|
|
// Register route middleware |
134
|
|
|
$router->aliasMiddleware('api-action-rate-limiter', ApiActionRateLimiter::class); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|