Skip to content

Commit

Permalink
added SettingsInspector + ability to settingsTypes via Settings::getM…
Browse files Browse the repository at this point in the history
…odel()

added getModel()

text prettify

added test for getModel

added missing use

introduced getModel() and SettingsInspector

fixed integer
  • Loading branch information
V1pr committed Oct 22, 2024
1 parent c65537b commit 870879f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,27 @@ class SettingsController
}
```

You can get the model of a perticular settings class with getModel():

```php
class IndexController
{
public function __invoke(GeneralSettings $settings){
return view('index', [
'settings_model' => $settings->getModel(),
]);
}
}

/*

Settings::getModel() returns an array:
[
"site_name" => [ "type" => "string", "nullable" => false ]
]
*/
```

### Selecting a repository

Settings will be stored and loaded from the repository. There are two types of repositories `database` and `redis`. And it is possible to create multiple repositories for these types. For example, you could have two `database` repositories, one that goes to a `settings` table in your database and another that goes to a `global_settings` table.
Expand Down
6 changes: 6 additions & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,10 @@ private function ensureConfigIsLoaded(): self

return $this;
}

public function getModel(): array
{
$settingsTypes = SettingsInspector::getSettingsTypes($this);
return $settingsTypes;
}
}
48 changes: 48 additions & 0 deletions src/SettingsInspector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Spatie\LaravelSettings;

use Spatie\LaravelSettings\Settings;
use ReflectionClass;
use ReflectionProperty;

class SettingsInspector
{
public static function getSettingsTypes(array|Settings $settingsClasses): array
{
$settingsCasts = [];

if ( $settingsClasses instanceof Settings ) {
$reflection = new ReflectionClass($settingsClasses);
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);

foreach ($properties as $property) {
$type = $property->getType();
$typeName = $type ? $type->getName() : 'mixed';

$settingsCasts[$property->getName()] = [
'type' => $typeName,
'nullable' => $type ? $type->allowsNull() : false,
];
}

} elseif ( is_array($settingsClasses) ) {
foreach ($settingsClasses as $group => $settingsClass) {
$reflection = new ReflectionClass($settingsClass);
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);

foreach ($properties as $property) {
$type = $property->getType();
$typeName = $type ? $type->getName() : 'mixed';

$settingsCasts[$group][$property->getName()] = [
'type' => $typeName,
'nullable' => $type ? $type->allowsNull() : false,
];
}
}
}

return $settingsCasts;
}
}
20 changes: 20 additions & 0 deletions tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,3 +782,23 @@ public static function group(): string

expect($settings->name)->toBeNull();
});

it('can use getModel', function() {
$this->migrateDummySettings(CarbonImmutable::create('2020-05-16')->startOfDay());

$settings = resolve(DummySettings::class);

$model = $settings->getModel();

expect($model)
->toBeArray()
->string->toBeArray()
->string->type->toEqual('string')
->string->nullable->toEqual(false)
->int->toBeArray()
->int->type->toEqual('int')
->int->nullable->toEqual(false)
->nullable_string->toBeArray()
->nullable_string->type->toEqual('string')
->nullable_string->nullable->toEqual(true);
});

0 comments on commit 870879f

Please sign in to comment.