From a5b2e7e521fd80dd0ee79b134f5523458dc2c180 Mon Sep 17 00:00:00 2001 From: Euan Torano Date: Sun, 6 Sep 2015 11:37:43 +0100 Subject: [PATCH 1/4] Removing setters from Store Creating, updating and deleting settings should instead be handled by the repository. This simplifies the store greatly. The database store can be updated in the future to use the repository too, but for now I'm keeping it simple. Signed-off-by: Euan Torano --- src/CacheStore.php | 14 -- src/DatabaseStore.php | 121 +------------ src/Facade.php | 2 +- src/Manager.php | 19 +- src/Middleware/SaveSettingsOnTerminate.php | 59 ------- src/Models/Setting.php | 2 +- src/Models/SettingValue.php | 2 +- .../Eloquent/SettingRepository.php | 163 ++++++++++++++++++ .../SettingRepositoryInterface.php | 61 +++++++ src/ServiceProvider.php | 15 +- src/Store.php | 155 ----------------- 11 files changed, 250 insertions(+), 363 deletions(-) delete mode 100644 src/Middleware/SaveSettingsOnTerminate.php create mode 100644 src/Repositories/Eloquent/SettingRepository.php create mode 100644 src/Repositories/SettingRepositoryInterface.php 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..20bb8b5 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,7 +54,7 @@ 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', @@ -98,109 +84,4 @@ protected function loadSettings() 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..af989ae --- /dev/null +++ b/src/Repositories/Eloquent/SettingRepository.php @@ -0,0 +1,163 @@ +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); + } +} \ No newline at end of file diff --git a/src/Repositories/SettingRepositoryInterface.php b/src/Repositories/SettingRepositoryInterface.php new file mode 100644 index 0000000..0410694 --- /dev/null +++ b/src/Repositories/SettingRepositoryInterface.php @@ -0,0 +1,61 @@ + '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'); +} \ No newline at end of file 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..362fe84 100644 --- a/src/Store.php +++ b/src/Store.php @@ -61,12 +61,6 @@ abstract class Store self::DEFAULT_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. * From bab90f4e1f6a27c6bbd80bfd90617227c82a62c2 Mon Sep 17 00:00:00 2001 From: Euan Torano Date: Sun, 6 Sep 2015 11:47:15 +0100 Subject: [PATCH 2/4] Reformatting code. Signed-off-by: Euan Torano --- src/CacheStore.php | 8 ++--- src/DatabaseStore.php | 15 ++++---- .../Eloquent/SettingRepository.php | 34 ++++++++++--------- .../SettingRepositoryInterface.php | 20 ++++++----- src/Store.php | 12 +++---- 5 files changed, 47 insertions(+), 42 deletions(-) diff --git a/src/CacheStore.php b/src/CacheStore.php index 275935a..f96cf91 100644 --- a/src/CacheStore.php +++ b/src/CacheStore.php @@ -33,11 +33,11 @@ class CacheStore extends DatabaseStore protected $cacheName; /** - * @param Guard $guard Laravel guard instance, used to get user settings. - * @param Setting $settingsModel Settings model instance. + * @param Guard $guard Laravel guard instance, used to get user settings. + * @param Setting $settingsModel Settings model instance. * @param SettingValue $settingValueModel Setting value model instance. - * @param Repository $cache Cache repository for settings. - * @param string $cacheName The name of the cache to use. + * @param Repository $cache Cache repository for settings. + * @param string $cacheName The name of the cache to use. */ public function __construct( Guard $guard, diff --git a/src/DatabaseStore.php b/src/DatabaseStore.php index 20bb8b5..48017f7 100644 --- a/src/DatabaseStore.php +++ b/src/DatabaseStore.php @@ -32,8 +32,8 @@ class DatabaseStore extends Store private $settingValueModel; /** - * @param Guard $guard Laravel guard instance, used to get user settings. - * @param Setting $settingsModel Settings model instance. + * @param Guard $guard Laravel guard instance, used to get user settings. + * @param Setting $settingsModel Settings model instance. * @param SettingValue $settingValueModel Setting value model instance. */ public function __construct( @@ -54,13 +54,14 @@ public function __construct( */ protected function loadSettings() { - $settings = $this->settingsModel->newQuery()->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) { @@ -75,10 +76,10 @@ 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, ]; } diff --git a/src/Repositories/Eloquent/SettingRepository.php b/src/Repositories/Eloquent/SettingRepository.php index af989ae..cf27833 100644 --- a/src/Repositories/Eloquent/SettingRepository.php +++ b/src/Repositories/Eloquent/SettingRepository.php @@ -37,16 +37,16 @@ public function __construct(Setting $settingsModel, SettingValue $settingValuesM /** * Update a setting. * - * @param array $name An array of setting/value to update. Eg: ['bbname' => 'MyBB Community']. + * @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. + * @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; + $user = $userId = (int)$userId; + $package = (string)$package; if ($userId === static::USER_NONE) { $user = null; @@ -74,14 +74,14 @@ public function update(array $name, $userId = SettingRepositoryInterface::USER_N * 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. + * @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]; + $name = [(string)$name]; } if (empty($package)) { @@ -92,7 +92,7 @@ public function create($name, $package = 'mybb/core') foreach ($name as $setting) { $insertArray[] = [ - 'name' => $setting, + 'name' => $setting, 'package' => $package, ]; } @@ -104,18 +104,19 @@ 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. + * @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; + $userId = (int)$userId; + $package = (string)$package; if (!is_array($name)) { - $name = [(string) $name]; + $name = [(string)$name]; } if (empty($package)) { @@ -137,15 +138,16 @@ public function delete($name, $userId = SettingRepositoryInterface::USER_NONE, $ /** * 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. + * @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; + $user = $userId = (int)$userId; if ($userId === static::USER_NONE) { $user = null; diff --git a/src/Repositories/SettingRepositoryInterface.php b/src/Repositories/SettingRepositoryInterface.php index 0410694..4a1596d 100644 --- a/src/Repositories/SettingRepositoryInterface.php +++ b/src/Repositories/SettingRepositoryInterface.php @@ -19,19 +19,19 @@ interface SettingRepositoryInterface /** * Update a setting. * - * @param array $name An array of setting/value to update. Eg: ['bbname' => 'MyBB Community']. + * @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. + * @param string $package The package the setting belongs to. * * @return void */ - public function update(array $name, $userId = SettingRepositoryInterface::USER_NONE, $package = 'mybb/core'); + 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. + * @param string $package The package the setting belongs to. * * @return void */ @@ -41,8 +41,9 @@ 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. + * @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. */ @@ -51,9 +52,10 @@ public function delete($name, $userId = SettingRepositoryInterface::USER_NONE, $ /** * 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. + * @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. */ diff --git a/src/Store.php b/src/Store.php index 362fe84..2b0407e 100644 --- a/src/Store.php +++ b/src/Store.php @@ -59,7 +59,7 @@ abstract class Store */ protected $createdSettings = [ self::DEFAULT_SETTING_KEY => [], - self::USER_SETTING_KEY => [], + self::USER_SETTING_KEY => [], ]; /** @@ -73,11 +73,11 @@ public function __construct(Guard $guard) /** * Get a setting value. * - * @param string $key The name of the setting. - * @param mixed $defaultValue A default value to use if the setting does not exist. Defaults to null. + * @param string $key The name of the setting. + * @param mixed $defaultValue A default value to use if the setting does not exist. Defaults to null. * @param bool $useUserSettings Whether to take into account user settings. User settings have priority over main * settings. Defaults to true. - * @param string $package The name of the package the setting belongs to. Defaults to 'mybb/core'. + * @param string $package The name of the package the setting belongs to. Defaults to 'mybb/core'. * * @return mixed The value of the setting. */ @@ -122,7 +122,7 @@ abstract protected function loadSettings(); /** * Determine the return value from an actual value and default value. * - * @param mixed $value The actual value. + * @param mixed $value The actual value. * @param mixed $defaultValue The default value. * * @return mixed The determined value. If both $value and $defaultValue are not null, $value will be typecast to @@ -144,7 +144,7 @@ private function determineValue($value, $defaultValue) /** * Check if a setting exists. * - * @param string $key The name of the setting. + * @param string $key The name of the setting. * @param string $package The name of the package the setting belongs to. Defaults to 'mybb/core'. * * @return bool Whether the setting exists. From 6c8159ac7e3c255674b25fbf81f881c75770d5b9 Mon Sep 17 00:00:00 2001 From: Euan Torano Date: Sun, 6 Sep 2015 11:49:02 +0100 Subject: [PATCH 3/4] Further code reformatting. Signed-off-by: Euan Torano --- src/CacheStore.php | 8 ++++---- src/DatabaseStore.php | 4 ++-- .../Eloquent/SettingRepository.php | 18 +++++++++--------- .../SettingRepositoryInterface.php | 18 +++++++++--------- src/Store.php | 10 +++++----- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/CacheStore.php b/src/CacheStore.php index f96cf91..275935a 100644 --- a/src/CacheStore.php +++ b/src/CacheStore.php @@ -33,11 +33,11 @@ class CacheStore extends DatabaseStore protected $cacheName; /** - * @param Guard $guard Laravel guard instance, used to get user settings. - * @param Setting $settingsModel Settings model instance. + * @param Guard $guard Laravel guard instance, used to get user settings. + * @param Setting $settingsModel Settings model instance. * @param SettingValue $settingValueModel Setting value model instance. - * @param Repository $cache Cache repository for settings. - * @param string $cacheName The name of the cache to use. + * @param Repository $cache Cache repository for settings. + * @param string $cacheName The name of the cache to use. */ public function __construct( Guard $guard, diff --git a/src/DatabaseStore.php b/src/DatabaseStore.php index 48017f7..f4cf3e8 100644 --- a/src/DatabaseStore.php +++ b/src/DatabaseStore.php @@ -32,8 +32,8 @@ class DatabaseStore extends Store private $settingValueModel; /** - * @param Guard $guard Laravel guard instance, used to get user settings. - * @param Setting $settingsModel Settings model instance. + * @param Guard $guard Laravel guard instance, used to get user settings. + * @param Setting $settingsModel Settings model instance. * @param SettingValue $settingValueModel Setting value model instance. */ public function __construct( diff --git a/src/Repositories/Eloquent/SettingRepository.php b/src/Repositories/Eloquent/SettingRepository.php index cf27833..508b3f3 100644 --- a/src/Repositories/Eloquent/SettingRepository.php +++ b/src/Repositories/Eloquent/SettingRepository.php @@ -37,8 +37,8 @@ public function __construct(Setting $settingsModel, SettingValue $settingValuesM /** * 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 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 @@ -73,7 +73,7 @@ public function update(array $name, $userId = SettingRepositoryInterface::USER_N /** * Create a new setting. * - * @param string|array $name The name, or an array of names, of the setting(s) to create. + * @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 @@ -103,9 +103,9 @@ 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|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. @@ -138,9 +138,9 @@ public function delete($name, $userId = SettingRepositoryInterface::USER_NONE, $ /** * 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 $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. diff --git a/src/Repositories/SettingRepositoryInterface.php b/src/Repositories/SettingRepositoryInterface.php index 4a1596d..c853b8c 100644 --- a/src/Repositories/SettingRepositoryInterface.php +++ b/src/Repositories/SettingRepositoryInterface.php @@ -19,8 +19,8 @@ interface SettingRepositoryInterface /** * 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 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 @@ -30,7 +30,7 @@ public function update(array $name, $userId = SettingRepositoryInterface::USER_N /** * Create a new setting. * - * @param string|array $name The name, or an array of names, of the setting(s) to create. + * @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 @@ -40,9 +40,9 @@ 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|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. @@ -52,9 +52,9 @@ public function delete($name, $userId = SettingRepositoryInterface::USER_NONE, $ /** * 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 $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. diff --git a/src/Store.php b/src/Store.php index 2b0407e..e98e1af 100644 --- a/src/Store.php +++ b/src/Store.php @@ -73,11 +73,11 @@ public function __construct(Guard $guard) /** * Get a setting value. * - * @param string $key The name of the setting. - * @param mixed $defaultValue A default value to use if the setting does not exist. Defaults to null. + * @param string $key The name of the setting. + * @param mixed $defaultValue A default value to use if the setting does not exist. Defaults to null. * @param bool $useUserSettings Whether to take into account user settings. User settings have priority over main * settings. Defaults to true. - * @param string $package The name of the package the setting belongs to. Defaults to 'mybb/core'. + * @param string $package The name of the package the setting belongs to. Defaults to 'mybb/core'. * * @return mixed The value of the setting. */ @@ -122,7 +122,7 @@ abstract protected function loadSettings(); /** * Determine the return value from an actual value and default value. * - * @param mixed $value The actual value. + * @param mixed $value The actual value. * @param mixed $defaultValue The default value. * * @return mixed The determined value. If both $value and $defaultValue are not null, $value will be typecast to @@ -144,7 +144,7 @@ private function determineValue($value, $defaultValue) /** * Check if a setting exists. * - * @param string $key The name of the setting. + * @param string $key The name of the setting. * @param string $package The name of the package the setting belongs to. Defaults to 'mybb/core'. * * @return bool Whether the setting exists. From 2ca1f002f2a2635337e4383fb60b06873fec6864 Mon Sep 17 00:00:00 2001 From: Euan Torano Date: Sun, 6 Sep 2015 11:52:35 +0100 Subject: [PATCH 4/4] More reformatting. Signed-off-by: Euan Torano --- src/Repositories/Eloquent/SettingRepository.php | 6 +++++- src/Repositories/SettingRepositoryInterface.php | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Repositories/Eloquent/SettingRepository.php b/src/Repositories/Eloquent/SettingRepository.php index 508b3f3..a2dbd1a 100644 --- a/src/Repositories/Eloquent/SettingRepository.php +++ b/src/Repositories/Eloquent/SettingRepository.php @@ -28,6 +28,10 @@ class SettingRepository implements SettingRepositoryInterface */ private $settingValuesModel; + /** + * @param Setting $settingsModel + * @param SettingValue $settingValuesModel + */ public function __construct(Setting $settingsModel, SettingValue $settingValuesModel) { $this->settingsModel = $settingsModel; @@ -162,4 +166,4 @@ public function exists($name, $userId = SettingRepositoryInterface::USER_NONE, $ return ($count > 0); } -} \ No newline at end of file +} diff --git a/src/Repositories/SettingRepositoryInterface.php b/src/Repositories/SettingRepositoryInterface.php index c853b8c..cbf2f53 100644 --- a/src/Repositories/SettingRepositoryInterface.php +++ b/src/Repositories/SettingRepositoryInterface.php @@ -60,4 +60,4 @@ public function delete($name, $userId = SettingRepositoryInterface::USER_NONE, $ * @return boolean Whether the setting exists. */ public function exists($name, $userId = SettingRepositoryInterface::USER_NONE, $package = 'mybb/core'); -} \ No newline at end of file +}