1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Squanto\Translators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Schema; |
6
|
|
|
use Illuminate\Translation\Translator as LaravelTranslator; |
7
|
|
|
use Thinktomorrow\Squanto\Domain\PageKey; |
8
|
|
|
|
9
|
|
|
class SquantoTranslator extends LaravelTranslator implements Translator |
10
|
|
|
{ |
11
|
|
|
private $databaseTranslator; |
12
|
|
|
|
13
|
|
|
private $keyAsDefault = true; |
14
|
|
|
private $isDatabaseAlreadyMigrated = null; |
15
|
|
|
|
16
|
57 |
|
public function setKeyAsDefault($keyAsDefault = true) |
17
|
|
|
{ |
18
|
57 |
|
$this->keyAsDefault = $keyAsDefault; |
19
|
57 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get the translation for the given key by following this priority chain: |
23
|
|
|
* |
24
|
|
|
* 1. Get from our cached translations |
25
|
|
|
* 2. Get from database |
26
|
|
|
* 3. Get from the /resources/lang |
27
|
|
|
* |
28
|
|
|
* @param string $key |
29
|
|
|
* @param array $replace |
30
|
|
|
* @param string $locale |
31
|
|
|
* @param bool $fallback |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
12 |
|
public function get($key, array $replace = array(), $locale = null, $fallback = true) |
35
|
|
|
{ |
36
|
12 |
|
$locale = $locale ?: $this->getLocale(); |
37
|
|
|
|
38
|
|
|
// The key is always stored as lowercase so make sure our key input is sanitized as well. |
39
|
12 |
|
$key = strtolower($key); |
40
|
|
|
|
41
|
12 |
|
if( null !== ($result = $this->getFromExcludedSource($key, $replace, $locale, $fallback))) { |
42
|
|
|
return $result; |
43
|
|
|
} |
44
|
|
|
|
45
|
12 |
|
if ( null !== ($result = $this->getFromCache($key, $replace, $locale, $fallback))) { |
46
|
7 |
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
if (null !== ($result = $this->getFromDatabase($key, $replace, $locale, $fallback))) { |
50
|
1 |
|
return $result; |
51
|
|
|
} |
52
|
|
|
|
53
|
5 |
|
$result = parent::get($key, $replace, $locale, $fallback); |
54
|
|
|
|
55
|
5 |
|
return ($this->keyAsDefault || $result !== $key) ? $result : null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get from excluded sources. This is used here to make retrieval of these |
60
|
|
|
* non-managed translations a lot faster by going straight to source |
61
|
|
|
* |
62
|
|
|
* @param $key |
63
|
|
|
* @param $replace |
64
|
|
|
* @param $locale |
65
|
|
|
* @param $fallback |
66
|
|
|
* @return array|null|string |
67
|
|
|
*/ |
68
|
12 |
|
private function getFromExcludedSource($key, $replace, $locale, $fallback) |
69
|
|
|
{ |
70
|
12 |
|
if(!PageKey::fromLineKeyString($key)->isExcludedSource()) return null; |
71
|
|
|
|
72
|
|
|
return parent::get($key, $replace, $locale, $fallback); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Retrieve the translation from the squanto cache. |
77
|
|
|
* |
78
|
|
|
* @param $key |
79
|
|
|
* @param array $replace |
80
|
|
|
* @param null $locale |
81
|
|
|
* @return mixed|null |
82
|
|
|
*/ |
83
|
12 |
|
private function getFromCache($key, array $replace = array(), $locale = null, $fallback = true) |
84
|
|
|
{ |
85
|
12 |
|
if (false === strpos($key, 'squanto::')) { |
86
|
12 |
|
$key = 'squanto::'.$key; |
87
|
|
|
} |
88
|
|
|
|
89
|
12 |
|
$result = parent::get($key, $replace, $locale, $fallback); |
|
|
|
|
90
|
|
|
|
91
|
12 |
|
return ($result !== $key) ? $result : null; |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
private function getFromDatabase($key, array $replace = array(), $locale = null, $fallback = true) |
95
|
|
|
{ |
96
|
|
|
/** |
97
|
|
|
* If database tables are not present we will soft ignore this call and delegate to the native |
98
|
|
|
*/ |
99
|
6 |
|
if (! $this->isDatabaseAlreadyMigrated()) { |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
6 |
|
if (!isset($this->databaseTranslator)) { |
104
|
6 |
|
$this->databaseTranslator = app(DatabaseTranslator::class); |
105
|
|
|
} |
106
|
|
|
|
107
|
6 |
|
return $this->databaseTranslator->get($key, $replace, $locale, $fallback); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Verify that SQUANTO migrations are already run and present in this environment |
112
|
|
|
* Allow for a soft install |
113
|
|
|
* |
114
|
|
|
* @return null |
115
|
|
|
*/ |
116
|
6 |
|
private function isDatabaseAlreadyMigrated() |
117
|
|
|
{ |
118
|
6 |
|
if (!is_null($this->isDatabaseAlreadyMigrated)) { |
119
|
|
|
return $this->isDatabaseAlreadyMigrated; |
120
|
|
|
} |
121
|
|
|
|
122
|
6 |
|
return ($this->isDatabaseAlreadyMigrated = Schema::hasTable('squanto_lines')); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.