|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zoutapps\Laravel\Backpack\Branding\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
|
|
7
|
|
|
trait ValueReplacerTrait |
|
8
|
|
|
{ |
|
9
|
|
|
protected $file; |
|
10
|
|
|
|
|
11
|
|
|
protected $appends = false; |
|
12
|
|
|
protected $defaults; |
|
13
|
|
|
|
|
14
|
|
|
protected function escape($value) |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
return $value; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected function old($key) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
return null; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
protected function replace($key, $value) |
|
25
|
|
|
{ |
|
26
|
|
|
return $key . '=' . $value; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function search($key, $old) |
|
30
|
|
|
{ |
|
31
|
|
|
return '/^' . $key . preg_quote('=' . $old, '/') . '.*$/m'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function applyValues() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->command->line('Enter the values you get asked for.'); |
|
|
|
|
|
|
37
|
|
|
$this->command->line('Leave blank if you don\'t want to change'); |
|
38
|
|
|
|
|
39
|
|
|
collect($this->keys)->each(function ($key) { |
|
|
|
|
|
|
40
|
|
|
if ($this->defaults->has($key)) { |
|
41
|
|
|
$this->setValueForKey($this->defaults[$key], $key); |
|
42
|
|
|
$this->defaults->forget($key); |
|
43
|
|
|
} else { |
|
44
|
|
|
$value = $this->command->ask($key .' ('.$this->old($key).')'); |
|
45
|
|
|
if ($value) { |
|
46
|
|
|
$this->setValueForKey($value, $key); |
|
47
|
|
|
} else { |
|
48
|
|
|
$this->command->line('<comment>'.$key . '</comment> not changed.'); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
}); |
|
52
|
|
|
|
|
53
|
|
|
$this->defaults->each(function ($value, $key) { |
|
54
|
|
|
$this->setValueForKey($value, $key); |
|
55
|
|
|
}); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function setValueForKey($value, $key) |
|
59
|
|
|
{ |
|
60
|
|
|
if (is_bool($value)) { |
|
61
|
|
|
$value = $value ? 'true': 'false'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$value = $this->escape($value); |
|
65
|
|
|
$replace = $this->replace($key, $value); |
|
66
|
|
|
|
|
67
|
|
|
if (Str::contains(file_get_contents($this->file), $key) === false) { |
|
68
|
|
|
if (!$this->appends) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
file_put_contents($this->file, PHP_EOL . $replace, FILE_APPEND); |
|
72
|
|
|
} else { |
|
73
|
|
|
$old = $this->old($key); |
|
|
|
|
|
|
74
|
|
|
$search = $this->search($key, $old); |
|
75
|
|
|
|
|
76
|
|
|
$currentContent = file_get_contents($this->file); |
|
77
|
|
|
$replacedContent = preg_replace($search, $replace, $currentContent); |
|
78
|
|
|
file_put_contents($this->file, $replacedContent); |
|
79
|
|
|
} |
|
80
|
|
|
if (isset($this->command)) { |
|
81
|
|
|
$this->command->line("Setting <comment>{$replace}</comment>"); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.