mirror of
https://github.com/nullthoughts/laravel-data-sync.git
synced 2026-03-03 21:00:03 +00:00
Allows for user provided path option
This commit is contained in:
@@ -7,14 +7,16 @@ use Illuminate\Console\Command;
|
|||||||
|
|
||||||
class Sync extends Command
|
class Sync extends Command
|
||||||
{
|
{
|
||||||
protected $signature = 'data:sync';
|
protected $signature = 'data:sync {--path=}';
|
||||||
|
|
||||||
protected $description = 'Update Models with respective sync data files';
|
protected $description = 'Update Models with respective sync data files';
|
||||||
|
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
|
$path = $this->option('path');
|
||||||
|
|
||||||
$this->info('Updating Models with sync data files');
|
$this->info('Updating Models with sync data files');
|
||||||
(new Updater)->run();
|
(new Updater($path))->run();
|
||||||
$this->comment('Data sync completed');
|
$this->comment('Data sync completed');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,12 @@ class Updater
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get files in sync directory
|
* Get files in sync directory
|
||||||
|
*
|
||||||
|
* @param string|null $path
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct($path = null)
|
||||||
{
|
{
|
||||||
$this->files = Storage::disk('sync')->files();
|
$this->files = Storage::files($this->getDirectory($path));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -23,9 +25,11 @@ class Updater
|
|||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
collect($this->files)->each(function($file) {
|
$records = collect($this->files)->map(function($file) {
|
||||||
$this->syncModel($file);
|
return $this->syncModel($file);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return $records;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,12 +43,31 @@ class Updater
|
|||||||
$model = $this->getModel($file);
|
$model = $this->getModel($file);
|
||||||
$records = $this->getRecords($file);
|
$records = $this->getRecords($file);
|
||||||
|
|
||||||
return $records->map(function($record) use ($model) {
|
$records->each(function($record) use ($model) {
|
||||||
$criteria = $this->getCriteria($record);
|
$criteria = $this->getCriteria($record);
|
||||||
$values = $this->getValues($record);
|
$values = $this->getValues($record);
|
||||||
|
|
||||||
return $model::updateOrCreate($criteria, $values);
|
return $model::updateOrCreate($criteria, $values);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return $records;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get sync files directory path
|
||||||
|
*
|
||||||
|
* @param object $record
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getDirectory($path)
|
||||||
|
{
|
||||||
|
$directory = $path ?? config('data-sync.path', base_path('sync'));
|
||||||
|
|
||||||
|
if(!file_exists($directory)) {
|
||||||
|
throw new \Exception("Specified sync file directory does not exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user