-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathServiceProvider.php
81 lines (63 loc) · 2.52 KB
/
ServiceProvider.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
<?php
namespace CustomD\EloquentModelEncrypt;
use Illuminate\Support\Fluent;
use Illuminate\Support\Facades\Event;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\ColumnDefinition;
use Illuminate\Database\Schema\Grammars\Grammar;
use CustomD\EloquentModelEncrypt\Store\SessionPem;
use CustomD\EloquentModelEncrypt\Console\Commands\EncryptModel;
use CustomD\EloquentModelEncrypt\Exceptions\UnknownGrammerException;
use CustomD\EloquentModelEncrypt\Listeners\UserEventSubscriber;
class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
protected const CONFIG_PATH = __DIR__ . '/../config/eloquent-model-encrypt.php';
protected const MIGRATIONS_PATH = __DIR__ . '/../database/migrations/';
public function boot()
{
$this->publishes([
self::CONFIG_PATH => config_path('eloquent-model-encrypt.php'),
], 'eloquent-model-encrypt_config');
$this->publishes([
self::MIGRATIONS_PATH => base_path('database/migrations/'),
], 'eloquent-model-encrypt_migration');
if ($this->app->runningInConsole()) {
$this->commands(EncryptModel::class);
}
}
public function register()
{
$this->mergeConfigFrom(
self::CONFIG_PATH,
'eloquent-model-encrypt'
);
Grammar::macro('typeEncrypted', function (Fluent $column) {
$className = (new \ReflectionClass($this))->getShortName();
if ($className === "MySqlGrammar") {
return 'blob';
}
if ($className === "PostgresGrammar") {
return 'bytea';
}
if ($className === "SQLiteGrammar") {
return 'blob';
}
if ($className === "SqlServerGrammar") {
return 'varbinary(max)';
}
throw new UnknownGrammerException();
});
Blueprint::macro('encrypted', function ($column): ColumnDefinition {
/** @var Blueprint $this */
return $this->addColumn('encrypted', $column);
});
/** @var class-string<\CustomD\EloquentModelEncrypt\Contracts\PemStore> $pemStore */
$pemStore = config('eloquent-model-encrypt.pem.class', SessionPem::class);
if ($pemStore) {
$this->app->bind('cd-pem-store', fn ($app) => new $pemStore(config('eloquent-model-encrypt.pem')));
}
if (config('eloquent-model-encrypt.listener')) {
Event::subscribe(UserEventSubscriber::class);
}
}
}