Skip to content

Commit

Permalink
Refactor HasSettings.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleigh committed Jun 6, 2016
1 parent dd5f4dc commit 6274078
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 0 additions & 12 deletions src/Commands/PublishUserSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
40 changes: 23 additions & 17 deletions src/Settings/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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.
*
Expand Down

0 comments on commit 6274078

Please sign in to comment.