1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yemenifree\LaravelArabicNumbersMiddleware; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
7
|
|
|
use Yemenifree\LaravelArabicNumbersMiddleware\Middleware\TransformArabicToEasternNumbers; |
8
|
|
|
use Yemenifree\LaravelArabicNumbersMiddleware\Middleware\TransformEasternToArabicNumbers; |
9
|
|
|
|
10
|
|
|
class ServiceProvider extends BaseServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* All of the short-hand keys for middlewares. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $middleware = [ |
18
|
|
|
'arabic-to-eastern' => TransformArabicToEasternNumbers::class, |
19
|
|
|
'eastern-to-arabic' => TransformEasternToArabicNumbers::class |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* list of group middleware to auto append middleware to them. |
24
|
|
|
* |
25
|
|
|
* true => all groups |
26
|
|
|
* false => none |
27
|
|
|
* ['web'] => for web group only |
28
|
|
|
* |
29
|
|
|
* @var bool|array |
30
|
|
|
*/ |
31
|
|
|
protected $groupMiddleware = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* auto append middleware. |
35
|
|
|
* |
36
|
|
|
* @var Collection |
37
|
|
|
*/ |
38
|
|
|
protected $auto_middleware; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Perform post-registration booting of services. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
12 |
|
public function boot() |
47
|
|
|
{ |
48
|
12 |
|
$this->publishes([ |
49
|
12 |
|
__DIR__.'/../config/arabic-numbers-middleware.php' => config_path('arabic-numbers-middleware.php'), |
50
|
12 |
|
], 'config'); |
51
|
|
|
|
52
|
12 |
|
$this->autoAppendMiddleware(); |
53
|
12 |
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* auto append middleware to router. |
58
|
|
|
*/ |
59
|
12 |
|
protected function autoAppendMiddleware() |
60
|
|
|
{ |
61
|
12 |
|
$this->groupMiddleware = $this->app['config']->get('arabic-numbers-middleware.auto_register_middleware', false); |
62
|
12 |
|
$this->auto_middleware = $this->app['config']->get('arabic-numbers-middleware.auto_middleware', false); |
63
|
|
|
|
64
|
12 |
|
if ($this->groupMiddleware === false || $this->auto_middleware === false) { |
65
|
4 |
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
12 |
|
if ($this->groupMiddleware === true) { // Register middleware as global Middleware |
69
|
12 |
|
$this->app->make('Illuminate\Contracts\Http\Kernel')->pushMiddleware($this->auto_middleware); |
70
|
|
|
} else if (is_array($this->groupMiddleware) && count($this->groupMiddleware) > 0) { // Register Middleware for route group |
71
|
|
|
$this->pushMiddlewareToGroups($this->auto_middleware); |
72
|
|
|
} |
73
|
|
|
|
74
|
12 |
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* push middleware to route groups. |
79
|
|
|
* |
80
|
|
|
* @param $middleware |
81
|
|
|
*/ |
82
|
|
|
function pushMiddlewareToGroups($middleware) |
83
|
|
|
{ |
84
|
|
|
foreach ($this->groupMiddleware as $group) { |
|
|
|
|
85
|
|
|
$this->app['router']->pushMiddlewareToGroup($group, $middleware); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Register any package services. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
12 |
|
public function register() |
95
|
|
|
{ |
96
|
12 |
|
$this->mergeConfigFrom(__DIR__.'/../config/arabic-numbers-middleware.php', 'arabic-numbers-middleware'); |
97
|
12 |
|
$this->registerAliasMiddleware(); |
98
|
12 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* register alias middleware. |
102
|
|
|
*/ |
103
|
12 |
|
protected function registerAliasMiddleware() |
104
|
|
|
{ |
105
|
12 |
|
foreach ($this->middleware as $alias => $middleware) { |
106
|
12 |
|
$this->app['router']->aliasMiddleware($alias, $middleware); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.