-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from mostafaznv/dev
Dev
- Loading branch information
Showing
39 changed files
with
1,792 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Mostafaznv\LaraCache\Actions; | ||
|
||
use Mostafaznv\LaraCache\Actions\Support\UpdateDeleteCache; | ||
use Mostafaznv\LaraCache\DTOs\CommandData; | ||
|
||
class DeleteCacheAction extends UpdateDeleteCache | ||
{ | ||
public function run(CommandData $data): void | ||
{ | ||
if (count($data->models) > 1) { | ||
foreach ($data->models as $model) { | ||
$this->deleteAll($model); | ||
} | ||
} | ||
else { | ||
$model = $data->models[0]; | ||
|
||
empty($data->entities) | ||
? $this->deleteAll($model) | ||
: $this->delete($model, $data->entities); | ||
} | ||
} | ||
|
||
/** | ||
* @param \Mostafaznv\LaraCache\Traits\LaraCache $model | ||
* @return void | ||
*/ | ||
private function deleteAll(string $model): void | ||
{ | ||
$entities = []; | ||
|
||
foreach ($model::cacheEntities() as $entity) { | ||
$entities[] = $entity->name; | ||
} | ||
|
||
$this->delete($model, $entities); | ||
} | ||
|
||
/** | ||
* @param \Mostafaznv\LaraCache\Traits\LaraCache $model | ||
* @param array $entities | ||
* @return void | ||
*/ | ||
private function delete(string $model, array $entities): void | ||
{ | ||
$this->console?->warn( | ||
sprintf('>> Deleting cache entities in [%s] model', class_basename($model)) | ||
); | ||
|
||
foreach ($entities as $entity) { | ||
$this->console?->line('— ' . $this->title($entity)); | ||
|
||
$model::cache()->delete($entity); | ||
|
||
$this->console?->info('Deleted'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Mostafaznv\LaraCache\Actions; | ||
|
||
use Mostafaznv\LaraCache\Actions\Support\UpdateDeleteCache; | ||
|
||
class DeleteGroupCacheAction extends UpdateDeleteCache | ||
{ | ||
public function run(string $group): void | ||
{ | ||
$group = $this->group($group); | ||
|
||
foreach ($group as $item) { | ||
DeleteCacheAction::make($this->console)->run($item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Mostafaznv\LaraCache\Actions\Support; | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\Console\Command; | ||
use Mostafaznv\LaraCache\DTOs\CommandData; | ||
use Mostafaznv\LaraCache\Exceptions\CacheGroupNotExist; | ||
use Mostafaznv\LaraCache\Exceptions\CacheGroupValueIsNotValid; | ||
|
||
abstract class UpdateDeleteCache | ||
{ | ||
public function __construct(protected ?Command $console) {} | ||
|
||
public static function make(?Command $console = null): self | ||
{ | ||
return new static($console); | ||
} | ||
|
||
|
||
protected function title(string $string): string | ||
{ | ||
return Str::title( | ||
Str::slug( | ||
title: Str::replace(['.', '-', '_'], ' ', $string), | ||
separator: ' ' | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @param string $group | ||
* @return CommandData[] | ||
*/ | ||
protected function group(string $group): array | ||
{ | ||
$groupName = $group; | ||
$group = config("laracache.groups.$group"); | ||
|
||
if (is_null($group)) { | ||
throw CacheGroupNotExist::make($groupName); | ||
} | ||
|
||
if (is_array($group)) { | ||
$data = []; | ||
|
||
foreach ($group as $item) { | ||
$data[] = $this->makeCommandDataFromGroupItem($item, $groupName); | ||
} | ||
|
||
return $data; | ||
} | ||
else { | ||
throw CacheGroupValueIsNotValid::make($groupName); | ||
} | ||
} | ||
|
||
private function makeCommandDataFromGroupItem(mixed $item, string $groupName): CommandData | ||
{ | ||
if (isset($item['model']) and is_string($item['model']) and isset($item['entities'])) { | ||
return CommandData::make( | ||
models: [$item['model']], | ||
entities: $item['entities'] | ||
); | ||
} | ||
else { | ||
throw CacheGroupValueIsNotValid::make($groupName); | ||
} | ||
} | ||
} |
Oops, something went wrong.