|
2 | 2 |
|
3 | 3 | namespace Vcian\LaravelDBAuditor\Traits;
|
4 | 4 |
|
| 5 | +use Illuminate\Support\Facades\File; |
5 | 6 | use Vcian\LaravelDBAuditor\Constants\Constant;
|
6 | 7 |
|
7 | 8 | trait DBMigrationTrack
|
8 | 9 | {
|
| 10 | + /** |
| 11 | + * Get Database related information from the migration file. |
| 12 | + * Collect all the details into one place. |
| 13 | + */ |
| 14 | + public function collectDataFromFile($type = 'command') |
| 15 | + { |
| 16 | + $data = []; |
| 17 | + |
| 18 | + $filesInFolder = File::files(database_path('migrations')); |
| 19 | + |
| 20 | + foreach ($filesInFolder as $path) { |
| 21 | + $file = pathinfo($path); |
| 22 | + |
| 23 | + $fileName = $file['basename']; |
| 24 | + |
| 25 | + if ($type === 'command') { |
| 26 | + array_push($data, |
| 27 | + [ |
| 28 | + $this->getMigrationDate($file['filename']), |
| 29 | + $this->getMigrationTableName($fileName), |
| 30 | + $this->replaceStringWithDots($this->getMigrationFieldName($fileName)), |
| 31 | + $this->getMigrationAction($fileName), |
| 32 | + $fileName, |
| 33 | + $this->getMigrationStatus($file['filename']), |
| 34 | + $this->getMigrationCreatedBy($fileName), |
| 35 | + ] |
| 36 | + ); |
| 37 | + } else { |
| 38 | + array_push($data, |
| 39 | + [ |
| 40 | + 'date' => $this->getMigrationDate($file['filename']), |
| 41 | + 'table' => $this->getMigrationTableName($fileName), |
| 42 | + 'fields' => $this->getMigrationFieldName($fileName), |
| 43 | + 'action' => $this->getMigrationAction($fileName), |
| 44 | + 'file' => $fileName, |
| 45 | + 'status' => $this->getMigrationStatus($file['filename']), |
| 46 | + 'createdby' => $this->getMigrationCreatedBy($fileName), |
| 47 | + ] |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + return $data; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Filter based on the command argument. |
| 58 | + */ |
| 59 | + public function filter($filterType, $data, $filter) |
| 60 | + { |
| 61 | + $result = array_filter($data, function ($item) use ($filter, $filterType) { |
| 62 | + |
| 63 | + switch ($filterType) { |
| 64 | + case 'table': |
| 65 | + return $item[1] === $filter; |
| 66 | + case 'action': |
| 67 | + return $item[3] === $filter; |
| 68 | + case 'status': |
| 69 | + return $item[5] === $filter; |
| 70 | + default: |
| 71 | + return $item; |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + return array_values($result); |
| 76 | + } |
| 77 | + |
9 | 78 | /**
|
10 | 79 | * Read File and Get File Content
|
11 | 80 | */
|
@@ -112,10 +181,12 @@ public function replaceStringWithDots(string $inputString): string
|
112 | 181 | */
|
113 | 182 | public function getMigrationCreatedBy(string $file): string
|
114 | 183 | {
|
| 184 | + $currentUser = gethostname(); |
| 185 | + |
115 | 186 | $repositoryPath = base_path();
|
116 | 187 | $migrationFilePath = 'database/migrations/'.$file;
|
117 | 188 | $authorName = trim(shell_exec("git -C $repositoryPath log --follow --format='%an' -- $migrationFilePath"));
|
118 | 189 |
|
119 |
| - return $authorName != '' ? $authorName : '-'; |
| 190 | + return $authorName != '' ? $authorName : $currentUser; |
120 | 191 | }
|
121 | 192 | }
|
0 commit comments