]> _ Git - cubist_cms-back.git/commitdiff
wip #5718 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 16 Feb 2023 11:19:30 +0000 (12:19 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 16 Feb 2023 11:19:30 +0000 (12:19 +0100)
src/app/Magic/Traits/CustomDataSource.php

index 47c6c5ef925514733a9c5b1da3803582c7d9c6d4..acfc6196b6b7322661fdd32c6193733dde077f96 100644 (file)
@@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Log;
 
 trait CustomDataSource
 {
-    protected $_data = null;
+    protected static $_data = null;
 
     public static function bootCustomDataSource()
     {
@@ -17,39 +17,39 @@ trait CustomDataSource
         }
     }
 
-
     /**
      * @return array[]
      */
-    public function getData()
+    public static function getData($force=false)
     {
-        if ($this->_data === null) {
-            $this->_data = $this->_getData();
+        if ($force || static::$_data === null) {
+            static::$_data = static::_getData();
         }
-        return $this->_data;
+        return static::$_data;
     }
 
     /**
      * @return array[]
      */
-    protected function _getData()
+    protected static function _getData()
     {
         return [];
     }
 
-    public function refreshDatabase()
+    public static function refreshDatabase($force=false)
     {
         $hadError = false;
 
+        $instance = (new static);
 
         $listAllInDb = static::all();
-        $pk = $this->getPrimaryKey();
+        $pk = $instance->getPrimaryKey();
         $existingKeys = [];
         foreach ($listAllInDb as $item) {
             $existingKeys[] = $item->{$pk};
         }
         $keys = [];
-        $rows = $this->getData();
+        $rows = static::getData($force);
         foreach ($rows as $row) {
             $keys[] = $row[$pk];
         }
@@ -69,25 +69,24 @@ trait CustomDataSource
             Log::error($e);
         }
 
-        static::whereIn($pk,$toDelete)->delete();
-
-        Cache::put($this->getCacheKey() . '_refresh', $hadError ? -1 : time());
+        static::whereIn($pk, $toDelete)->delete();
+        Cache::put(static::getCacheKey() . '_refresh', $hadError ? -1 : time());
     }
 
-    public function getInsertChunkSize()
+    public static function getInsertChunkSize()
     {
         return 100;
     }
 
-    public function shouldRefreshDatabase()
+    public static function shouldRefreshDatabase()
     {
-        $lastDatabaseRefresh = Cache::get($this->getCacheKey() . '_refresh', -1);
-        return $lastDatabaseRefresh < $this->getDataLastChange();
+        $lastDatabaseRefresh = Cache::get(static::getCacheKey() . '_refresh', -1);
+        return $lastDatabaseRefresh < static::getDataLastChange();
     }
 
-    public function getDataLastChange()
+    public static function getDataLastChange()
     {
-        $file = $this->getDataLastChangeFile();
+        $file = static::getDataLastChangeFile();
         if ($file === '') {
             return 0;
         }
@@ -97,16 +96,21 @@ trait CustomDataSource
         return filemtime($file);
     }
 
-    public function getDataLastChangeFile()
+    public static function getDataLastChangeFile()
     {
         return '';
     }
 
 
-    public function getCacheKey()
+    public static function getCacheKey()
     {
-        return 'custom_data_source_' . get_class($this);
+        return 'custom_data_source_' . static::class;
     }
 
 
+    public static function touchChangeFile()
+    {
+        touch(static::getDataLastChangeFile());
+    }
+
 }