refactor itemCollection class

This commit is contained in:
Rev 2021-10-25 21:02:05 +09:00
parent 6234808d4f
commit 3a5849d91a

View File

@ -4,42 +4,43 @@ namespace App\Data;
use App\Exceptions\ItemNotFoundException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use App\Config\GiteaConfig;
class ItemsCollection
{
protected Collection $items;
protected Collection $locales;
protected array $items;
protected array $locales;
private string $items_cache_key = 'items';
private string $locales_cache_key = 'locales';
private string $items_file_key = 'items.json';
private string $locales_file_key = 'locales.json';
public function __construct()
{
if (!Cache::has($this->items_cache_key)) {
if (!File::exists(storage_path($this->items_file_key))) {
$this->refreshItemsCache();
} else {
$this->items = Cache::get($this->items_cache_key);
$content = file_get_contents(storage_path($this->items_file_key));
$this->items = json_decode($content, true);
}
if (!Cache::has($this->locales_cache_key)) {
if (!File::exists(storage_path($this->locales_file_key ))) {
$this->refreshLocalesCache();
} else {
$this->locales = Cache::get($this->locales_cache_key);
$content = file_get_contents(storage_path($this->locales_file_key ));
$this->locales = json_decode($content, true);
}
}
public function refreshLocalesCache(): void
{
$this->locales = collect();
$this->locales = [];
$rawLocalesGlobalBaseUrl = GiteaConfig::RAW_LOCALES_GLOBAL_BASE_URL;
// Getting all locales in project/assets/database/locales/global from the Server development branch repository
$localesList = collect(Http::withOptions(['verify' => false])->get(GiteaConfig::LOCALES_GLOBAL_URL)->json());
$localesList = Http::withOptions(['verify' => false])->get(GiteaConfig::LOCALES_GLOBAL_URL)->json();
foreach ($localesList as $item) {
// Extract the json name for the locale
preg_match('/([a-z]{2}(-[a-z]{2})?).json/', $item['name'], $currentLocaleName, PREG_OFFSET_CAPTURE);
@ -49,12 +50,12 @@ class ItemsCollection
$trimmedCurrentLocaleName = trim($currentLocaleName[1][0]);
$currentLocaleJson = Http::withOptions(['verify' => false])
->get("${rawLocalesGlobalBaseUrl}/${trimmedCurrentLocaleName}.json")->json();
$templateLocale = collect($currentLocaleJson['templates']);
$customizationLocale = collect($currentLocaleJson['customization']);
$this->locales = $this->locales->merge([$trimmedCurrentLocaleName => $templateLocale->concat($customizationLocale)]);
->get("$rawLocalesGlobalBaseUrl/$trimmedCurrentLocaleName.json")->json();
$templateLocale = $currentLocaleJson['templates'];
$customizationLocale = $currentLocaleJson['customization'];
$this->locales[$trimmedCurrentLocaleName] = array_merge($templateLocale, $customizationLocale);
}
Cache::put($this->locales_cache_key, $this->locales);
file_put_contents(storage_path($this->locales_file_key ), json_encode($this->locales));
}
/**
@ -62,10 +63,10 @@ class ItemsCollection
*/
public function refreshItemsCache(): void
{
$this->items = collect(Http::withOptions(['verify' => false])->get(GiteaConfig::RAW_ITEMS_URL)->json());
$this->items = $this->items->merge(collect(Http::withOptions(['verify' => false])
->get(GiteaConfig::RAW_CUSTOMIZATION_URL)->json()));
Cache::put($this->items_cache_key, $this->items);
$items = Http::withOptions(['verify' => false])->get(GiteaConfig::RAW_ITEMS_URL)->json();
$customization = Http::withOptions(['verify' => false])->get(GiteaConfig::RAW_CUSTOMIZATION_URL)->json();
$this->items = array_merge($items, $customization);
file_put_contents(storage_path($this->items_file_key), json_encode($this->items));
}
/**
@ -106,7 +107,8 @@ class ItemsCollection
*/
public function findItem(string $query, string $locale): Collection
{
return $this->items->filter(function ($val, $key) use ($query, $locale) {
$items = collect($this->items);
return $items->filter(function ($val, $key) use ($query, $locale) {
return $this->contains($val['_id'], $query)
|| $this->contains($val['_name'], $query)
|| $this->contains($val['_parent'], $query)
@ -139,13 +141,14 @@ class ItemsCollection
];
}
public function getHierarchy(string $id, string $locale = 'en'): Collection {
public function getHierarchy(string $id, string $locale = 'en'): Collection
{
// Return 404 if the item does not exist
$itemData = $this->items[$id] ?? throw new ItemNotFoundException('Item not found');
// Initialize the hierarchy with the current item
$item = [
'item'=> [
'item' => [
'_id' => $itemData['_id'],
'_name' => $itemData['_name'],
'_parent' => $itemData['_parent']
@ -159,7 +162,7 @@ class ItemsCollection
$itemtId = $item['item']['_parent'];
$itemData = $this->items[$itemtId] ?? null;
$item = [
'item'=> [
'item' => [
'_id' => $itemData['_id'],
'_name' => $itemData['_name'],
'_parent' => $itemData['_parent']