Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Combind committed Jul 29, 2024
1 parent d22b3ea commit 22c26c7
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 95 deletions.
72 changes: 55 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/combindma/laravel-option/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/combindma/laravel-option/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/combindma/laravel-option.svg?style=flat-square)](https://packagist.org/packages/combindma/laravel-option)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

This package uses [spatie valuestore](https://github.com/spatie/valuestore) under the hood.
## About Combind Agency

[Combine Agency](https://combind.ma?utm_source=github&utm_medium=banner&utm_campaign=package_name) is a leading web development agency specializing in building innovative and high-performance web applications using modern technologies. Our experienced team of developers, designers, and project managers is dedicated to providing top-notch services tailored to the unique needs of our clients.
Expand All @@ -22,13 +21,6 @@ You can install the package via composer:
composer require combindma/laravel-option
```

You can publish and run the migrations with:

```bash
php artisan vendor:publish --tag="laravel-option-migrations"
php artisan migrate
```

You can publish the config file with:

```bash
Expand All @@ -39,20 +31,66 @@ This is the contents of the published config file:

```php
return [
/*
|--------------------------------------------------------------------------
| Default disk
|--------------------------------------------------------------------------
|
| This is the default disk that will be used to store the options file.
|
*/

'disk' => 'local',
/*
|--------------------------------------------------------------------------
| Default filename
|--------------------------------------------------------------------------
|
| This is the default filename for the options file.
|
*/

'filename' => 'options.json',
];
```

Optionally, you can publish the views using

```bash
php artisan vendor:publish --tag="laravel-option-views"
```

## Usage
This package makes it easy to store and retrieve some loose values. Stored values are saved as a json file.

It can be used like this:

```php
$option = new Combindma\Option();
echo $option->echoPhrase('Hello, Combindma!');
option()->put('key', 'value');

option()->get('key'); // Returns 'value'

option()->has('key'); // Returns true

// Specify a default value for when the specified key does not exist
option()->get('non existing key', 'default') // Returns 'default'

option()->put('anotherKey', 'anotherValue');

// Put multiple items in one go
option()->put(['ringo' => 'drums', 'paul' => 'bass']);

option()->all(); // Returns an array with all items

option()->forget('key'); // Removes the item

option()->flush(); // Empty the entire options

option()->flushStartingWith('somekey'); // remove all items whose keys start with "somekey"

option()->increment('number'); // option()->get('number') will return 1
option()->increment('number'); // option()->get('number') will return 2
option()->increment('number', 3); // option()->get('number') will return 5

// Option implements ArrayAccess
option()['key'] = 'value';
option()['key']; // Returns 'value'
isset(option()['key']); // Return true
unset(option()['key']); // Equivalent to removing the value
```

## Testing
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
],
"require": {
"php": "^8.3",
"illuminate/contracts": "^11.0",
"spatie/laravel-package-tools": "^1.16",
"illuminate/contracts": "^11.0"
"spatie/valuestore": "^1.3"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down
32 changes: 6 additions & 26 deletions config/option.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,24 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Default Option Store
| Default disk
|--------------------------------------------------------------------------
|
| This option controls the default option connection that gets used while
| using this caching library. This connection is used when another is
| not explicitly specified when executing a given caching function.
|
| Supported: "database", "file"
| This is the default disk that will be used to store the options file.
|
*/

'default' => env('OPTION_DRIVER', 'file'),

'disk' => 'local',
/*
|--------------------------------------------------------------------------
| Option Stores
| Default filename
|--------------------------------------------------------------------------
|
| Here you may define all of the option "stores" for your application as
| well as their drivers. You may even define multiple stores for the
| same option driver to group types of items stored in your options.
| This is the default filename for the options file.
|
*/

'stores' => [
'database' => [
'driver' => 'database',
'table' => 'options',
'connection' => null,
],

'file' => [
'driver' => 'file',
'path' => storage_path('framework/app/options'),
'single_file' => true,
],
],
'filename' => 'options.json',
];
23 changes: 0 additions & 23 deletions database/migrations/create_options_table.php.stub

This file was deleted.

29 changes: 28 additions & 1 deletion src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,31 @@

namespace Combindma\Option;

class Option {}
use Illuminate\Support\Facades\Storage;
use Spatie\Valuestore\Valuestore;

class Option
{
public string $fileName;

public string $disk;

public function __construct()
{
$this->fileName = config('option.filename');
$this->disk = config('option.disk');
$this->init();
}

public function init(): void
{
if (! Storage::disk($this->disk)->exists($this->fileName)) {
Storage::disk($this->disk)->put($this->fileName, json_encode([]));
}
}

public function make(): Valuestore
{
return Valuestore::make(Storage::disk($this->disk)->path($this->fileName));
}
}
3 changes: 1 addition & 2 deletions src/OptionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public function configurePackage(Package $package): void
{
$package
->name('laravel-option')
->hasConfigFile('option')
->hasMigration('create_options_table');
->hasConfigFile('option');
}
}
30 changes: 5 additions & 25 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
<?php

if (! function_exists('option')) {
/**
* Add Or Retrieve Option.
*
* @param string $key
* @param mixed $default
* @return mixed $value
*/
function option($key = null, $default = null)
{
if (! is_null($key)) {
return app('armincms.option')->get($key, $default);
}

return app('armincms.option')->store();
}
}
use Combindma\Option\Option;
use Spatie\Valuestore\Valuestore;

if (! function_exists('option_exists')) {
/**
* Check existance of option.
*
* @return bool
*/
function option_exists(string $key)
if (! function_exists('option')) {
function option(): Valuestore
{
return option()->has($key);
return (new Option)->make();
}
}

0 comments on commit 22c26c7

Please sign in to comment.