2019-08-05 20:27:53 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\Installer;
|
|
|
|
|
|
|
|
use App\Contracts\Service;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Services\DatabaseService;
|
2020-10-19 22:10:28 +08:00
|
|
|
use Carbon\Carbon;
|
2019-08-05 20:27:53 +08:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2019-11-19 23:54:42 +08:00
|
|
|
use function trim;
|
2019-08-05 20:27:53 +08:00
|
|
|
|
|
|
|
class SeederService extends Service
|
|
|
|
{
|
|
|
|
private $databaseSvc;
|
|
|
|
|
|
|
|
private $counters = [];
|
|
|
|
private $offsets = [];
|
|
|
|
|
2019-08-07 03:54:21 +08:00
|
|
|
// Map an environment to a seeder directory, if we want to share
|
|
|
|
public static $seed_mapper = [];
|
2019-08-05 20:27:53 +08:00
|
|
|
|
|
|
|
public function __construct(DatabaseService $databaseSvc)
|
|
|
|
{
|
|
|
|
$this->databaseSvc = $databaseSvc;
|
|
|
|
}
|
|
|
|
|
2019-11-19 23:54:42 +08:00
|
|
|
/**
|
|
|
|
* See if there are any seeds that are out of sync
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function seedsPending(): bool
|
|
|
|
{
|
|
|
|
if ($this->settingsSeedsPending()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->permissionsSeedsPending()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-19 22:10:28 +08:00
|
|
|
if ($this->moduleSeedsPending()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-19 23:54:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-05 20:27:53 +08:00
|
|
|
/**
|
|
|
|
* Syncronize all of the seed files, run this after the migrations
|
|
|
|
* and on first install.
|
|
|
|
*/
|
|
|
|
public function syncAllSeeds(): void
|
|
|
|
{
|
|
|
|
$this->syncAllYamlFileSeeds();
|
|
|
|
$this->syncAllSettings();
|
|
|
|
$this->syncAllPermissions();
|
2020-10-19 22:10:28 +08:00
|
|
|
$this->syncAllModules();
|
2019-08-05 20:27:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read all of the YAML files from disk and seed them
|
|
|
|
*/
|
|
|
|
public function syncAllYamlFileSeeds(): void
|
|
|
|
{
|
|
|
|
Log::info('Running seeder');
|
|
|
|
$env = App::environment();
|
|
|
|
if (array_key_exists($env, self::$seed_mapper)) {
|
|
|
|
$env = self::$seed_mapper[$env];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather all of the files to seed
|
|
|
|
collect()
|
|
|
|
->concat(Storage::disk('seeds')->files($env))
|
|
|
|
->map(function ($file) {
|
|
|
|
return database_path('seeds/'.$file);
|
|
|
|
})
|
|
|
|
->filter(function ($file) {
|
|
|
|
$info = pathinfo($file);
|
|
|
|
return $info['extension'] === 'yml';
|
|
|
|
})
|
|
|
|
->each(function ($file) {
|
|
|
|
Log::info('Seeding .'.$file);
|
|
|
|
$this->databaseSvc->seed_from_yaml_file($file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-19 22:10:28 +08:00
|
|
|
public function syncAllModules(): void
|
|
|
|
{
|
|
|
|
$data = file_get_contents(database_path('/seeds/modules.yml'));
|
|
|
|
$yml = Yaml::parse($data);
|
|
|
|
foreach ($yml as $module) {
|
|
|
|
$module['updated_at'] = Carbon::now();
|
|
|
|
$count = DB::table('modules')->where('name', $module['name'])->count('name');
|
|
|
|
if ($count === 0) {
|
|
|
|
$module['created_at'] = Carbon::now();
|
|
|
|
DB::table('modules')->insert($module);
|
|
|
|
} else {
|
|
|
|
DB::table('modules')
|
|
|
|
->where('name', $module['name'])
|
|
|
|
->update($module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 20:27:53 +08:00
|
|
|
public function syncAllSettings(): void
|
|
|
|
{
|
|
|
|
$data = file_get_contents(database_path('/seeds/settings.yml'));
|
|
|
|
$yml = Yaml::parse($data);
|
|
|
|
foreach ($yml as $setting) {
|
2019-11-19 23:54:42 +08:00
|
|
|
if (trim($setting['key']) === '') {
|
2019-08-05 20:27:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addSetting($setting['key'], $setting);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function syncAllPermissions(): void
|
|
|
|
{
|
|
|
|
$data = file_get_contents(database_path('/seeds/permissions.yml'));
|
|
|
|
$yml = Yaml::parse($data);
|
|
|
|
foreach ($yml as $perm) {
|
|
|
|
$count = DB::table('permissions')->where('name', $perm['name'])->count('name');
|
|
|
|
if ($count === 0) {
|
|
|
|
DB::table('permissions')->insert($perm);
|
|
|
|
} else {
|
|
|
|
DB::table('permissions')
|
|
|
|
->where('name', $perm['name'])
|
|
|
|
->update($perm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @param $attrs
|
|
|
|
*/
|
|
|
|
public function addSetting($key, $attrs): void
|
|
|
|
{
|
|
|
|
$id = Setting::formatKey($key);
|
|
|
|
$group = $attrs['group'];
|
|
|
|
$order = $this->getNextOrderNumber($group);
|
|
|
|
|
|
|
|
$attrs = array_merge(
|
|
|
|
[
|
|
|
|
'id' => $id,
|
|
|
|
'key' => $key,
|
|
|
|
'offset' => $this->offsets[$group],
|
|
|
|
'order' => $order,
|
|
|
|
'name' => '',
|
|
|
|
'group' => $group,
|
|
|
|
'value' => '',
|
|
|
|
'default' => $attrs['value'],
|
|
|
|
'options' => '',
|
|
|
|
'type' => 'hidden',
|
|
|
|
'description' => '',
|
|
|
|
],
|
|
|
|
$attrs
|
|
|
|
);
|
|
|
|
|
|
|
|
$count = DB::table('settings')->where('id', $id)->count('id');
|
|
|
|
if ($count === 0) {
|
|
|
|
DB::table('settings')->insert($attrs);
|
|
|
|
} else {
|
|
|
|
unset($attrs['value']); // Don't overwrite this
|
|
|
|
DB::table('settings')
|
|
|
|
->where('id', $id)
|
|
|
|
->update($attrs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dynamically figure out the offset and the start number for a group.
|
|
|
|
* This way we don't need to mess with how to order things
|
|
|
|
* When calling getNextOrderNumber(users) 31, will be returned, then 32, and so on
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* @param null $offset
|
|
|
|
* @param int $start_offset
|
|
|
|
*/
|
|
|
|
private function addCounterGroup($name, $offset = null, $start_offset = 0): void
|
|
|
|
{
|
|
|
|
if ($offset === null) {
|
|
|
|
$group = DB::table('settings')
|
|
|
|
->where('group', $name)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($group === null) {
|
|
|
|
$offset = (int) DB::table('settings')->max('offset');
|
|
|
|
if ($offset === null) {
|
|
|
|
$offset = 0;
|
|
|
|
$start_offset = 1;
|
|
|
|
} else {
|
|
|
|
$offset += 100;
|
|
|
|
$start_offset = $offset + 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Now find the number to start from
|
|
|
|
$start_offset = (int) DB::table('settings')->where('group', $name)->max('order');
|
|
|
|
if ($start_offset === null) {
|
|
|
|
$start_offset = $offset + 1;
|
|
|
|
} else {
|
|
|
|
$start_offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset = $group->offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->counters[$name] = $start_offset;
|
|
|
|
$this->offsets[$name] = $offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next increment number from a group
|
|
|
|
*
|
|
|
|
* @param $group
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getNextOrderNumber($group): int
|
|
|
|
{
|
|
|
|
if (!\in_array($group, $this->counters, true)) {
|
|
|
|
$this->addCounterGroup($group);
|
|
|
|
}
|
|
|
|
|
|
|
|
$idx = $this->counters[$group];
|
|
|
|
$this->counters[$group]++;
|
|
|
|
|
|
|
|
return $idx;
|
|
|
|
}
|
2019-11-19 23:54:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See if there are seeds pending for the settings
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function settingsSeedsPending(): bool
|
|
|
|
{
|
2020-01-31 01:44:59 +08:00
|
|
|
$all_settings = DB::table('settings')->get();
|
2019-11-19 23:54:42 +08:00
|
|
|
$data = file_get_contents(database_path('/seeds/settings.yml'));
|
|
|
|
$yml = Yaml::parse($data);
|
|
|
|
|
|
|
|
// See if any are missing from the DB
|
|
|
|
foreach ($yml as $setting) {
|
|
|
|
if (trim($setting['key']) === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = Setting::formatKey($setting['key']);
|
2020-01-31 01:44:59 +08:00
|
|
|
$row = $all_settings->firstWhere('id', $id);
|
2019-11-19 23:54:42 +08:00
|
|
|
|
|
|
|
// Doesn't exist in the table, quit early and say there is stuff pending
|
|
|
|
if (!$row) {
|
|
|
|
Log::info('Setting '.$id.' missing, update available');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
// See if any of these column values have changed
|
|
|
|
foreach (['name', 'description'] as $column) {
|
|
|
|
$currVal = $row->{$column};
|
|
|
|
$newVal = $setting[$column];
|
|
|
|
if ($currVal !== $newVal) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 23:54:42 +08:00
|
|
|
// See if any of the options have changed
|
|
|
|
if ($row->type === 'select') {
|
2020-04-26 23:55:20 +08:00
|
|
|
if (!empty($row->options) && $row->options !== $setting['options']) {
|
2019-11-19 23:54:42 +08:00
|
|
|
Log::info('Options for '.$id.' changed, update available');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if there are seeds pending for the permissions
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function permissionsSeedsPending(): bool
|
|
|
|
{
|
2020-01-31 01:44:59 +08:00
|
|
|
$all_permissions = DB::table('permissions')->get();
|
|
|
|
|
2019-11-19 23:54:42 +08:00
|
|
|
$data = file_get_contents(database_path('/seeds/permissions.yml'));
|
|
|
|
$yml = Yaml::parse($data);
|
|
|
|
|
|
|
|
foreach ($yml as $perm) {
|
2020-01-31 01:44:59 +08:00
|
|
|
$row = $all_permissions->firstWhere('name', $perm['name']);
|
2019-11-27 22:19:20 +08:00
|
|
|
if (!$row) {
|
2019-11-19 23:54:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
// See if any of these column values have changed
|
|
|
|
foreach (['display_name', 'description'] as $column) {
|
|
|
|
if ($row->{$column} !== $perm[$column]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 23:54:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-19 22:10:28 +08:00
|
|
|
|
|
|
|
private function moduleSeedsPending(): bool
|
|
|
|
{
|
|
|
|
$all_modules = DB::table('modules')->get();
|
|
|
|
|
|
|
|
$data = file_get_contents(database_path('/seeds/modules.yml'));
|
|
|
|
$yml = Yaml::parse($data);
|
|
|
|
|
|
|
|
foreach ($yml as $perm) {
|
|
|
|
$row = $all_modules->firstWhere('name', $perm['name']);
|
|
|
|
if (!$row) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if any of these column values have changed
|
|
|
|
foreach (['name', 'enabled'] as $column) {
|
|
|
|
if ($row->{$column} !== $perm[$column]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-05 20:27:53 +08:00
|
|
|
}
|