Skip to content
This repository has been archived by the owner on Feb 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Minor optimizations and comments.
  • Loading branch information
DarkGhostHunter authored Jul 18, 2021
2 parents 6da6be6 + 5d4241a commit 0a8c3f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ LARACONFIG_STORE=redis
#### Managing the cache

You can forcefully regenerate the cache of a single user using `regenerate()`. This basically retrieves all the settings from the database (for the bags used) and saves them into the cache.
You can forcefully regenerate the cache of a single user using `regenerate()`. This basically saves the settings present and saves them into the cache.

```php
$user->settings->regenerate();
Expand All @@ -492,7 +492,7 @@ You can also invalidate the cached settings using `invalidate()`, which just del
$user->settings->invalidate();
```

Finally, you can have a little peace of mind by setting `regeneratesOnExit` to `true`, which will regenerate the cache when the settings are garbage collected by the PHP process
Finally, you can have a little peace of mind by setting `regeneratesOnExit` to `true`, which will regenerate the cache when the settings are garbage collected by the PHP process.

```php
$user->settings->regeneratesOnExit = true;
Expand All @@ -502,7 +502,7 @@ $user->settings->regeneratesOnExit = true;
#### Regenerating the Cache on migration

If the [Cache is activated](#cache), the migration will not invalidate the setting cache of all users after it completes.
If the [Cache is activated](#cache), the migration will invalidate the setting cache for each user after it completes.

Depending on the Cache system, forgetting each cache key can be detrimental. Instead, you can use the `--flush-cache` command to flush the cache store used by Laraconfig, instead of deleting each key one by one.

Expand All @@ -512,7 +512,7 @@ Depending on the Cache system, forgetting each cache key can be detrimental. Ins
## Validation

Settings values are casted, but not validated. You should validate in your app every value that you plan to store in a setting.
Settings values are _casted_, but not validated. You should validate in your app every value that you plan to store in a setting.

```php
use App\Models\User;
Expand Down
47 changes: 27 additions & 20 deletions src/MorphManySettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

use DarkGhostHunter\Laraconfig\Eloquent\Metadata;
use Illuminate\Contracts\Cache\Factory;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

/**
* @mixin \Illuminate\Database\Eloquent\Builder
Expand All @@ -34,26 +32,20 @@ class MorphManySettings extends MorphMany
*/
protected ?SettingsCache $cache = null;

public function __construct(Builder $query, Model $parent, $type, $id, $localKey)
{
$this->windUp($parent);

parent::__construct($query, $parent, $type, $id, $localKey);
}

/**
* Set the base constraints on the relation query.
* MorphManySettings constructor.
*
* @return void
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $type
* @param string $id
* @param string $localKey
*/
public function addConstraints(): void
public function __construct(Builder $query, Model $parent, string $type, string $id, string $localKey)
{
parent::addConstraints();
$this->windUp($parent);

// Filter the bags of the model for this relationship.
$this->getQuery()->whereHas('metadata', function (Builder $query): void {
$query->whereIn('bag', $this->bags);
});
parent::__construct($query, $parent, $type, $id, $localKey);
}

/**
Expand All @@ -65,8 +57,7 @@ public function addConstraints(): void
*/
public function windUp(Model $parent): void
{
/** @var \Illuminate\Contracts\Config\Repository $config */
$config = app(Repository::class);
$config = app('config');

// We'll enable the cache for the settings only if is enabled in the
// application config. This object will handle the cache easily and
Expand All @@ -75,11 +66,27 @@ public function windUp(Model $parent): void
$this->cache = SettingsCache::make($config, app(Factory::class), $parent);
}

// And filter the bags if the model has stated them.
$this->bags = Arr::wrap(
method_exists($parent, 'filterBags') ? $parent->filterBags() : $config->get('laraconfig.default', 'users')
);
}

/**
* Set the base constraints on the relation query.
*
* @return void
*/
public function addConstraints(): void
{
parent::addConstraints();

// Filter the bags of the model for this relationship.
$this->getQuery()->whereHas('metadata', function (Builder $query): void {
$query->whereIn('bag', $this->bags);
});
}


/**
* Generates the key for the model to save into the cache.
Expand Down Expand Up @@ -193,4 +200,4 @@ public function bags(): array
{
return $this->bags;
}
}
}

0 comments on commit 0a8c3f8

Please sign in to comment.