trait CustomDataSource
{
- protected $_data = null;
+ protected static $_data = null;
public static function bootCustomDataSource()
{
}
}
-
/**
* @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];
}
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;
}
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());
+ }
+
}