From 6274078c5018875a0c21d24ada30cb7239e127e7 Mon Sep 17 00:00:00 2001 From: Zach Leigh Date: Mon, 6 Jun 2016 15:48:56 +0900 Subject: [PATCH] Refactor HasSettings. --- README.md | 17 ++++++++---- src/Commands/PublishUserSettings.php | 12 --------- src/Settings/HasSettings.php | 40 ++++++++++++++++------------ 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index b368268..50d88b8 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,17 @@ In Laravel's config/app.php file, add the service provider to the array with the LaravelPropertyBag\ServiceProvider::class ``` -##### Export the config file and migration +##### Publish the migration ``` php artisan vendor:publish --provider="LaravelPropertyBag\ServiceProvider" ``` +##### Publish the UserSettings directory to your app/ directory +``` +php artisan lpb:publish-user +``` +This will create a UserSettings directory containing a UserPropertyBag model and a UserSettings class where you can configure how the package works. + ##### Run the migration ``` php artisan migrate @@ -45,15 +51,16 @@ class User extends Model ``` ##### Register your settings plus their allowed values and defaults -In /config/laravel-property-bag.php, register settings under the registered_user_settings key. +After publishing the UserSettings directory (hopefully you did this above), register settings in the UserSettings class. ```php -'registered_user_settings' => collect([ +protected $registeredSettings = [ 'example_setting' => [ 'allowed' => [true, false], - 'default' => true + 'default' => false ] -]) +]; ``` +Each setting must contain an array of allowed values and a default value. ##### Set the setting from the user model or from the global settings() helper ```php diff --git a/src/Commands/PublishUserSettings.php b/src/Commands/PublishUserSettings.php index d3c5c57..35a4a6c 100644 --- a/src/Commands/PublishUserSettings.php +++ b/src/Commands/PublishUserSettings.php @@ -24,18 +24,6 @@ class PublishUserSettings extends Command */ protected $description = 'Publish the user settings files to the app'; - /** - * Create a new command instance. - * - * @param DripEmailer $drip - * @return void - */ - public function __construct() - { - parent::__construct(); - - } - /** * Execute the console command. * diff --git a/src/Settings/HasSettings.php b/src/Settings/HasSettings.php index ea269c9..0c400cf 100644 --- a/src/Settings/HasSettings.php +++ b/src/Settings/HasSettings.php @@ -3,7 +3,7 @@ namespace LaravelPropertyBag\Settings; use LaravelPropertyBag\UserSettings\UserSettings; -use \Illuminate\Console\AppNamespaceDetectorTrait; +use Illuminate\Console\AppNamespaceDetectorTrait; use LaravelPropertyBag\UserSettings\UserPropertyBag; trait HasSettings @@ -74,39 +74,29 @@ public function allSettings() */ protected function getSettingsClass() { - $settingsName = $this->getSettingsName(); - - $appNamespace = $this->getAppNamespace(); - - $classNamespace = $appNamespace.$settingsName.'\\'.$settingsName; + $classNamespace = $this->makeNamespace([$this, 'getSettingsName']); if (isset($this->settingsClass)) { return $this->settingsClass; - } else if (class_exists($classNamespace)) { + } elseif (class_exists($classNamespace)) { return $classNamespace; } - + return UserSettings::class; } /** - * Get the settings class name. + * Get the property bag class name. * * @return string */ public function getPropertyBagClass() { - $settingsName = $this->getSettingsName(); - - $propertyBagName = $this->getPropertyBagName(); - - $appNamespace = $this->getAppNamespace(); - - $classNamespace = $appNamespace.$settingsName.'\\'.$propertyBagName; + $classNamespace = $this->makeNamespace([$this, 'getPropertyBagName']); if (isset($this->propertyBagClass)) { return $this->propertyBagClass; - } else if (class_exists($classNamespace)) { + } elseif (class_exists($classNamespace)) { return $classNamespace; } @@ -133,6 +123,22 @@ protected function getPropertyBagName() return $this->getShortClassName().'PropertyBag'; } + /** + * Make class namespace. + * + * @param callable $callback + * + * @return string + */ + protected function makeNamespace(callable $callback) + { + $settingsName = $this->getSettingsName(); + + $appNamespace = $this->getAppNamespace(); + + return $appNamespace.$settingsName.'\\'.$callback(); + } + /** * Get the short name of the model. *