diff --git a/src/CacheStore.php b/src/CacheStore.php index d3dd397..275935a 100644 --- a/src/CacheStore.php +++ b/src/CacheStore.php @@ -51,20 +51,6 @@ public function __construct( $this->cacheName = $cacheName; } - /** - * Flush all setting changes to the backing store. - * - * @param int $userId The ID of the user to save the user settings for. - * - * @return bool Whether the settings were flushed correctly. - */ - protected function flush($userId = -1) - { - parent::flush($userId); - - $this->cache->forget($this->cacheName); - } - /** * Load all settings into the setting store. * diff --git a/src/DatabaseStore.php b/src/DatabaseStore.php index b58a853..f4cf3e8 100644 --- a/src/DatabaseStore.php +++ b/src/DatabaseStore.php @@ -47,20 +47,6 @@ public function __construct( $this->settingValueModel = $settingValueModel; } - /** - * Flush all setting changes to the backing store. - * - * @param int $userId The ID of the user to save the user settings for. - * - * @return bool Whether the settings were flushed correctly. - */ - protected function flush($userId = -1) - { - $this->handleCreatedSettings(); - $this->handleUpdatedSettings(); - $this->handleDeletedSettings(); - } - /** * Load all settings into the setting store. * @@ -68,13 +54,14 @@ protected function flush($userId = -1) */ protected function loadSettings() { - $settings = $this->settingsModel->leftJoin('setting_values', 'setting_values.setting_id', '=', 'settings.id') + $settings = $this->settingsModel->newQuery() + ->leftJoin('setting_values', 'setting_values.setting_id', '=', 'settings.id') ->select([ 'settings.id', 'settings.name', 'settings.package', 'setting_values.value', - 'setting_values.user_id' + 'setting_values.user_id', ]); if (($user = $this->guard->user()) !== null && $user->getAuthIdentifier() > 0) { @@ -89,118 +76,13 @@ protected function loadSettings() $settingType = ($setting->user_id === null) ? Store::DEFAULT_SETTING_KEY : Store::USER_SETTING_KEY; $this->settings[$setting->package][$setting->name][$settingType] = [ - 'id' => $setting->id, - 'value' => $setting->value, + 'id' => $setting->id, + 'value' => $setting->value, 'package' => $setting->package, - 'name' => $setting->name, + 'name' => $setting->name, ]; } return $this->settings; } - - /** - * Update existing settings with new setting values. - */ - private function handleUpdatedSettings() - { - foreach ($this->modifiedSettings as $id => $setting) { - if (is_numeric($id)) { - $this->settingValueModel->where('setting_id', '=', $id) - ->where('user_id', '=', $setting['user_id']) - ->update(['value' => $setting['value']]); - } else { - if ($setting['id'] != -1) { - $this->settingValueModel->create([ - 'value' => $setting['value'], - 'user_id' => $setting['user_id'], - 'setting_id' => $setting['id'] - ]); - } else { - $foundSetting = $this->settingsModel->where('name', '=', $setting['name']) - ->where('package', '=', $setting['package'])->first(); - - if ($foundSetting != null) { - $foundSetting->values()->create([ - 'value' => $setting['value'], - 'user_id' => $setting['user_id'] - ]); - } - } - } - - unset($this->modifiedSettings[$id]); - } - } - - /** - * Create any new settings that have been created. - */ - private function handleCreatedSettings() - { - foreach ($this->createdSettings[static::DEFAULT_SETTING_KEY] as $key => $createdDefaultSetting) { - $setting = $this->settingsModel->create([ - 'package' => $createdDefaultSetting['package'], - 'name' => $createdDefaultSetting['name'], - ]); - - $setting->values()->create(['value' => $createdDefaultSetting['value']]); - - unset($this->createdSettings[static::DEFAULT_SETTING_KEY][$key]); - } - - if (($user = $this->guard->user()) !== null && $user->getAuthIdentifier() > 0) { - foreach ($this->createdSettings[static::USER_SETTING_KEY] as $key => $createdDefaultSetting) { - $setting = $this->settingsModel->create([ - 'package' => $createdDefaultSetting['package'], - 'name' => $createdDefaultSetting['name'], - ]); - - $setting->values()->create([ - 'value' => $createdDefaultSetting['value'], - 'user_id' => $user->getAuthIdentifier() - ]); - - unset($this->createdSettings[static::USER_SETTING_KEY][$key]); - } - } - } - - /** - * Remove any settings that have been deleted. - */ - private function handleDeletedSettings() - { - foreach ($this->deletedSettings as $key => $setting) { - if ($setting['just_user']) { - if (($user = $this->guard->user()) !== null) { - $this->settingValueModel->join( - 'settings', - 'setting_values.setting_id', - '=', - 'settings.id' - )->where('settings.name', '=', $setting['name'])->where( - 'settings.package', - '=', - $setting['package'] - )->where( - 'setting_values.user_id', - '=', - $user->getAuthIdentifier() - )->delete(); - } - } else { - $settingEntry = $this->settingsModel->where('name', '=', $setting['name'])->where( - 'package', - '=', - $setting['package'] - )->first(); - - $settingEntry->values()->delete(); - $settingEntry->delete(); - } - - unset($this->deletedSettings[$key]); - } - } } diff --git a/src/Facade.php b/src/Facade.php index 0b57674..1d68fbb 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -23,6 +23,6 @@ class Facade extends \Illuminate\Support\Facades\Facade */ protected static function getFacadeAccessor() { - return 'MyBB\Settings\Manager'; + return Manager::class; } } diff --git a/src/Manager.php b/src/Manager.php index 945c8ee..a5dc1e7 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -12,6 +12,11 @@ namespace MyBB\Settings; +use Illuminate\Contracts\Auth\Guard; +use Illuminate\Contracts\Cache\Repository; +use MyBB\Settings\Models\Setting; +use MyBB\Settings\Models\SettingValue; + class Manager extends \Illuminate\Support\Manager { /** @@ -32,9 +37,9 @@ public function getDefaultDriver() public function createDatabaseDriver() { return new DatabaseStore( - $this->app->make('Illuminate\Contracts\Auth\Guard'), - $this->app->make('MyBB\Settings\Models\Setting'), - $this->app->make('MyBB\Settings\Models\SettingValue') + $this->app->make(Guard::class), + $this->app->make(Setting::class), + $this->app->make(SettingValue::class) ); } @@ -45,13 +50,13 @@ public function createDatabaseDriver() */ public function createCacheDriver() { - $cache = $this->app->make('Illuminate\Contracts\Cache\Repository'); + $cache = $this->app->make(Repository::class); $cacheName = $this->app['config']->get('settings.settings_cache_name'); return new CacheStore( - $this->app->make('Illuminate\Contracts\Auth\Guard'), - $this->app->make('MyBB\Settings\Models\Setting'), - $this->app->make('MyBB\Settings\Models\SettingValue'), + $this->app->make(Guard::class), + $this->app->make(Setting::class), + $this->app->make(SettingValue::class), $cache, $cacheName ); diff --git a/src/Middleware/SaveSettingsOnTerminate.php b/src/Middleware/SaveSettingsOnTerminate.php deleted file mode 100644 index 4110eab..0000000 --- a/src/Middleware/SaveSettingsOnTerminate.php +++ /dev/null @@ -1,59 +0,0 @@ -settings = $settings; - } - - /** - * Perform any final actions for the request lifecycle. - * - * @param \Symfony\Component\HttpFoundation\Request $request - * @param \Symfony\Component\HttpFoundation\Response $response - * - * @return void - */ - public function terminate($request, $response) - { - $this->settings->save(); - } - - /** - * Handle an incoming request. - * - * @param \Illuminate\Http\Request $request - * @param \Closure $next - * - * @return mixed - */ - public function handle($request, Closure $next) - { - return $next($request); - } -} diff --git a/src/Models/Setting.php b/src/Models/Setting.php index e962af6..90b20ff 100644 --- a/src/Models/Setting.php +++ b/src/Models/Setting.php @@ -54,6 +54,6 @@ class Setting extends Model public function values() { - return $this->hasMany('MyBB\Settings\Models\SettingValue'); + return $this->hasMany(SettingValue::class); } } diff --git a/src/Models/SettingValue.php b/src/Models/SettingValue.php index 3389dfb..ca6af69 100644 --- a/src/Models/SettingValue.php +++ b/src/Models/SettingValue.php @@ -54,6 +54,6 @@ class SettingValue extends Model public function setting() { - return $this->belongsTo('MyBB\Settings\Models\Setting'); + return $this->belongsTo(Setting::class); } } diff --git a/src/Repositories/Eloquent/SettingRepository.php b/src/Repositories/Eloquent/SettingRepository.php new file mode 100644 index 0000000..a2dbd1a --- /dev/null +++ b/src/Repositories/Eloquent/SettingRepository.php @@ -0,0 +1,169 @@ +settingsModel = $settingsModel; + $this->settingValuesModel = $settingValuesModel; + } + + /** + * Update a setting. + * + * @param array $name An array of setting/value to update. Eg: ['bbname' => 'MyBB Community']. + * @param integer $userId The user to set the setting value for. Defaults to none (-1) to update a board setting. + * @param string $package The package the setting belongs to. + * + * @return void + */ + public function update(array $name, $userId = SettingRepositoryInterface::USER_NONE, $package = 'mybb/core') + { + $user = $userId = (int)$userId; + $package = (string)$package; + + if ($userId === static::USER_NONE) { + $user = null; + } + + if (empty($package)) { + $package = 'mybb/core'; + } + + foreach ($name as $setting => $value) { + $this->settingValuesModel->newQuery() + ->leftJoin('settings', 'settings.id', '=', 'setting_values.setting_id') + ->where('settings.name', $setting) + ->where('setting_values.user_id', $user) + ->where('settings.package', $package) + ->update( + [ + 'value' => $value, + ] + ); + } + } + + /** + * Create a new setting. + * + * @param string|array $name The name, or an array of names, of the setting(s) to create. + * @param string $package The package the setting belongs to. + * + * @return void + */ + public function create($name, $package = 'mybb/core') + { + if (!is_array($name)) { + $name = [(string)$name]; + } + + if (empty($package)) { + $package = 'mybb/core'; + } + + $insertArray = []; + + foreach ($name as $setting) { + $insertArray[] = [ + 'name' => $setting, + 'package' => $package, + ]; + } + + $this->settingsModel->newQuery()->insert($insertArray); + } + + /** + * Delete a setting. + * + * @param string|array $name The name, or an array of names, of the setting(s) to delete. + * @param integer $userId The user to delete the setting value for. Defaults to none (-1) to delete a setting + * completely. + * @param string $package The package the setting belongs to. + * + * @return boolean Whether the setting was deleted. + */ + public function delete($name, $userId = SettingRepositoryInterface::USER_NONE, $package = 'mybb/core') + { + $userId = (int)$userId; + $package = (string)$package; + + if (!is_array($name)) { + $name = [(string)$name]; + } + + if (empty($package)) { + $package = 'mybb/core'; + } + + if ($userId === static::USER_NONE) { + return $this->settingsModel->newQuery()->whereIn('name', $name)->where('package', $package)->delete(); + } else { + return $this->settingValuesModel->newQuery() + ->leftJoin('settings', 'settings.id', '=', 'setting_values.setting_id') + ->where('settings.name', $name) + ->where('setting_values.user_id', $userId) + ->where('settings.package', $package) + ->delete(); + } + } + + /** + * Check if a setting exists. + * + * @param string $name The name of the setting to check. + * @param integer $userId The user to check if the setting exists for. Defaults to none (-1) to check if a setting + * exists at all. + * @param string $package The package the setting belongs to. + * + * @return boolean Whether the setting exists. + */ + public function exists($name, $userId = SettingRepositoryInterface::USER_NONE, $package = 'mybb/core') + { + $user = $userId = (int)$userId; + + if ($userId === static::USER_NONE) { + $user = null; + } + + $count = $this->settingValuesModel->newQuery() + ->leftJoin('settings', 'settings.id', '=', 'setting_values.setting_id') + ->where('settings.name', $name) + ->where('setting_values.user_id', $user) + ->where('settings.package', $package) + ->count(); + + return ($count > 0); + } +} diff --git a/src/Repositories/SettingRepositoryInterface.php b/src/Repositories/SettingRepositoryInterface.php new file mode 100644 index 0000000..cbf2f53 --- /dev/null +++ b/src/Repositories/SettingRepositoryInterface.php @@ -0,0 +1,63 @@ + 'MyBB Community']. + * @param integer $userId The user to set the setting value for. Defaults to none (-1) to update a board setting. + * @param string $package The package the setting belongs to. + * + * @return void + */ + public function update(array $name, $userId = SettingRepositoryInterface::USER_NONE, $package = 'mybb/core'); + + /** + * Create a new setting. + * + * @param string|array $name The name, or an array of names, of the setting(s) to create. + * @param string $package The package the setting belongs to. + * + * @return void + */ + public function create($name, $package = 'mybb/core'); + + /** + * Delete a setting. + * + * @param string|array $name The name, or an array of names, of the setting(s) to delete. + * @param integer $userId The user to delete the setting value for. Defaults to none (-1) to delete a setting + * completely. + * @param string $package The package the setting belongs to. + * + * @return boolean Whether the setting was deleted. + */ + public function delete($name, $userId = SettingRepositoryInterface::USER_NONE, $package = 'mybb/core'); + + /** + * Check if a setting exists. + * + * @param string $name The name of the setting to check. + * @param integer $userId The user to check if the setting exists for. Defaults to none (-1) to check if a setting + * exists at all. + * @param string $package The package the setting belongs to. + * + * @return boolean Whether the setting exists. + */ + public function exists($name, $userId = SettingRepositoryInterface::USER_NONE, $package = 'mybb/core'); +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 979bb4e..2ccce31 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -13,6 +13,8 @@ namespace MyBB\Settings; use Illuminate\Contracts\Foundation\Application; +use MyBB\Settings\Repositories\Eloquent\SettingRepository; +use MyBB\Settings\Repositories\SettingRepositoryInterface; class ServiceProvider extends \Illuminate\Support\ServiceProvider { @@ -41,15 +43,17 @@ public function boot() */ public function register() { - $this->app->bindShared('MyBB\Settings\Manager', function (Application $app) { + $this->app->singleton(Manager::class, function (Application $app) { return new Manager($app); }); - $this->app->bind('MyBB\Settings\Store', function (Application $app) { - return $app->make('MyBB\Settings\Manager')->driver(); + $this->app->bind(Store::class, function (Application $app) { + return $app->make(Manager::class)->driver(); }); + $this->app->bind(SettingRepositoryInterface::class, SettingRepository::class); + $this->mergeConfigFrom(__DIR__ . '/config/settings.php', 'settings'); } @@ -61,8 +65,9 @@ public function register() public function provides() { return [ - 'MyBB\Settings\Manager', - 'MyBB\Settings\Store', + Manager::class, + Store::class, + SettingRepositoryInterface::class, ]; } } diff --git a/src/Store.php b/src/Store.php index 8e43bb5..e98e1af 100644 --- a/src/Store.php +++ b/src/Store.php @@ -59,14 +59,8 @@ abstract class Store */ protected $createdSettings = [ self::DEFAULT_SETTING_KEY => [], - self::USER_SETTING_KEY => [], + self::USER_SETTING_KEY => [], ]; - /** - * A list of deleted settings. - * - * @var array - */ - protected $deletedSettings = []; /** * @param Guard $guard Laravel guard instance, used to get user settings. @@ -147,100 +141,6 @@ private function determineValue($value, $defaultValue) return $value; } - /** - * Set a setting value. - * - * @param string $key The name of the setting. - * @param mixed $value The value for the setting. - * @param bool $useUserSettings Whether to set the setting as a user setting. Defaults to false. - * - * @param string $package The name of the package the setting belongs to. Defaults to 'mybb/core'. - * - * @return void - */ - public function set($key, $value, $useUserSettings = false, $package = 'mybb/core') - { - $this->assertLoaded(); - - if (!is_array($key) && $value === null) { - $this->delete($key, $useUserSettings, $package); - - return; - } - - if (is_array($key)) { - foreach ($key as $k => $v) { - $settingKey = $k; - $settingVal = $v; - - if (is_array($value) && isset($value[$k])) { - $settingKey = $v; - $settingVal = $value[$k]; - - } - - $this->set($settingKey, $settingVal, $useUserSettings, $package); - } - } else { - $settingType = ($useUserSettings === true) ? static::USER_SETTING_KEY : static::DEFAULT_SETTING_KEY; - - // Updating setting or adding user/default value to existing setting - if (isset($this->settings[$package][$key])) { - if (!isset($this->settings[$package][$key][$settingType])) { - $this->modified = true; - - $existingSettingType = - ($settingType == static::USER_SETTING_KEY) - ? static::DEFAULT_SETTING_KEY - : static::USER_SETTING_KEY; - - $id = -1; - - if (isset($this->settings[$package][$key][$existingSettingType]['id'])) { - $id = $this->settings[$package][$key][$existingSettingType]['id']; - } - - $setting = $this->settings[$package][$key][$settingType] = [ - 'id' => $id, - 'package' => $package, - 'name' => $key, - 'value' => $value, - 'user_id' => null, - ]; - - if ($useUserSettings && ($user = $this->guard->user()) !== null) { - $setting['user_id'] = $user->getAuthIdentifier(); - } - - $this->modifiedSettings[$package . '.' . $key . '-' . $settingType] = $setting; - } else { - if ($this->settings[$package][$key][$settingType]['value'] != $value) { - $this->modified = true; - $this->settings[$package][$key][$settingType]['value'] = $value; - - $setting = $this->settings[$package][$key][$settingType]; - $setting['user_id'] = null; - - if ($useUserSettings && ($user = $this->guard->user()) !== null) { - $setting['user_id'] = $user->getAuthIdentifier(); - } - - $this->modifiedSettings[$this->settings[$package][$key][$settingType]['id']] = $setting; - } - } - } else { // Creating setting - $this->modified = true; - $setting = $this->settings[$package][$key][$settingType] = [ - 'package' => $package, - 'name' => $key, - 'value' => $value, - ]; - - $this->createdSettings[$settingType][$package . '.' . $key] = $setting; - } - } - } - /** * Check if a setting exists. * @@ -256,61 +156,6 @@ public function has($key, $package = 'mybb/core') return isset($this->settings[$package][$key]); } - /** - * Delete a setting by key. - * - * @param string $key The key of the setting to delete. - * @param bool $dropJustUserSetting Whether to only delete the user setting if one exists. - * Default behaviour is to delete the setting and all values. - * @param string $package The name of the package to delete the setting for. Defaults to 'mybb/core'. - */ - public function delete($key, $dropJustUserSetting = false, $package = 'mybb/core') - { - $this->assertLoaded(); - - if ($this->has($key, $package)) { - $this->modified = true; - - $this->deletedSettings[] = [ - 'package' => $package, - 'name' => $key, - 'just_user' => (bool)$dropJustUserSetting, - ]; - - unset($this->settings[$package][$key]); - } - } - - /** - * Save any changes to the settings. - * - * @return bool Whether the settings were saved correctly. - */ - public function save() - { - if ($this->modified) { - $user = $this->guard->user(); - $userId = -1; - - if ($user !== null) { - $userId = $user->getAuthIdentifier(); - } - - return $this->flush($userId); - } - - return false; - } - - /** - * Flush all setting changes to the backing store. - * - * @param int $userId The ID of the user to save the user settings for. - * - * @return bool Whether the settings were flushed correctly. - */ - abstract protected function flush($userId = -1); - /** * Get all settings. *