1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Lodash package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Longman\LaravelLodash; |
13
|
|
|
|
14
|
|
|
use Blade; |
15
|
|
|
use Illuminate\Http\Request; |
16
|
|
|
use Illuminate\Support\ServiceProvider; |
17
|
|
|
|
18
|
|
|
class LodashServiceProvider extends ServiceProvider |
19
|
|
|
{ |
20
|
11 |
|
public function boot(): void |
21
|
|
|
{ |
22
|
11 |
|
$this->publishes([ |
23
|
11 |
|
__DIR__ . '/../config/config.php' => config_path('lodash.php'), |
24
|
|
|
]); |
25
|
11 |
|
} |
26
|
|
|
|
27
|
11 |
|
public function register(): void |
28
|
|
|
{ |
29
|
11 |
|
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'lodash'); |
30
|
|
|
|
31
|
11 |
|
$this->registerCommands(); |
32
|
|
|
|
33
|
11 |
|
$this->registerRequestMacros(); |
34
|
|
|
|
35
|
11 |
|
$this->registerBladeDirectives(); |
36
|
11 |
|
} |
37
|
|
|
|
38
|
11 |
|
protected function registerCommands(): void |
39
|
|
|
{ |
40
|
11 |
|
if (empty($commands = (array) config('lodash.available_commands'))) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
11 |
|
foreach ($commands as $name => $class) { |
45
|
11 |
|
$this->app->singleton($name, $class); |
46
|
|
|
} |
47
|
|
|
|
48
|
11 |
|
$this->commands(array_keys($commands)); |
49
|
11 |
|
} |
50
|
|
|
|
51
|
11 |
|
protected function registerBladeDirectives(): void |
52
|
|
|
{ |
53
|
11 |
|
$register_directives = config('lodash.register_blade_directives'); |
54
|
|
|
|
55
|
11 |
|
if ($register_directives['datetime'] ?? false) { |
56
|
|
|
Blade::directive('datetime', function ($expression) { |
57
|
|
|
return "<?php echo '<time datetime=\'' . with({$expression})->toIso8601String() |
58
|
|
|
. '\' title=\'' . $expression . '\'>' |
59
|
|
|
. with({$expression})->diffForHumans() . '</time>' ?>"; |
60
|
11 |
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
11 |
|
if ($register_directives['plural'] ?? false) { |
64
|
|
|
Blade::directive('plural', function ($expression) { |
65
|
|
|
$expression = trim($expression, '()'); |
66
|
|
|
list($count, $str, $spacer) = array_pad(preg_split('/,\s*/', $expression), 3, "' '"); |
67
|
|
|
|
68
|
|
|
return "<?php echo $count . $spacer . str_plural($str, $count) ?>"; |
69
|
11 |
|
}); |
70
|
|
|
} |
71
|
11 |
|
} |
72
|
|
|
|
73
|
11 |
|
protected function registerRequestMacros(): void |
74
|
|
|
{ |
75
|
11 |
|
$register_request_macros = config('lodash.register_request_macros'); |
76
|
|
|
|
77
|
11 |
View Code Duplication |
if ($register_request_macros['getInt'] ?? false) { |
|
|
|
|
78
|
|
|
Request::macro('getInt', function (string $name, int $default = 0): int { |
79
|
|
|
return (int) $this->get($name, $default); |
|
|
|
|
80
|
11 |
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
11 |
View Code Duplication |
if ($register_request_macros['getBool'] ?? false) { |
|
|
|
|
84
|
|
|
Request::macro('getBool', function (string $name, bool $default = false): bool { |
85
|
|
|
return (bool) $this->get($name, $default); |
|
|
|
|
86
|
11 |
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
11 |
View Code Duplication |
if ($register_request_macros['getFloat'] ?? false) { |
|
|
|
|
90
|
|
|
Request::macro('getFloat', function (string $name, float $default = 0): float { |
91
|
|
|
return (float) $this->get($name, $default); |
|
|
|
|
92
|
11 |
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
11 |
View Code Duplication |
if ($register_request_macros['getString'] ?? false) { |
|
|
|
|
96
|
11 |
|
Request::macro('getString', function (string $name, string $default = ''): string { |
97
|
|
|
return (string) $this->get($name, $default); |
|
|
|
|
98
|
11 |
|
}); |
99
|
|
|
} |
100
|
11 |
|
} |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.