1
|
|
|
<?php |
2
|
|
|
namespace TopviewDigital\TranslationHelper\Service; |
3
|
|
|
|
4
|
|
|
use TopviewDigital\TranslationHelper\Model\VocabTerm; |
5
|
|
|
use TopviewDigital\TranslationHelper\Model\VocabCite; |
6
|
|
|
use TopviewDigital\TranslationHelper\Interfaces\AsyncBrokerInterface; |
7
|
|
|
|
8
|
|
|
class CiteUpdater implements AsyncBrokerInterface |
9
|
|
|
{ |
10
|
|
|
protected $cite; |
11
|
|
|
protected $term; |
12
|
|
|
|
13
|
|
|
public function __construct(VocabTerm $vocab, array $tracer) |
14
|
|
|
{ |
15
|
|
|
$cite = []; |
16
|
|
|
//Clean Tail Text |
17
|
|
|
$cite['file'] = preg_replace('/(\.php)(.+)$/', '${1}', $tracer[0]['file']); |
18
|
|
|
//Remove Head of Base_Path |
19
|
|
|
$cite['file'] = str_replace(base_path(), '', $cite['file']); |
20
|
|
|
$cite['line'] = $tracer[0]['line']; |
21
|
|
|
array_shift($tracer); |
22
|
|
|
$cite['function'] = $tracer[0]['function'] ?? ''; |
23
|
|
|
$cite['class'] = $tracer[0]['class'] ?? ''; |
24
|
|
|
$cite = VocabCite::firstOrCreate($cite); |
25
|
|
|
$cite->refresh(); |
26
|
|
|
$vocab->cites()->sync([$cite->id], false); |
27
|
|
|
$cite->save(); |
28
|
|
|
$this->cite = $cite->id; |
29
|
|
|
$this->term = $vocab->term; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function initVocabCite(VocabCite $cite) |
33
|
|
|
{ |
34
|
|
|
if (empty($cite->code)) { |
35
|
|
|
$lines = explode("\n", file_get_contents(base_path() . $cite->file)); |
|
|
|
|
36
|
|
|
$cite->code = $lines[$cite->line - 1]; |
|
|
|
|
37
|
|
|
if (substr($cite->file, -10) != '.blade.php') { |
38
|
|
|
for ($start = $cite->line - 2; $start > -1; $start--) { |
39
|
|
|
$char = substr(rtrim($lines[$start]), -1); |
40
|
|
|
if ($char == ';' || $char == '}' || $char == '{') { |
41
|
|
|
$start++; |
42
|
|
|
break; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
$count = count($lines); |
46
|
|
|
for ($end = $cite->line - 1; $end < $count; $end++) { |
47
|
|
|
$char = substr(rtrim($lines[$end]), -1); |
48
|
|
|
if ($char == ';') { |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
$code = array_filter(array_slice($lines, $start, $end - $start + 1, true), 'trim'); |
53
|
|
|
$max = strlen($end); |
54
|
|
|
$cite->code = implode("\n", array_map(function ($u, $v) use ($max) { |
55
|
|
|
return sprintf("%{$max}d %s ", $u + 1, rtrim($v)); |
56
|
|
|
}, array_keys($code), $code)); |
57
|
|
|
$cite->save(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function handle() |
63
|
|
|
{ |
64
|
|
|
$this->initVocabCite(VocabCite::find($this->cite)); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.