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

Morphs #71

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +84,34 @@

/*
|--------------------------------------------------------------------------
| Teams
| Morphs
|--------------------------------------------------------------------------
|
| When set to true the package implements teams using the `team_foreign_key`.
| When set to true the package implements morphs on the settings`.
|
| If you want the migrations to register the `team_foreign_key`, you must
| If you want the migrations to register the morphs, you must
| set this to true before running the migration.
|
| If you already ran the migrations, then you must make a new migration to
| add the `team_foreign_key` column to the settings table, and update the
| unique constraint on the table. See the `add_settings_team_field` migration
| add the morphs columns to the settings table, and update the
| unique constraint on the table. See the `add_settings_morphs_fields` migration
| for how to do this.
|
*/
'teams' => false,
'morphs' => false,

/*
|--------------------------------------------------------------------------
| Team Foreign Key
| Morph name
|--------------------------------------------------------------------------
|
| When teams is set to true, our database/eloquent drivers will use this
| column as a team foreign key to scope queries to.
| When morphs is set to true, our database/eloquent drivers will use this
| name for columns to scope queries to.
|
| The team id will also be included in a cache key when caching is enabled.
| The morphs will also be included in a cache key when caching is enabled.
|
*/
'team_foreign_key' => 'team_id',
'morph_name' => 'model',

/*
|--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ return new class extends Migration
{
public function up(): void
{
if (! config('settings.teams')) {
if (! config('settings.morphs')) {
return;
}

Schema::table(config('settings.table'), function (Blueprint $table) {
$table->unsignedBigInteger(config('settings.team_foreign_key'))->nullable()->after('id');
$table->index(config('settings.team_foreign_key'), 'settings_team_id_index');
$table->nullableUuidMorphs(config('settings.morph_name'));

$table->dropUnique('settings_key_unique');

$table->unique([
'key',
config('settings.team_foreign_key'),
config('settings.morph_name') . '_id',
config('settings.morph_name') . '_type',
], 'settings_key_team_id_unique');
});
}
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ parameters:
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false
12 changes: 6 additions & 6 deletions src/Contracts/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

interface Driver
{
public function forget($key, $teamId = null);
public function forget($key, $morphId = null, $morphType = null);

public function get(string $key, $default = null, $teamId = null);
public function get(string $key, $default = null, $morphId = null, $morphType = null);

public function all($teamId = null, $keys = null): array|Arrayable;
public function all($morphId = null, $morphType = null, $keys = null): array|Arrayable;

public function has($key, $teamId = null): bool;
public function has($key, $morphId = null, $morphType = null): bool;

public function set(string $key, $value = null, $teamId = null);
public function set(string $key, $value = null, $morphId = null, $morphType = null);

public function flush($teamId = null, $keys = null);
public function flush($morphId = null, $keys = null, $morphType = null);
}
12 changes: 6 additions & 6 deletions src/Contracts/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

interface Setting
{
public static function getValue(string $key, $default = null, $teamId = null);
public static function getValue(string $key, $default = null, $morphId = null, $morphType = null);

public static function getAll($teamId = null, $keys = null): array|Arrayable;
public static function getAll($morphId = null, $morphType = null, $keys = null): array|Arrayable;

public static function has($key, $teamId = null): bool;
public static function has($key, $morphId = null, $morphType = null): bool;

public static function removeSetting($key, $teamId = null);
public static function removeSetting($key, $morphId = null, $morphType = null): void;

public static function set(string $key, $value = null, $teamId = null);
public static function set(string $key, $value = null, $morphId = null, $morphType = null);

public static function flush($teamId = null, $keys = null);
public static function flush($morphId = null, $morphType = null, $keys = null);
}
60 changes: 40 additions & 20 deletions src/Drivers/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,81 @@ class DatabaseDriver implements Driver
public function __construct(
protected Connection $connection,
protected string $table,
protected ?string $teamForeignKey = null,
protected ?string $morphName = null,
) {}

public function forget($key, $teamId = null): void
public function forget($key, $morphId = null, $morphType = null): void
{
$this->db()
->where('key', $key)
->when(
$teamId !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->teamForeignKey}", $teamId)
$morphId !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_id", $morphId)
)
->when(
$morphType !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_type", $morphType)
)
->delete();
}

public function get(string $key, $default = null, $teamId = null)
public function get(string $key, $default = null, $morphId = null, $morphType = null)
{
$value = $this->db()
->where('key', $key)
->when(
$teamId !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->teamForeignKey}", $teamId)
$morphId !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_id", $morphId)
)
->when(
$morphType !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_type", $morphType)
)
->value('value');

return $value ?? $default;
}

public function all($teamId = null, $keys = null): array|Arrayable
public function all($morphId = null, $morphType = null, $keys = null): array|Arrayable
{
return $this->baseBulkQuery($teamId, $keys)->get();
return $this->baseBulkQuery($morphId, $morphType, $keys)->get();
}

public function has($key, $teamId = null): bool
public function has($key, $morphId = null, $morphType = null): bool
{
return $this->db()
->where('key', $key)
->when(
$teamId !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->teamForeignKey}", $teamId)
$morphId !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_id", $morphId)
)
->when(
$morphType !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_type", $morphType)
)
->exists();
}

public function set(string $key, $value = null, $teamId = null): void
public function set(string $key, $value = null, $morphId = null, $morphType = null): void
{
$data = [
'key' => $key,
];

if ($teamId !== false) {
$data[$this->teamForeignKey] = $teamId;
if ($morphId !== false) {
$data["{$this->morphName}_id"] = $morphId;
}

if ($morphType !== false) {
$data["{$this->morphName}_type"] = $morphType;
}

$this->db()->updateOrInsert($data, compact('value'));
}

public function flush($teamId = null, $keys = null): void
public function flush($morphId = null, $keys = null, $morphType = null): void
{
$this->baseBulkQuery($teamId, $keys)->delete();
$this->baseBulkQuery($morphId, $morphType, $keys)->delete();
}

protected function db(): Builder
Expand All @@ -95,7 +111,7 @@ protected function normalizeKeys($keys): string|Collection|bool
return collect($keys)->flatten()->filter();
}

private function baseBulkQuery($teamId, $keys): Builder
private function baseBulkQuery($morphId, $morphType, $keys): Builder
{
$keys = $this->normalizeKeys($keys);

Expand All @@ -115,8 +131,12 @@ private function baseBulkQuery($teamId, $keys): Builder
fn (Builder $query) => $query->whereIn('key', $keys),
)
->when(
$teamId !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->teamForeignKey}", $teamId)
$morphId !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_id", $morphId)
)
->when(
$morphType !== false,
fn (Builder $query) => $query->where("{$this->table}.{$this->morphName}_type", $morphType)
);
}
}
24 changes: 12 additions & 12 deletions src/Drivers/EloquentDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ class EloquentDriver implements Driver
{
public function __construct(protected Setting $model) {}

public function forget($key, $teamId = null): void
public function forget($key, $morphId = null, $morphType = null): void
{
$this->model::removeSetting($key, $teamId);
$this->model::removeSetting($key, $morphId, $morphType);
}

public function get(string $key, $default = null, $teamId = null)
public function get(string $key, $default = null, $morphId = null, $morphType = null)
{
return $this->model::getValue($key, $default, $teamId);
return $this->model::getValue($key, $default, $morphId, $morphType);
}

public function all($teamId = null, $keys = null): array|Arrayable
public function all($morphId = null, $morphType = null, $keys = null): array|Arrayable
{
return $this->model::getAll($teamId, $keys);
return $this->model::getAll($morphId, $morphType, $keys);
}

public function has($key, $teamId = null): bool
public function has($key, $morphId = null, $morphType = null): bool
{
return $this->model::has($key, $teamId);
return $this->model::has($key, $morphId, $morphType);
}

public function set(string $key, $value = null, $teamId = null): void
public function set(string $key, $value = null, $morphId = null, $morphType = null): void
{
$this->model::set($key, $value, $teamId);
$this->model::set($key, $value, $morphId, $morphType);
}

public function flush($teamId = null, $keys = null): void
public function flush($morphId = null, $keys = null, $morphType = null): void
{
$this->model::flush($teamId, $keys);
$this->model::flush($morphId, $morphType, $keys);
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function createDatabaseDriver(array $config): DatabaseDriver
return new DatabaseDriver(
connection: $this->app['db']->connection(Arr::get($config, 'connection')),
table: $this->app['config']['settings.table'],
teamForeignKey: $this->app['config']['settings.team_foreign_key'] ?? null,
morphName: $this->app['config']['settings.morph_name'] ?? null,
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Events/SettingWasDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function __construct(
public string $key,
public string $storageKey,
public string $cacheKey,
public mixed $teamId,
public mixed $morphId,
public mixed $morphType,
public bool|Context|null $context,
) {}
}
3 changes: 2 additions & 1 deletion src/Events/SettingsFlushed.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ final class SettingsFlushed

public function __construct(
public bool|Collection|string $keys,
public mixed $teamId,
public mixed $modelId,
public mixed $modelType,
public bool|Context|null $context,
) {}
}
14 changes: 7 additions & 7 deletions src/Facades/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
* @method static self temporarilyDisableCache()
* @method static self disableEncryption()
* @method static self enableEncryption()
* @method static null|mixed getTeamId()
* @method static self setTeamId(mixed $id)
* @method static self enableTeams()
* @method static self disableTeams()
* @method static bool teamsAreEnabled()
* @method static self usingTeam(mixed $teamId)
* @method static self withoutTeams()
* @method static null|mixed getMorphId()
* @method static self setMorphs(mixed $id, mixed $morphType = null)
* @method static self enableMorphs()
* @method static self disableMorphs()
* @method static bool morphsAreEnabled()
* @method static self usingMorph(mixed $morphId, mixed $morphType = null)
* @method static self withoutMorphs()
* @method static \Rawilk\Settings\Contracts\KeyGenerator getKeyGenerator()
* @method static string cacheKeyForSetting(string $key)
*/
Expand Down
Loading