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

Commit 0a8c3f8

Browse files
Merge pull request #2 from DarkGhostHunter/master
Minor optimizations and comments.
2 parents 6da6be6 + 5d4241a commit 0a8c3f8

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ LARACONFIG_STORE=redis
480480
481481
#### Managing the cache
482482

483-
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.
483+
You can forcefully regenerate the cache of a single user using `regenerate()`. This basically saves the settings present and saves them into the cache.
484484

485485
```php
486486
$user->settings->regenerate();
@@ -492,7 +492,7 @@ You can also invalidate the cached settings using `invalidate()`, which just del
492492
$user->settings->invalidate();
493493
```
494494

495-
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
495+
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.
496496

497497
```php
498498
$user->settings->regeneratesOnExit = true;
@@ -502,7 +502,7 @@ $user->settings->regeneratesOnExit = true;
502502
503503
#### Regenerating the Cache on migration
504504

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

507507
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.
508508

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

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

517517
```php
518518
use App\Models\User;

src/MorphManySettings.php

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
use DarkGhostHunter\Laraconfig\Eloquent\Metadata;
66
use Illuminate\Contracts\Cache\Factory;
7-
use Illuminate\Contracts\Config\Repository;
87
use Illuminate\Database\Eloquent\Builder;
98
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
109
use Illuminate\Database\Eloquent\Model;
1110
use Illuminate\Database\Eloquent\Relations\MorphMany;
1211
use Illuminate\Support\Arr;
13-
use Illuminate\Support\Collection;
1412

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

37-
public function __construct(Builder $query, Model $parent, $type, $id, $localKey)
38-
{
39-
$this->windUp($parent);
40-
41-
parent::__construct($query, $parent, $type, $id, $localKey);
42-
}
43-
4435
/**
45-
* Set the base constraints on the relation query.
36+
* MorphManySettings constructor.
4637
*
47-
* @return void
38+
* @param \Illuminate\Database\Eloquent\Builder $query
39+
* @param \Illuminate\Database\Eloquent\Model $parent
40+
* @param string $type
41+
* @param string $id
42+
* @param string $localKey
4843
*/
49-
public function addConstraints(): void
44+
public function __construct(Builder $query, Model $parent, string $type, string $id, string $localKey)
5045
{
51-
parent::addConstraints();
46+
$this->windUp($parent);
5247

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

5951
/**
@@ -65,8 +57,7 @@ public function addConstraints(): void
6557
*/
6658
public function windUp(Model $parent): void
6759
{
68-
/** @var \Illuminate\Contracts\Config\Repository $config */
69-
$config = app(Repository::class);
60+
$config = app('config');
7061

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

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

75+
/**
76+
* Set the base constraints on the relation query.
77+
*
78+
* @return void
79+
*/
80+
public function addConstraints(): void
81+
{
82+
parent::addConstraints();
83+
84+
// Filter the bags of the model for this relationship.
85+
$this->getQuery()->whereHas('metadata', function (Builder $query): void {
86+
$query->whereIn('bag', $this->bags);
87+
});
88+
}
89+
8390

8491
/**
8592
* Generates the key for the model to save into the cache.
@@ -193,4 +200,4 @@ public function bags(): array
193200
{
194201
return $this->bags;
195202
}
196-
}
203+
}

0 commit comments

Comments
 (0)