pwweb /
localisation
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * PWWEB\Localisation. |
||||
| 5 | * |
||||
| 6 | * Localisation Registrar. |
||||
| 7 | * |
||||
| 8 | * @author Frank Pillukeit <[email protected]> |
||||
| 9 | * @copyright 2020 pw-websolutions.com |
||||
| 10 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||||
| 11 | */ |
||||
| 12 | |||||
| 13 | namespace PWWEB\Localisation; |
||||
| 14 | |||||
| 15 | use Illuminate\Cache\CacheManager; |
||||
| 16 | use Illuminate\Contracts\Cache\Repository; |
||||
| 17 | use Illuminate\Support\Collection; |
||||
| 18 | use PWWEB\Localisation\Contracts\Address as AddressContract; |
||||
| 19 | use PWWEB\Localisation\Contracts\Address\Type as AddressTypeContract; |
||||
| 20 | use PWWEB\Localisation\Contracts\Country as CountryContract; |
||||
| 21 | use PWWEB\Localisation\Contracts\Currency as CurrencyContract; |
||||
| 22 | use PWWEB\Localisation\Contracts\Language as LanguageContract; |
||||
| 23 | |||||
| 24 | class LocalisationRegistrar |
||||
| 25 | { |
||||
| 26 | /** |
||||
| 27 | * The cache repository. |
||||
| 28 | * |
||||
| 29 | * @var Repository |
||||
| 30 | */ |
||||
| 31 | protected $cache; |
||||
| 32 | |||||
| 33 | /** |
||||
| 34 | * The cache manager object. |
||||
| 35 | * |
||||
| 36 | * @var \Illuminate\Cache\CacheManager |
||||
| 37 | */ |
||||
| 38 | protected $cacheManager; |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * The address class used for the package. |
||||
| 42 | * Can be either original value or overwritten for custom use. |
||||
| 43 | * |
||||
| 44 | * @var string |
||||
| 45 | */ |
||||
| 46 | protected $addressClass; |
||||
| 47 | |||||
| 48 | /** |
||||
| 49 | * The address type class used for the package. |
||||
| 50 | * Can be either original value or overwritten for custom use. |
||||
| 51 | * |
||||
| 52 | * @var string |
||||
| 53 | */ |
||||
| 54 | protected $addressTypeClass; |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * The country class used for the package. |
||||
| 58 | * Can be either original value or overwritten for custom use. |
||||
| 59 | * |
||||
| 60 | * @var string |
||||
| 61 | */ |
||||
| 62 | protected $countryClass; |
||||
| 63 | |||||
| 64 | /** |
||||
| 65 | * The currency class used for the package. |
||||
| 66 | * Can be either original value or overwritten for custom use. |
||||
| 67 | * |
||||
| 68 | * @var string |
||||
| 69 | */ |
||||
| 70 | protected $currencyClass; |
||||
| 71 | |||||
| 72 | /** |
||||
| 73 | * The language class used for the package. |
||||
| 74 | * Can be either original value or overwritten for custom use. |
||||
| 75 | * |
||||
| 76 | * @var string |
||||
| 77 | */ |
||||
| 78 | protected $languageClass; |
||||
| 79 | |||||
| 80 | /** |
||||
| 81 | * The set of addresses available in the cache. |
||||
| 82 | * |
||||
| 83 | * @var \Illuminate\Support\Collection |
||||
| 84 | */ |
||||
| 85 | protected $addresses; |
||||
| 86 | |||||
| 87 | /** |
||||
| 88 | * The set of languages available in the cache. |
||||
| 89 | * |
||||
| 90 | * @var \Illuminate\Support\Collection |
||||
| 91 | */ |
||||
| 92 | protected $languages; |
||||
| 93 | |||||
| 94 | /** |
||||
| 95 | * The cache expiration time. |
||||
| 96 | * |
||||
| 97 | * @var \DateInterval|int |
||||
| 98 | */ |
||||
| 99 | public static $cacheExpirationTime; |
||||
| 100 | |||||
| 101 | /** |
||||
| 102 | * The cache key. |
||||
| 103 | * |
||||
| 104 | * @var string |
||||
| 105 | */ |
||||
| 106 | public static $cacheKey; |
||||
| 107 | |||||
| 108 | /** |
||||
| 109 | * The cache model key. |
||||
| 110 | * |
||||
| 111 | * @var string |
||||
| 112 | */ |
||||
| 113 | public static $cacheModelKey; |
||||
| 114 | |||||
| 115 | /** |
||||
| 116 | * PermissionRegistrar constructor. |
||||
| 117 | * |
||||
| 118 | * @param \Illuminate\Cache\CacheManager $cacheManager The cache manager object |
||||
| 119 | */ |
||||
| 120 | public function __construct(CacheManager $cacheManager) |
||||
| 121 | { |
||||
| 122 | $this->addressClass = config('pwweb.localisation.models.address'); |
||||
| 123 | $this->addressTypeClass = config('pwweb.localisation.models.address_type'); |
||||
| 124 | $this->countryClass = config('pwweb.localisation.models.country'); |
||||
| 125 | $this->currencyClass = config('pwweb.localisation.models.currency'); |
||||
| 126 | $this->languageClass = config('pwweb.localisation.models.language'); |
||||
| 127 | |||||
| 128 | $this->cacheManager = $cacheManager; |
||||
| 129 | $this->initializeCache(); |
||||
| 130 | } |
||||
| 131 | |||||
| 132 | /** |
||||
| 133 | * Initialize the cache for the package. |
||||
| 134 | * |
||||
| 135 | * @return void |
||||
| 136 | */ |
||||
| 137 | protected function initializeCache() |
||||
| 138 | { |
||||
| 139 | self::$cacheExpirationTime = config('pwweb.localisation.cache.expiration_time', config('pwweb.localisation.cache_expiration_time')); |
||||
| 140 | |||||
| 141 | self::$cacheKey = config('pwweb.localisation.cache.key'); |
||||
| 142 | self::$cacheModelKey = config('pwweb.localisation.cache.model_key'); |
||||
| 143 | |||||
| 144 | $this->cache = $this->getCacheStoreFromConfig(); |
||||
| 145 | } |
||||
| 146 | |||||
| 147 | /** |
||||
| 148 | * Retrieve the cache store from the configuration of the package. |
||||
| 149 | * |
||||
| 150 | * @return Repository Cache store |
||||
| 151 | */ |
||||
| 152 | protected function getCacheStoreFromConfig(): Repository |
||||
| 153 | { |
||||
| 154 | // the 'default' fallback here is from the localisation.php config file, where 'default' means to use config(cache.default) |
||||
| 155 | $cacheDriver = config('pwweb.localisation.cache.store', 'default'); |
||||
| 156 | |||||
| 157 | // when 'default' is specified, no action is required since we already have the default instance |
||||
| 158 | if ('default' === $cacheDriver) { |
||||
| 159 | return $this->cacheManager->store(); |
||||
| 160 | } |
||||
| 161 | |||||
| 162 | // if an undefined cache store is specified, fallback to 'array' which is Laravel's closest equiv to 'none' |
||||
| 163 | if (false === \array_key_exists($cacheDriver, config('cache.stores'))) { |
||||
| 164 | $cacheDriver = 'array'; |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | return $this->cacheManager->store($cacheDriver); |
||||
| 168 | } |
||||
| 169 | |||||
| 170 | /** |
||||
| 171 | * Register the languages check method. |
||||
| 172 | * |
||||
| 173 | * @return bool |
||||
| 174 | */ |
||||
| 175 | public function registerLanguages(): bool |
||||
| 176 | { |
||||
| 177 | return true; |
||||
| 178 | } |
||||
| 179 | |||||
| 180 | /** |
||||
| 181 | * Flush the cache. |
||||
| 182 | * |
||||
| 183 | * @return bool |
||||
| 184 | */ |
||||
| 185 | public function forgetCachedAddresses() |
||||
| 186 | { |
||||
| 187 | $this->addresses = null; |
||||
| 188 | |||||
| 189 | return $this->cache->forget(self::$cacheKey); |
||||
| 190 | } |
||||
| 191 | |||||
| 192 | /** |
||||
| 193 | * Flush the cache. |
||||
| 194 | * |
||||
| 195 | * @return bool |
||||
| 196 | */ |
||||
| 197 | public function forgetCachedLanguages() |
||||
| 198 | { |
||||
| 199 | $this->languages = null; |
||||
| 200 | |||||
| 201 | return $this->cache->forget(self::$cacheKey); |
||||
| 202 | } |
||||
| 203 | |||||
| 204 | /** |
||||
| 205 | * Clear class languages. |
||||
| 206 | * This is only intended to be called by the LocalisationServiceProvider on boot, |
||||
| 207 | * so that long-running instances like Swoole don't keep old data in memory. |
||||
| 208 | * |
||||
| 209 | * @return void |
||||
| 210 | */ |
||||
| 211 | public function clearClassLanguages() |
||||
| 212 | { |
||||
| 213 | $this->languages = null; |
||||
| 214 | } |
||||
| 215 | |||||
| 216 | /** |
||||
| 217 | * Get the addresses based on the passed params. |
||||
| 218 | * |
||||
| 219 | * @param array $params additional parameters for query |
||||
| 220 | * |
||||
| 221 | * @return \Illuminate\Support\Collection |
||||
| 222 | */ |
||||
| 223 | public function getAddresses(array $params = []): Collection |
||||
| 224 | { |
||||
| 225 | if (null === $this->addresses) { |
||||
| 226 | $this->addresses = $this->cache->remember( |
||||
| 227 | self::$cacheKey.'.addresses', |
||||
| 228 | self::$cacheExpirationTime, |
||||
| 229 | function () { |
||||
| 230 | return $this->getAddressModel() |
||||
| 231 | ->with('type') |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 232 | ->get(); |
||||
| 233 | } |
||||
| 234 | ); |
||||
| 235 | } |
||||
| 236 | |||||
| 237 | $addresses = clone $this->addresses; |
||||
| 238 | |||||
| 239 | foreach ($params as $attr => $value) { |
||||
| 240 | $attr = 'type' === $attr ? 'type.name' : $attr; |
||||
| 241 | $addresses = $addresses->where($attr, $value); |
||||
| 242 | } |
||||
| 243 | |||||
| 244 | return $addresses; |
||||
| 245 | } |
||||
| 246 | |||||
| 247 | /** |
||||
| 248 | * Get the languages based on the passed params. |
||||
| 249 | * |
||||
| 250 | * @param array $params additional parameters for query |
||||
| 251 | * |
||||
| 252 | * @return \Illuminate\Support\Collection |
||||
| 253 | */ |
||||
| 254 | public function getLanguages(array $params = []): Collection |
||||
| 255 | { |
||||
| 256 | if (null === $this->languages) { |
||||
| 257 | $this->languages = $this->cache->remember( |
||||
| 258 | self::$cacheKey, |
||||
| 259 | self::$cacheExpirationTime, |
||||
| 260 | function () { |
||||
| 261 | return $this->getLanguageClass() |
||||
| 262 | //->with('countries') |
||||
| 263 | ->get(); |
||||
|
0 ignored issues
–
show
The method
get() does not exist on PWWEB\Localisation\Contracts\Language. Since it exists in all sub-types, consider adding an abstract or default implementation to PWWEB\Localisation\Contracts\Language.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 264 | } |
||||
| 265 | ); |
||||
| 266 | } |
||||
| 267 | |||||
| 268 | $languages = clone $this->languages; |
||||
| 269 | |||||
| 270 | foreach ($params as $attr => $value) { |
||||
| 271 | $languages = $languages->where($attr, $value); |
||||
| 272 | } |
||||
| 273 | |||||
| 274 | return $languages; |
||||
| 275 | } |
||||
| 276 | |||||
| 277 | /** |
||||
| 278 | * Get an instance of the address class. |
||||
| 279 | * |
||||
| 280 | * @return \PWWEB\Localisation\Contracts\Address|null |
||||
| 281 | */ |
||||
| 282 | public function getAddressModel(): ?AddressContract |
||||
| 283 | { |
||||
| 284 | $addressModel = app($this->addressClass); |
||||
| 285 | |||||
| 286 | if ($addressModel instanceof AddressContract) { |
||||
| 287 | return $addressModel; |
||||
| 288 | } |
||||
| 289 | |||||
| 290 | return null; |
||||
| 291 | } |
||||
| 292 | |||||
| 293 | /** |
||||
| 294 | * Get an instance of the address type class. |
||||
| 295 | * |
||||
| 296 | * @return \PWWEB\Localisation\Contracts\Address\Type|null |
||||
| 297 | */ |
||||
| 298 | public function getAddressTypeClass(): ?AddressTypeContract |
||||
| 299 | { |
||||
| 300 | $addressTypeModel = app($this->addressTypeClass); |
||||
| 301 | |||||
| 302 | if ($addressTypeModel instanceof AddressTypeContract) { |
||||
| 303 | return $addressTypeModel; |
||||
| 304 | } |
||||
| 305 | |||||
| 306 | return null; |
||||
| 307 | } |
||||
| 308 | |||||
| 309 | /** |
||||
| 310 | * Get an instance of the country class. |
||||
| 311 | * |
||||
| 312 | * @return \PWWEB\Localisation\Contracts\Country|null |
||||
| 313 | */ |
||||
| 314 | public function getCountryClass(): ?CountryContract |
||||
| 315 | { |
||||
| 316 | $countryModel = app($this->countryClass); |
||||
| 317 | |||||
| 318 | if ($countryModel instanceof CountryContract) { |
||||
| 319 | return $countryModel; |
||||
| 320 | } |
||||
| 321 | |||||
| 322 | return null; |
||||
| 323 | } |
||||
| 324 | |||||
| 325 | /** |
||||
| 326 | * Get an instance of the currency class. |
||||
| 327 | * |
||||
| 328 | * @return \PWWEB\Localisation\Contracts\Currency|null |
||||
| 329 | */ |
||||
| 330 | public function getCurrencyClass(): ?CurrencyContract |
||||
| 331 | { |
||||
| 332 | $currencyModel = app($this->currencyClass); |
||||
| 333 | |||||
| 334 | if ($currencyModel instanceof CurrencyContract) { |
||||
| 335 | return $currencyModel; |
||||
| 336 | } |
||||
| 337 | |||||
| 338 | return null; |
||||
| 339 | } |
||||
| 340 | |||||
| 341 | /** |
||||
| 342 | * Get an instance of the language class. |
||||
| 343 | * |
||||
| 344 | * @return \PWWEB\Localisation\Contracts\Language|null |
||||
| 345 | */ |
||||
| 346 | public function getLanguageClass(): ?LanguageContract |
||||
| 347 | { |
||||
| 348 | $languageModel = app($this->languageClass); |
||||
| 349 | |||||
| 350 | if ($languageModel instanceof LanguageContract) { |
||||
| 351 | return $languageModel; |
||||
| 352 | } |
||||
| 353 | |||||
| 354 | return null; |
||||
| 355 | } |
||||
| 356 | |||||
| 357 | /** |
||||
| 358 | * Set the instance of the address class. |
||||
| 359 | * |
||||
| 360 | * @param string $addressClass the address class to be used |
||||
| 361 | * |
||||
| 362 | * @return object |
||||
| 363 | */ |
||||
| 364 | public function setAddressClass(string $addressClass) |
||||
| 365 | { |
||||
| 366 | $this->addressClass = $addressClass; |
||||
| 367 | |||||
| 368 | return $this; |
||||
| 369 | } |
||||
| 370 | |||||
| 371 | /** |
||||
| 372 | * Set the instance of the language class. |
||||
| 373 | * |
||||
| 374 | * @param string $languageClass the language class to be used |
||||
| 375 | * |
||||
| 376 | * @return object |
||||
| 377 | */ |
||||
| 378 | public function setLanguageClass(string $languageClass) |
||||
| 379 | { |
||||
| 380 | $this->languageClass = $languageClass; |
||||
| 381 | |||||
| 382 | return $this; |
||||
| 383 | } |
||||
| 384 | |||||
| 385 | /** |
||||
| 386 | * Get the instance of the Cache Store. |
||||
| 387 | * |
||||
| 388 | * @return \Illuminate\Contracts\Cache\Store |
||||
| 389 | */ |
||||
| 390 | public function getCacheStore(): \Illuminate\Contracts\Cache\Store |
||||
| 391 | { |
||||
| 392 | return $this->cache->getStore(); |
||||
| 393 | } |
||||
| 394 | } |
||||
| 395 |