Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Models and Traits Publishable #39

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
# Changelog
All notable changes to `laravel-config` will be documented in this file.

## [Unreleased]
## [Unreleased] - 2024-04-09

- ### Braking Change: Made the Models and Traits Publishable, so you can add SoftDeletes, auditing, and/or caching if you want (YOU MUST RUN THE INSTALLATION COMMAND)
- ### Braking Change: Check your Namespaces
- Moved the config functions to a Trait so adding functions is as simple as editing the published Trait or adding another Trait
- I left the original `LaravelConfig` class as an alies of the Model, but it needs more testing
- ### Braking Change: The Update function needed to be renamed from `update` to `update_config`
- ### Braking Change: The Update function Only takes one parameter just the `ConfigItem`
- ### Braking Change: The `update_config` helper Only takes one parameter just the `ConfigItem`
- ### Braking Change: The Delete function needed to be renamed from `delete` to `delete_config`
- Because of the Braking Changes I also added a `get_config`, `set_config`, `has_config`, and `create_config` aliases for consistency
- NON Braking Change: Removed the `all` function as it's not needed anymore since it's a function of a Model
- I'm adding more functions to another Trait that can be added by including it in the Model these are all traits that I've found useful
- Added comands to `get_config` and `set_config` from the console
- Added more datatypes


## [5.1.0] - 2024-04-08

Expand Down
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,27 @@ You can install the package via composer:
```bash
composer require tarfin-labs/laravel-config
```
Next, you should publish the Laravel config migration file using the vendor:publish Artisan command.
Next, you MUST publish Models, Factories, and Traits
When updating you might need to rerun the installation command be warned this will overwrite any changes made to any of the published Models, Factories, and Traits

```bash
php artisan laravel-config:install
```
Next, you should publish the Laravel config migration file using the vendor:publish Artisan command.

```bash
php artisan vendor:publish --provider="TarfinLabs\LaravelConfig\LaravelConfigServiceProvider" --tag="laravel-config"
```

If you want to use Laravel Config database factory, you can publish it too, using the command:

```
```bash
php artisan vendor:publish --provider="TarfinLabs\LaravelConfig\LaravelConfigServiceProvider" --tag="laravel-config-factories"
```

Finally, you should run your database migrations:

```
```bash
php artisan migrate
```

Expand All @@ -40,7 +46,7 @@ Simple usage example of laravel-config package in your Laravel app.

Create new config parameter:

``` php
```php
$factory = new ConfigFactory();
$configItem = $factory->setName('key')
->setType(ConfigDataType::BOOLEAN)
Expand All @@ -54,37 +60,37 @@ LaravelConfig::create($configItem);

Get value with config name:

``` php
```php
LaravelConfig::get('key');
```

Set value with config name and value:

``` php
```php
LaravelConfig::set('key', 'value');
```

Get all config parameters:

``` php
```php
LaravelConfig::all();
```

Get config items by tag:

``` php
```php
LaravelConfig::getByTag('key');
```

Check if the config exists:

``` php
```php
LaravelConfig::has('key');
```

Update config with new values:

``` php
```php
$factory = new ConfigFactory($configId);
$configItem = $factory->setName('updated-key')
->setType(ConfigDataType::BOOLEAN)
Expand All @@ -93,13 +99,13 @@ $configItem = $factory->setName('updated-key')
->setDescription('updated description')
->get();

LaravelConfig::update($configItem);
LaravelConfig::update_config($configItem);
```

Remove config:

``` php
LaravelConfig::delete('key');
```php
LaravelConfig::delete_config('key');
```

### Nested Parameters
Expand Down Expand Up @@ -141,7 +147,7 @@ LaravelConfig::getNested('foo');
### Helpers
You can also use helper functions:

``` php
```php
// Creating config item
$factory = new ConfigFactory();
$configItem = $factory->setName('key')
Expand Down Expand Up @@ -182,7 +188,7 @@ read_nested('foo.bar');

### Testing

``` bash
```bash
composer test
```

Expand Down
3 changes: 0 additions & 3 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php

/*
* You can place your custom package configuration in here.
*/
return [
'table' => env('LARAVEL_CONFIG_TABLE', 'laravel_config'),
];
6 changes: 3 additions & 3 deletions database/factories/ConfigFactory.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

use App\Models\Config as ConfigModel;
// use Illuminate\Database\Eloquent\Factory;
use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factory;
use Illuminate\Support\Carbon;
use TarfinLabs\LaravelConfig\Config\Config;

/* @var $factory Factory */

$factory->define(Config::class, function (Faker $faker, array $attributes = []) {
$factory->define(ConfigModel::class, function (Faker $faker, array $attributes = []) {
return [
'name' => $attributes['name'] ?? $faker->word().$faker->asciify('*****'),
'type' => $attributes['type'] ?? $faker->randomElement(['boolean', 'text']),
Expand Down
23 changes: 20 additions & 3 deletions src/Casts/ConfigValueCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@

use Carbon\Carbon;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use TarfinLabs\LaravelConfig\Enums\ConfigDataType;

class ConfigValueCast implements CastsAttributes
{
public function get(Model $model, string $key, mixed $value, array $attributes)
/**
* Transform the attribute from the underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return TGet|null
*/
public function get($model, string $key, mixed $value, array $attributes)
{
return match ($attributes['type']) {
ConfigDataType::BOOLEAN->value => (bool) $value,
Expand All @@ -21,7 +29,16 @@ public function get(Model $model, string $key, mixed $value, array $attributes)
};
}

public function set(Model $model, string $key, mixed $value, array $attributes)
/**
* Transform the attribute to its underlying model values.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param TSet|null $value
* @param array $attributes
* @return mixed
*/
public function set($model, string $key, mixed $value, array $attributes)
{
return $value;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Config/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

namespace TarfinLabs\LaravelConfig\Config;

use App\Models\Config as ConfigModel;

class ConfigFactory
{
/**
* @var ConfigItem
*/
protected $configItem;
protected ConfigItem $configItem;

/**
* ConfigFactory constructor.
*
* @param Config|null $config
* @param ConfigModel|null $config
*/
public function __construct(Config $config = null)
public function __construct(ConfigModel $config = null)
{
$this->configItem = new ConfigItem();

Expand Down
25 changes: 25 additions & 0 deletions src/Console/GetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace TarfinLabs\LaravelConfig\Console;

use Illuminate\Console\Command;
use TarfinLabs\LaravelConfig\LaravelConfigFacade as LaravelConfig;

class GetCommand extends Command
{
protected $signature = 'laravel-config:get
{key : Key }';

protected $description = 'Get a key from the database and dump it to stdout';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$value = LaravelConfig::get_config($this->argument('key'));
$this->line($value);
}
}
45 changes: 45 additions & 0 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace TarfinLabs\LaravelConfig\Console;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class InstallCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'laravel-config:install';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Install the Laravel-config Models, Factories, and Traits.';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
// Models...
(new Filesystem)->ensureDirectoryExists(app_path('Models/'));
(new Filesystem)->copyDirectory(__DIR__.'/../Models', app_path('Models/'));

// Traits...
(new Filesystem)->ensureDirectoryExists(app_path('Traits/'));
(new Filesystem)->copyDirectory(__DIR__.'/../Traits', app_path('Traits/'));

// Factory...
(new Filesystem)->ensureDirectoryExists(database_path('factories/'));
(new Filesystem)->copy(__DIR__.'/../../database/factories/ConfigFactory.php', database_path('factories/ConfigFactory.php'));

$this->info('Laravel-config installed successfully.');
}
}
25 changes: 25 additions & 0 deletions src/Console/SetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace TarfinLabs\LaravelConfig\Console;

use Illuminate\Console\Command;
use TarfinLabs\LaravelConfig\LaravelConfigFacade as LaravelConfig;

class SetCommand extends Command
{
protected $signature = 'laravel-config:set
{key : Key }
{value : A string value }';

protected $description = 'Save a key to the database';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
LaravelConfig::set_config($this->argument('key'), $this->argument('value'));
}
}
3 changes: 3 additions & 0 deletions src/Enums/ConfigDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

enum ConfigDataType: string
{
case STRING = 'string';
case NUMERIC = 'numeric';
case NONE = 'None';
case INTEGER = 'integer';
case BOOLEAN = 'boolean';
case JSON = 'json';
Expand Down
Loading