From: Vincent Vanwaelscappel Date: Thu, 16 Feb 2023 11:19:30 +0000 (+0100) Subject: wip #5718 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=0f23d5d75437d240d035c8748a17d12b36285949;p=cubist_cms-back.git wip #5718 @0.5 --- diff --git a/src/app/Magic/Traits/CustomDataSource.php b/src/app/Magic/Traits/CustomDataSource.php index 47c6c5e..acfc619 100644 --- a/src/app/Magic/Traits/CustomDataSource.php +++ b/src/app/Magic/Traits/CustomDataSource.php @@ -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()); + } + }