]> _ Git - cubist_cms-back.git/commitdiff
wip #5718 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 15 Feb 2023 08:33:58 +0000 (09:33 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 15 Feb 2023 08:33:58 +0000 (09:33 +0100)
composer.json
src/app/Magic/Traits/CustomDataSource.php

index 6d456b7d20d62b53187290c608fcdfc8c8cbef56..6778e0ca29cefb591b00c697b69b3667cd4d9302 100644 (file)
@@ -53,8 +53,7 @@
         "laravel/framework": "^v8.83.27",
         "laravel-lang/lang": "^10.9",
         "laravel-lang/publisher": "^10.3",
-        "spatie/laravel-permission": "^4.4|^5.8",
-        "calebporzio/sushi": "^2.4"
+        "spatie/laravel-permission": "^4.4|^5.8"
     },
     "require-dev": {
         "filp/whoops": "^2.14",
index 1c01e13656433e3fe3ed1faf5a246d4ae9620fd7..9d0581b108d07c6559cbade70173828063295c90 100644 (file)
@@ -2,7 +2,77 @@
 
 namespace Cubist\Backpack\Magic\Traits;
 
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Log;
+
 trait CustomDataSource
 {
+    public static function bootCustomDataSource()
+    {
+        $instance = (new static);
+        if ($instance->shouldRefreshDatabase()) {
+            $instance->refreshDatabase();
+        }
+    }
+
+    /**
+     * @return array[]
+     */
+    public function getData()
+    {
+        return [];
+    }
+
+    public function refreshDatabase()
+    {
+        $hadError = false;
+        foreach (array_chunk($this->getData(), $this->getInsertChunkSize()) ?? [] as $inserts) {
+            if (!empty($inserts)) {
+                try {
+                    static::insert($inserts);
+                } catch (\Exception $e) {
+                    $hadError = true;
+                    Log::error($e);
+                    break;
+                }
+            }
+        }
+        Cache::put($this->getCacheKey() . '_refresh', $hadError ? -1 : time());
+    }
+
+    public function getInsertChunkSize()
+    {
+        return 100;
+    }
+
+    public function shouldRefreshDatabase()
+    {
+        $lastDatabaseRefresh = Cache::get($this->getCacheKey() . '_refresh', -1);
+        return $lastDatabaseRefresh < $this->getDataLastChange();
+    }
+
+    public function getDataLastChange()
+    {
+        $file = $this->getDataLastChangeFile();
+        if ($file === '') {
+            return 0;
+        }
+        if (!file_exists($file)) {
+            touch($file);
+        }
+        return filemtime($file);
+    }
+
+    public function getDataLastChangeFile()
+    {
+        return '';
+    }
+
+
+    public function getCacheKey()
+    {
+        return 'custom_data_source_' . get_class($this);
+    }
+
 
 }