Files
my-projects-website/app/application/app/Http/Middleware/ProjectLanguage.php
Leonid Nikitin 224240708d The cache was not cleared when saving
Clearing the cache using tags did not work correctly. Had to abandon them.
2025-08-22 16:14:22 +05:00

44 lines
1.7 KiB
PHP

<?php declare(strict_types=1);
namespace App\Http\Middleware;
use App\Models\Project;
use App\Repositories\ProjectLanguageRepository;
use App\Repositories\ProjectRepository;
use App\Repositories\ProjectTranslationRepository;
use App\Services\WebsiteTranslations;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
abstract class ProjectLanguage
{
public function __construct(
protected readonly ProjectRepository $projectRepository,
private readonly ProjectLanguageRepository $projectLanguageRepository,
private readonly ProjectTranslationRepository $projectTranslationRepository
) { }
protected function getWebsiteTranslations(Project $project, ?string $languageCode): ?WebsiteTranslations
{
$seconds = 3600 * 3;
$language = Cache::remember(self::class . $project->id . '-' . $languageCode, $seconds, function () use ($project, $languageCode) {
return $this->projectLanguageRepository->getProjectLanguageByCodeOrDefault($project, $languageCode) ?? false;
});
if ($language === false) {
return null;
}
if ($language !== null) {
if ($language->system_lang) {
App::setLocale($language->system_lang->getLocale());
}
}
$seconds = 3600 * 24;
$translations = Cache::remember(self::class . '-translations-' . $project->id . '-' . $language->id, $seconds, function () use ($project, $language) {
return $this->projectTranslationRepository->getProjectTranslations($project->id, $language->id)->all()->pluck('text', 'code')->toArray();
});
return new WebsiteTranslations($language, $translations);
}
}