1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel MultiLang package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\LaravelMultiLang; |
12
|
|
|
|
13
|
|
|
use Illuminate\Cache\CacheManager as Cache; |
14
|
|
|
use Illuminate\Database\DatabaseManager as Database; |
15
|
|
|
use Illuminate\Http\Request; |
16
|
|
|
use Illuminate\Support\Collection; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
use Longman\LaravelMultiLang\Config; |
19
|
|
|
|
20
|
|
|
class Repository |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Language/Locale. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $lang; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The instance of the cache. |
31
|
|
|
* |
32
|
|
|
* @var \Illuminate\Cache\CacheManager |
33
|
|
|
*/ |
34
|
|
|
protected $cache; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Config. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $config; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The instance of the database. |
45
|
|
|
* |
46
|
|
|
* @var \Illuminate\Database\DatabaseManager |
47
|
|
|
*/ |
48
|
|
|
protected $db; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Name of the cache. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $cache_name; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create a new MultiLang instance. |
61
|
|
|
* |
62
|
|
|
* @param string $environment |
|
|
|
|
63
|
|
|
* @param array $config |
64
|
|
|
* @param \Illuminate\Cache\CacheManager $cache |
65
|
|
|
* @param \Illuminate\Database\DatabaseManager $db |
66
|
|
|
*/ |
67
|
|
|
public function __construct(Config $config, Cache $cache, Database $db) |
68
|
|
|
{ |
69
|
|
|
$this->config = $config; |
|
|
|
|
70
|
|
|
$this->cache = $cache; |
71
|
|
|
$this->db = $db; |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
View Code Duplication |
public function loadFromDatabase($lang) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$texts = $lang ? $this->getDb()->table($this->getTableName()) |
79
|
|
|
->where('lang', $lang) |
80
|
|
|
->get(['key', 'value', 'lang', 'scope']) : $this->getDb()->table($this->getTableName())->get(['key', 'value', 'lang', 'scope']); |
81
|
|
|
|
82
|
|
|
$array = []; |
83
|
|
|
foreach ($texts as $row) { |
84
|
|
|
$array[$row->key] = $row->value; |
85
|
|
|
} |
86
|
|
|
return $array; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get a database connection instance. |
91
|
|
|
* |
92
|
|
|
* @return \Illuminate\Database\Connection |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function getDb() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$connection = $this->config->get('db.connection'); |
|
|
|
|
97
|
|
|
if ($connection == 'default') { |
98
|
|
|
return $this->db->connection(); |
99
|
|
|
} |
100
|
|
|
return $this->db->connection($connection); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get a cache driver instance. |
105
|
|
|
* |
106
|
|
|
* @return \Illuminate\Contracts\Cache\Repository |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function getCache() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
if ($this->config->get('cache.enabled', true) === false) { |
|
|
|
|
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
$store = $this->config->get('cache.store', 'default'); |
|
|
|
|
114
|
|
|
if ($store == 'default') { |
115
|
|
|
return $this->cache->store(); |
116
|
|
|
} |
117
|
|
|
return $this->cache->store($store); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
public function save($texts) |
122
|
|
|
{ |
123
|
|
|
if (empty($texts)) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$table = $this->getTableName(); |
128
|
|
|
$locales = $this->getLocales(); |
|
|
|
|
129
|
|
|
|
130
|
|
View Code Duplication |
foreach ($texts as $k => $v) { |
|
|
|
|
131
|
|
|
foreach ($locales as $lang => $locale_data) { |
132
|
|
|
$exists = $this->getDb()->table($table)->where([ |
133
|
|
|
'key' => $k, |
134
|
|
|
'lang' => $lang, |
135
|
|
|
])->first(); |
136
|
|
|
|
137
|
|
|
if ($exists) { |
138
|
|
|
continue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->getDb()->table($table)->insert([ |
142
|
|
|
'key' => $k, |
143
|
|
|
'lang' => $lang, |
144
|
|
|
'value' => $v, |
145
|
|
|
]); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
protected function getTableName() |
153
|
|
|
{ |
154
|
|
|
$table = $this->config->get('db.texts_table', 'texts'); |
|
|
|
|
155
|
|
|
return $table; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.