├── .github └── workflows │ └── tests.yml ├── LICENSE.md ├── README.md ├── composer.json ├── database └── migrations │ └── 2017_03_03_100000_create_options_table.php ├── phpunit.xml └── src ├── Console └── OptionSetCommand.php ├── Option.php ├── OptionFacade.php ├── OptionsServiceProvider.php └── helpers.php /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: [pull_request] 4 | 5 | jobs: 6 | test: 7 | runs-on: ${{ matrix.os }} 8 | timeout-minutes: 5 9 | strategy: 10 | fail-fast: true 11 | matrix: 12 | os: [ubuntu-latest] 13 | php: [8.4, 8.3] 14 | laravel: [12.*, 11.*, 10.*] 15 | stability: [prefer-lowest, prefer-stable] 16 | include: 17 | - laravel: 12.* 18 | testbench: 10.* 19 | - laravel: 11.* 20 | testbench: 9.* 21 | - laravel: 10.* 22 | testbench: 8.* 23 | 24 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} 25 | 26 | steps: 27 | - name: Checkout code 28 | uses: actions/checkout@v4 29 | 30 | - name: Setup PHP 31 | uses: shivammathur/setup-php@v2 32 | with: 33 | php-version: ${{ matrix.php }} 34 | coverage: none 35 | 36 | - name: Install dependencies 37 | run: | 38 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 39 | composer update --${{ matrix.stability }} --prefer-dist --no-interaction 40 | 41 | - name: List Installed Dependencies 42 | run: composer show -D 43 | 44 | - name: Execute tests 45 | run: vendor/bin/phpunit 46 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Appstract 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 | # Laravel Options 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/appstract/laravel-options.svg?style=flat-square)](https://packagist.org/packages/appstract/laravel-options) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/appstract/laravel-options.svg?style=flat-square)](https://packagist.org/packages/appstract/laravel-options) 5 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 6 | 7 | Global key-value store in the database 8 | 9 | ## Installation 10 | 11 | To get started with laravel-options, use Composer to add the package to your project's dependencies: 12 | 13 | ```bash 14 | composer require appstract/laravel-options 15 | ``` 16 | 17 | ### Publish, migrate 18 | 19 | By running `php artisan vendor:publish --provider="Appstract\Options\OptionsServiceProvider"` in your project all files for this package will be published. For this package, it's only a migration. Run `php artisan migrate` to migrate the table. There will now be an `options` table in your database. 20 | 21 | ## Usage 22 | 23 | With the `option()` helper, we can get and set options: 24 | 25 | ```php 26 | // Get option 27 | option('someKey'); 28 | 29 | // Get option, with a default fallback value if the key doesn't exist 30 | option('someKey', 'Some default value if the key is not found'); 31 | 32 | // Set option 33 | option(['someKey' => 'someValue']); 34 | 35 | // Remove option 36 | option()->remove('someKey'); 37 | 38 | // Check the option exists 39 | option_exists('someKey'); 40 | ``` 41 | 42 | If you want to check if an option exists, you can use the facade: 43 | 44 | ```php 45 | use Option; 46 | 47 | $check = Option::exists('someKey'); 48 | ``` 49 | 50 | The helpers are available as Blade directives: 51 | ```php 52 | @option('someKey') 53 | 54 | @option('someKey', 'Default value') 55 | 56 | @optionExists('someKey') 57 | // Code 58 | @endif 59 | ``` 60 | 61 | ### Console 62 | 63 | It is also possible to set options within the console: 64 | 65 | ```bash 66 | php artisan option:set {someKey} {someValue} 67 | ``` 68 | 69 | ## Testing 70 | 71 | ```bash 72 | $ composer test 73 | ``` 74 | 75 | ## Contributing 76 | 77 | Contributions are welcome, [thanks to y'all](https://github.com/appstract/laravel-options/graphs/contributors) :) 78 | 79 | ## About Appstract 80 | 81 | Appstract is a small team from The Netherlands. We create (open source) tools for webdevelopment and write about related subjects on [Medium](https://medium.com/appstract). You can [follow us on Twitter](https://twitter.com/teamappstract), [buy us a beer](https://www.paypal.me/teamappstract/10) or [support us on Patreon](https://www.patreon.com/appstract). 82 | 83 | ## License 84 | 85 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 86 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "appstract/laravel-options", 3 | "description": "Global options loaded from the database", 4 | "keywords": [ 5 | "appstract", 6 | "laravel-options" 7 | ], 8 | "homepage": "https://github.com/appstract/laravel-options", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Gijs Jorissen", 13 | "email": "gijs@appstract.nl", 14 | "homepage": "https://appstract.nl", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=8.3", 20 | "illuminate/support": "^10.0|^11.0|^12.0", 21 | "illuminate/database": "^10.0|^11.0|^12.0" 22 | }, 23 | "require-dev": { 24 | "orchestra/testbench": "^10.0|^9.0|^8.22" 25 | }, 26 | "autoload": { 27 | "files": [ 28 | "src/helpers.php" 29 | ], 30 | "psr-4": { 31 | "Appstract\\Options\\": "src" 32 | } 33 | }, 34 | "autoload-dev": { 35 | "psr-4": { 36 | "Appstract\\Options\\Test\\": "tests" 37 | } 38 | }, 39 | "scripts": { 40 | "test": "vendor/bin/phpunit" 41 | }, 42 | "config": { 43 | "sort-packages": true 44 | }, 45 | "extra": { 46 | "laravel": { 47 | "providers": [ 48 | "Appstract\\Options\\OptionsServiceProvider" 49 | ], 50 | "aliases": { 51 | "Option": "Appstract\\Options\\OptionFacade" 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /database/migrations/2017_03_03_100000_create_options_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->string('key')->unique(); 19 | $table->json('value'); 20 | }); 21 | } 22 | 23 | /** 24 | * Reverse the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function down() 29 | { 30 | Schema::dropIfExists('options'); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/Console/OptionSetCommand.php: -------------------------------------------------------------------------------- 1 | argument('key'), $this->argument('value')); 34 | 35 | $this->info('Option added.'); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Option.php: -------------------------------------------------------------------------------- 1 | 'json', 23 | ]; 24 | 25 | /** 26 | * The attributes that are mass assignable. 27 | * 28 | * @var [type] 29 | */ 30 | protected $fillable = [ 31 | 'key', 32 | 'value', 33 | ]; 34 | 35 | /** 36 | * Determine if the given option value exists. 37 | * 38 | * @param string $key 39 | * 40 | * @return bool 41 | */ 42 | public function exists($key) 43 | { 44 | return self::where('key', $key)->exists(); 45 | } 46 | 47 | /** 48 | * Get the specified option value. 49 | * 50 | * @param string $key 51 | * @param mixed $default 52 | * 53 | * @return mixed 54 | */ 55 | public function get($key, $default = null) 56 | { 57 | if ($option = self::where('key', $key)->first()) { 58 | return $option->value; 59 | } 60 | 61 | return $default; 62 | } 63 | 64 | /** 65 | * Set a given option value. 66 | * 67 | * @param array|string $key 68 | * @param mixed $value 69 | * 70 | * @return void 71 | */ 72 | public function set($key, $value = null) 73 | { 74 | $keys = is_array($key) ? $key : [$key => $value]; 75 | 76 | foreach ($keys as $key => $value) { 77 | self::updateOrCreate(['key' => $key], ['value' => $value]); 78 | } 79 | 80 | // @todo: return the option 81 | } 82 | 83 | /** 84 | * Remove/delete the specified option value. 85 | * 86 | * @param string $key 87 | * 88 | * @return bool 89 | */ 90 | public function remove($key) 91 | { 92 | return (bool) self::where('key', $key)->delete(); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/OptionFacade.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 19 | $this->publishes([ 20 | __DIR__.'/../database/migrations' => database_path('migrations'), 21 | ], 'migrations'); 22 | 23 | $this->commands([ 24 | \Appstract\Options\Console\OptionSetCommand::class, 25 | ]); 26 | } 27 | } 28 | 29 | /** 30 | * Register the application services. 31 | */ 32 | public function register() 33 | { 34 | $this->app->bind('option', \Appstract\Options\Option::class); 35 | 36 | $this->directives()->each(function ($item, $key) { 37 | Blade::directive($key, $item); 38 | }); 39 | } 40 | 41 | private function directives(): Collection 42 | { 43 | return collect([ 44 | 'option' => function ($key, $default = null) { 45 | return ""; 46 | }, 47 | 48 | 'optionExists' => function ($key) { 49 | return ""; 50 | }, 51 | ]); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | set($key); 22 | } 23 | 24 | return app('option')->get($key, $default); 25 | } 26 | } 27 | 28 | if (!function_exists('option_exists')) { 29 | /** 30 | * Check the specified option exits. 31 | * 32 | * @param string $key 33 | * 34 | * @return mixed 35 | */ 36 | function option_exists($key) 37 | { 38 | return app('option')->exists($key); 39 | } 40 | } 41 | --------------------------------------------------------------------------------