1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCed; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use Illuminate\Database\Schema\Blueprint; |
7
|
|
|
|
8
|
|
|
class LaraCedServiceProvider extends ServiceProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Register the service provider. |
12
|
|
|
* |
13
|
|
|
* @return void |
14
|
|
|
*/ |
15
|
|
|
public function register() |
16
|
|
|
{ |
17
|
|
|
$userClass = config('auth.providers.users.model'); |
18
|
|
|
|
19
|
|
|
$userClass = new $userClass; |
20
|
|
|
|
21
|
|
|
$userModel = $userClass->getTable(); |
22
|
|
|
|
23
|
|
|
Blueprint::macro('ced', function () { |
24
|
|
|
$this->unsignedInteger('created_by')->nullable()->index(); |
25
|
|
|
$this->unsignedInteger('updated_by')->nullable()->index(); |
26
|
|
|
$this->unsignedInteger('deleted_by')->nullable()->index(); |
27
|
|
|
}); |
28
|
|
|
|
29
|
|
|
Blueprint::macro('creator', function ($name = 'created_by') { |
30
|
|
|
$this->unsignedInteger($name)->nullable()->index(); |
31
|
|
|
}); |
32
|
|
|
Blueprint::macro('editor', function ($name = 'updated_by') { |
33
|
|
|
$this->unsignedInteger($name)->nullable()->index(); |
34
|
|
|
}); |
35
|
|
|
Blueprint::macro('destroyer', function ($name = 'deleted_by') { |
36
|
|
|
$this->unsignedInteger($name)->nullable()->index(); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
Blueprint::macro('dropCed', function () { |
40
|
|
|
$this->dropColumn(['created_by', 'updated_by', 'deleted_by']); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
Blueprint::macro('cedForeign', function () use ($userModel) { |
44
|
|
|
$tableName = $this->getTable(); |
45
|
|
|
|
46
|
|
|
$this->foreign('created_by', 'fk_' . $tableName . 'has_created_by')->references('id')->on($userModel)->onUpdate('NO ACTION')->onDelete('NO ACTION'); |
47
|
|
|
|
48
|
|
|
$this->foreign('updated_by', 'fk_' . $tableName . 'has_updated_by')->references('id')->on($userModel)->onUpdate('NO ACTION')->onDelete('NO ACTION'); |
49
|
|
|
|
50
|
|
|
$this->foreign('deleted_by', 'fk_' . $tableName . 'has_deleted_by')->references('id')->on($userModel)->onUpdate('NO ACTION')->onDelete('NO ACTION'); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
View Code Duplication |
Blueprint::macro('creatorForeign', function ($name = 'created_by') use ($userModel) { |
|
|
|
|
54
|
|
|
$tableName = $this->getTable(); |
55
|
|
|
|
56
|
|
|
$this->foreign($name, 'fk_' . $tableName . 'has_' . $name)->references('id')->on($userModel)->onUpdate('NO ACTION')->onDelete('NO ACTION'); |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
View Code Duplication |
Blueprint::macro('editorForeign', function ($name = 'updated_by') use ($userModel) { |
|
|
|
|
60
|
|
|
$tableName = $this->getTable(); |
61
|
|
|
|
62
|
|
|
$this->foreign($name, 'fk_' . $tableName . 'has_' . $name)->references('id')->on($userModel)->onUpdate('NO ACTION')->onDelete('NO ACTION'); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
View Code Duplication |
Blueprint::macro('destroyerForeign', function ($name = 'deleted_by') use ($userModel) { |
|
|
|
|
66
|
|
|
$tableName = $this->getTable(); |
67
|
|
|
|
68
|
|
|
$this->foreign($name, 'fk_' . $tableName . 'has_' . $name)->references('id')->on($userModel)->onUpdate('NO ACTION')->onDelete('NO ACTION'); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
Blueprint::macro('dropCedForeign', function () use ($userModel) { |
72
|
|
|
$tableName = $this->getTable(); |
73
|
|
|
|
74
|
|
|
$this->dropForeign('fk_' . $tableName . '_has_created_by'); |
75
|
|
|
$this->dropForeign('fk_' . $tableName . '_has_updated_by'); |
76
|
|
|
$this->dropForeign('fk_' . $tableName . '_has_deleted_by'); |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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.