1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Services;
- use App\Models\DistAppearancePublishList;
- use App\Models\SiteAppearanceTemplate;
- use Illuminate\Support\Facades\Storage;
- class TemplateUpdater
- {
- protected static ?string $baseTemplatePath = null;
-
- public static function updateTemplates($dist): string
- {
- if(!$dist->appearance_id)
- {
- return "No appearance found for dist_id: $dist->id.";
- }
-
- $template_dist_id = $dist->id ?? trim(config('liquid.template_dist_id'));
- $template_name = $dist->appearance->folder ?? trim(config('liquid.template_name'));
- if (self::$baseTemplatePath === null) {
- self::$baseTemplatePath = rtrim(config('liquid.template_path'), '/') . '/' .
- trim($template_dist_id). '/' .
- ltrim($template_name, '/');
- }
-
- $publishList = DistAppearancePublishList::where('dist_id', $dist->id)
- ->where('appearance_id', $dist->appearance_id)
- ->first();
- if (!$publishList) {
- return "No publishList templates found in dist_appearance_template for dist_id: $dist?->id, appearance_id: $dist?->appearance_id.";
- }
-
- $updateCode = $publishList->template_update_code;
-
- $templates = SiteAppearanceTemplate::where('dist_id', $dist->id)
- ->where('appearance_id', $dist->appearance_id)
- ->get();
- if ($templates->isEmpty()) {
- return "No templates found in dist_appearance_template for dist_id: $dist->id, appearance_id: $dist->appearance_id.";
- }
-
- foreach ($templates as $template) {
-
-
- $filePath = self::$baseTemplatePath.'/'.formatDirectory($template->file_path).$template->file_name;
-
- formatAndCreateAbsoluteDirectory($filePath);
-
- $contentUtf8 = mb_convert_encoding($template->content, 'UTF-8', 'auto');
-
- file_put_contents($filePath, $contentUtf8);
- }
-
- $publishList->template_local_code = $updateCode;
- $publishList->save();
- return "Templates updated successfully for dist_id: $dist->id and appearance_id: $dist->appearance_id.";
- }
- }
|