forked from jamesmills/laravel-timezone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaravelTimezoneServiceProvider.php
94 lines (80 loc) · 2.81 KB
/
LaravelTimezoneServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace JamesMills\LaravelTimezone;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use JamesMills\LaravelTimezone\Listeners\Auth\UpdateUsersTimezone;
class LaravelTimezoneServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
// Allow migrations publish
if (! class_exists('AddTimezoneColumnToUsersTable')) {
$this->publishes([
__DIR__ . '/database/migrations/add_timezone_column_to_users_table.php.stub' => database_path('/migrations/' . date('Y_m_d_His') . '_add_timezone_column_to_users_table.php'),
], 'migrations');
}
// Register the Timezone alias
AliasLoader::getInstance()->alias('Timezone', \JamesMills\LaravelTimezone\Facades\Timezone::class);
// Register an event listener
$this->registerEventListener();
// Allow config publish
$this->publishes([
__DIR__ . '/config/timezone.php' => config_path('timezone.php'),
], 'config');
// Register a blade directive to show user date/time in their timezone
Blade::directive(
'displayDate',
function ($expression) {
$options = explode(',', $expression);
if (count($options) == 1) {
return "<?php echo e(Timezone::convertToLocal($options[0])); ?>";
} elseif (count($options) == 2) {
return "<?php echo e(Timezone::convertToLocal($options[0], $options[1])); ?>";
} elseif (count($options) == 3) {
return "<?php echo e(Timezone::convertToLocal($options[0], $options[1], $options[2])); ?>";
} elseif (count($options) == 4) {
return "<?php echo e(Timezone::convertToLocal($options[0], $options[1], $options[2], $options[3])); ?>";
} else {
return 'error';
}
}
);
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('timezone', Timezone::class);
$this->mergeConfigFrom(
__DIR__ . '/config/timezone.php',
'timezone'
);
}
/**
*
*/
private function registerEventListener(): void
{
$events = [
\Illuminate\Auth\Events\Login::class,
\Laravel\Passport\Events\AccessTokenCreated::class,
];
Event::listen($events, UpdateUsersTimezone::class);
}
}