├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── json-settings.php └── src ├── Facades └── Settings.php ├── LaravelJsonSettingsServiceProvider.php └── SettingsRepository.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-json-settings` will be documented in this file. 4 | 5 | ## v0.1.0 - 2022-02-14 6 | 7 | **Full Changelog**: https://github.com/ryangjchandler/laravel-json-settings/commits/v0.1.0 8 | 9 | ## 1.0.0 - 202X-XX-XX 10 | 11 | - initial release 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) ryangjchandler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Store your Laravel application settings in an on-disk JSON file. 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/ryangjchandler/laravel-json-settings.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/laravel-json-settings) 4 | [![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/laravel-json-settings/run-tests?label=tests)](https://github.com/ryangjchandler/laravel-json-settings/actions?query=workflow%3Arun-tests+branch%3Amain) 5 | [![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/laravel-json-settings/Check%20&%20fix%20styling?label=code%20style)](https://github.com/ryangjchandler/laravel-json-settings/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/ryangjchandler/laravel-json-settings.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/laravel-json-settings) 7 | 8 | This package provides a simple `SettingsRepository` class that can be used to store your application's settings in a single JSON file. 9 | 10 | ## Installation 11 | 12 | You can install the package via composer: 13 | 14 | ```bash 15 | composer require ryangjchandler/laravel-json-settings 16 | ``` 17 | 18 | You can publish the config file with: 19 | 20 | ```bash 21 | php artisan vendor:publish --tag="json-settings-config" 22 | ``` 23 | 24 | ## Usage 25 | 26 | You can resolve an instance of `RyanChandler\LaravelJsonSettings\SettingsRepository` from the container by type-hinting it in any DI-supported method, e.g. a controller method. 27 | 28 | ```php 29 | class IndexController 30 | { 31 | public function __invoke(SettingsRepository $settings) 32 | { 33 | return view('index', [ 34 | 'title' => $settings->get('index.title'), 35 | ]); 36 | } 37 | } 38 | ``` 39 | 40 | The `SettingsRepository` class contains the following methods: 41 | 42 | * `get(string $key, mixed $default = null)` - retrieve the value of a setting by providing the key (dot-notation supported). 43 | * `set(string $key, mixed $value, bool $save = true)` - set the value of a setting and toggle auto-save. 44 | * `has(string $key)` - determine if a setting exists. 45 | * `save()` - manually save your settings back to disk. 46 | * `reload()` - clear the cache and reload the settings from disk. 47 | 48 | If you prefer to use facades, you can interact with the `RyanChandler\LaravelJsonSettings\Facades\Settings` facade directly too. 49 | 50 | ## Testing 51 | 52 | ```bash 53 | composer test 54 | ``` 55 | 56 | ## Changelog 57 | 58 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 59 | 60 | ## Contributing 61 | 62 | Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. 63 | 64 | ## Security Vulnerabilities 65 | 66 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 67 | 68 | ## Credits 69 | 70 | - [Ryan Chandler](https://github.com/ryangjchandler) 71 | - [All Contributors](../../contributors) 72 | 73 | ## License 74 | 75 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 76 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ryangjchandler/laravel-json-settings", 3 | "description": "Store your Laravel application settings in an on-disk JSON file.", 4 | "keywords": [ 5 | "ryangjchandler", 6 | "laravel", 7 | "laravel-json-settings" 8 | ], 9 | "homepage": "https://github.com/ryangjchandler/laravel-json-settings", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Ryan Chandler", 14 | "email": "support@ryangjchandler.co.uk", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0", 20 | "spatie/laravel-package-tools": "^1.9.2", 21 | "illuminate/contracts": "^9.0" 22 | }, 23 | "require-dev": { 24 | "nunomaduro/collision": "^6.0", 25 | "nunomaduro/larastan": "^1.0", 26 | "orchestra/testbench": "^7.0", 27 | "pestphp/pest": "^1.21", 28 | "pestphp/pest-plugin-laravel": "^1.1", 29 | "phpstan/extension-installer": "^1.1", 30 | "phpstan/phpstan-deprecation-rules": "^1.0", 31 | "phpstan/phpstan-phpunit": "^1.0", 32 | "phpunit/phpunit": "^9.5", 33 | "spatie/laravel-ray": "^1.26" 34 | }, 35 | "autoload": { 36 | "psr-4": { 37 | "RyanChandler\\LaravelJsonSettings\\": "src", 38 | "RyanChandler\\LaravelJsonSettings\\Database\\Factories\\": "database/factories" 39 | } 40 | }, 41 | "autoload-dev": { 42 | "psr-4": { 43 | "RyanChandler\\LaravelJsonSettings\\Tests\\": "tests" 44 | } 45 | }, 46 | "scripts": { 47 | "analyse": "vendor/bin/phpstan analyse", 48 | "test": "vendor/bin/pest", 49 | "test-coverage": "vendor/bin/pest --coverage" 50 | }, 51 | "config": { 52 | "sort-packages": true 53 | }, 54 | "extra": { 55 | "laravel": { 56 | "providers": [ 57 | "RyanChandler\\LaravelJsonSettings\\LaravelJsonSettingsServiceProvider" 58 | ], 59 | "aliases": { 60 | "LaravelJsonSettings": "RyanChandler\\LaravelJsonSettings\\Facades\\LaravelJsonSettings" 61 | } 62 | } 63 | }, 64 | "minimum-stability": "dev", 65 | "prefer-stable": true 66 | } 67 | -------------------------------------------------------------------------------- /config/json-settings.php: -------------------------------------------------------------------------------- 1 | storage_path('settings.json'), 16 | 17 | /* 18 | |-------------------------------------------------------------------------- 19 | | Cache 20 | |-------------------------------------------------------------------------- 21 | | 22 | | Your settings will be cached indefinitely until a value changes. You can 23 | | use these configuration options to change the cache key and the tag that 24 | | gets used. 25 | | 26 | */ 27 | 28 | 'cache' => [ 29 | 'key' => 'json-settings', 30 | 'tag' => null, 31 | ], 32 | 33 | ]; 34 | -------------------------------------------------------------------------------- /src/Facades/Settings.php: -------------------------------------------------------------------------------- 1 | name('laravel-json-settings') 14 | ->hasConfigFile(); 15 | } 16 | 17 | public function bootingPackage() 18 | { 19 | $this->app->singleton(SettingsRepository::class, function ($app) { 20 | return new SettingsRepository($app['config']->get('json-settings.path')); 21 | }); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/SettingsRepository.php: -------------------------------------------------------------------------------- 1 | path)) { 22 | File::put($this->path, json_encode([], JSON_PRETTY_PRINT | JSON_FORCE_OBJECT)); 23 | } 24 | 25 | $this->load(); 26 | } 27 | 28 | public function get(string $key, mixed $default = null): mixed 29 | { 30 | return Arr::get($this->settings, $key, $default); 31 | } 32 | 33 | public function set(string $key, mixed $value, bool $save = true): static 34 | { 35 | Arr::set($this->settings, $key, $value); 36 | 37 | if ($save) { 38 | $this->save(); 39 | } 40 | 41 | return $this; 42 | } 43 | 44 | public function has(string|array $keys): bool 45 | { 46 | return Arr::has($this->settings, $keys); 47 | } 48 | 49 | public function save(int $flags = 0): static 50 | { 51 | File::put($this->path, json_encode($this->settings, JSON_PRETTY_PRINT | $flags)); 52 | 53 | $this->forget(); 54 | 55 | return $this; 56 | } 57 | 58 | public function reload(): static 59 | { 60 | $this->forget(); 61 | $this->load(); 62 | 63 | return $this; 64 | } 65 | 66 | protected function forget(): void 67 | { 68 | $this->getCacheStore()->forget($this->getCacheKey()); 69 | } 70 | 71 | public function getPath(): string 72 | { 73 | return $this->path; 74 | } 75 | 76 | protected function getCacheKey(): string 77 | { 78 | return config('json-settings.cache.key'); 79 | } 80 | 81 | protected function getCacheTag(): ?string 82 | { 83 | return config('json-settings.cache.tag'); 84 | } 85 | 86 | protected function getCacheStore(): TaggedCache|Repository 87 | { 88 | return $this->getCacheTag() ? Cache::tags($this->getCacheTag()) : Cache::store(); 89 | } 90 | 91 | protected function load(): void 92 | { 93 | $this->settings = $this->getCacheStore() 94 | ->rememberForever($this->getCacheKey(), function () { 95 | return json_decode(File::get($this->path), true); 96 | }); 97 | } 98 | } 99 | --------------------------------------------------------------------------------