|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Longman\LaravelLodash; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Support\ServiceProvider; |
|
9
|
|
|
use Longman\LaravelLodash\Commands\ClearAll; |
|
10
|
|
|
use Longman\LaravelLodash\Commands\DbClear; |
|
11
|
|
|
use Longman\LaravelLodash\Commands\DbDump; |
|
12
|
|
|
use Longman\LaravelLodash\Commands\DbRestore; |
|
13
|
|
|
use Longman\LaravelLodash\Commands\LogClear; |
|
14
|
|
|
use Longman\LaravelLodash\Commands\UserAdd; |
|
15
|
|
|
use Longman\LaravelLodash\Commands\UserPassword; |
|
16
|
|
|
|
|
17
|
|
|
use function array_keys; |
|
18
|
|
|
use function array_pad; |
|
19
|
|
|
use function preg_split; |
|
20
|
|
|
use function trim; |
|
21
|
|
|
|
|
22
|
|
|
class LodashServiceProvider extends ServiceProvider |
|
23
|
|
|
{ |
|
24
|
|
|
protected $commands = [ |
|
25
|
|
|
'command.lodash.clear-all' => ClearAll::class, |
|
26
|
|
|
'command.lodash.db.clear' => DbClear::class, |
|
27
|
|
|
'command.lodash.db.dump' => DbDump::class, |
|
28
|
|
|
'command.lodash.db.restore' => DbRestore::class, |
|
29
|
|
|
'command.lodash.log.clear' => LogClear::class, |
|
30
|
|
|
'command.lodash.user.add' => UserAdd::class, |
|
31
|
|
|
'command.lodash.user.password' => UserPassword::class, |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
71 |
|
public function boot(): void |
|
35
|
|
|
{ |
|
36
|
71 |
|
$this->publishes([ |
|
37
|
71 |
|
__DIR__ . '/../config/config.php' => config_path('lodash.php'), |
|
38
|
|
|
]); |
|
39
|
|
|
|
|
40
|
71 |
|
$this->registerBladeDirectives(); |
|
41
|
|
|
|
|
42
|
71 |
|
$this->loadTranslations(); |
|
43
|
71 |
|
} |
|
44
|
|
|
|
|
45
|
71 |
|
public function register(): void |
|
46
|
|
|
{ |
|
47
|
71 |
|
$this->registerCommands(); |
|
48
|
|
|
|
|
49
|
71 |
|
$this->registerRequestMacros(); |
|
50
|
71 |
|
} |
|
51
|
|
|
|
|
52
|
71 |
|
protected function registerCommands(): void |
|
53
|
|
|
{ |
|
54
|
71 |
|
foreach ($this->commands as $name => $class) { |
|
55
|
71 |
|
$this->app->singleton($name, $class); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
71 |
|
$this->commands(array_keys($this->commands)); |
|
59
|
71 |
|
} |
|
60
|
|
|
|
|
61
|
71 |
|
protected function registerBladeDirectives(): void |
|
62
|
|
|
{ |
|
63
|
|
|
// Display relative time |
|
64
|
71 |
|
app('blade.compiler')->directive('datetime', static function ($expression) { |
|
65
|
|
|
|
|
66
|
|
|
return "<?php echo '<time datetime=\'' . with({$expression})->toIso8601String() |
|
67
|
|
|
. '\' title=\'' . $expression . '\'>' |
|
68
|
|
|
. with({$expression})->diffForHumans() . '</time>' ?>"; |
|
69
|
71 |
|
}); |
|
70
|
|
|
|
|
71
|
|
|
// Pluralization helper |
|
72
|
71 |
|
app('blade.compiler')->directive('plural', static function ($expression) { |
|
73
|
|
|
$expression = trim($expression, '()'); |
|
74
|
|
|
[$count, $str, $spacer] = array_pad(preg_split('/,\s*/', $expression), 3, "' '"); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
return "<?php echo $count . $spacer . str_plural($str, $count) ?>"; |
|
77
|
71 |
|
}); |
|
78
|
71 |
|
} |
|
79
|
|
|
|
|
80
|
71 |
|
protected function registerRequestMacros(): void |
|
81
|
|
|
{ |
|
82
|
71 |
|
Request::macro('getInt', function (string $name, int $default = 0): int { |
|
83
|
|
|
|
|
84
|
|
|
return (int) $this->get($name, $default); |
|
|
|
|
|
|
85
|
71 |
|
}); |
|
86
|
|
|
|
|
87
|
71 |
|
Request::macro('getBool', function (string $name, bool $default = false): bool { |
|
88
|
|
|
|
|
89
|
|
|
return (bool) $this->get($name, $default); |
|
|
|
|
|
|
90
|
71 |
|
}); |
|
91
|
|
|
|
|
92
|
71 |
|
Request::macro('getFloat', function (string $name, float $default = 0): float { |
|
93
|
|
|
|
|
94
|
|
|
return (float) $this->get($name, $default); |
|
|
|
|
|
|
95
|
71 |
|
}); |
|
96
|
|
|
|
|
97
|
71 |
|
Request::macro('getString', function (string $name, string $default = ''): string { |
|
98
|
|
|
|
|
99
|
|
|
return (string) $this->get($name, $default); |
|
|
|
|
|
|
100
|
71 |
|
}); |
|
101
|
71 |
|
} |
|
102
|
|
|
|
|
103
|
71 |
|
protected function loadTranslations(): void |
|
104
|
|
|
{ |
|
105
|
71 |
|
$this->loadTranslationsFrom(__DIR__ . '/../translations', 'lodash'); |
|
106
|
|
|
|
|
107
|
71 |
|
$this->publishes([ |
|
108
|
71 |
|
__DIR__ . '/../translations' => resource_path('lang/vendor/lodash'), |
|
109
|
|
|
]); |
|
110
|
71 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.