Skip to content

Commit

Permalink
Refactored out checking of Cachable trait
Browse files Browse the repository at this point in the history
(instead of looking if method exists)
Fixes #81 #57
  • Loading branch information
mikebronner committed Feb 19, 2018
1 parent a755769 commit cf08e6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Console/Commands/Flush.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public function handle()
}

$model = new $option;
$usesCachableTrait = collect(class_uses($model))
->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable");

if (! method_exists($model, 'flushCache')) {
if (! $usesCachableTrait) {
$this->error("'{$option}' is not an instance of CachedModel.");
$this->line("Only CachedModel instances can be flushed.");

Expand Down
15 changes: 13 additions & 2 deletions src/Traits/Cachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ protected function cache(array $tags = [])
}

if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
array_push($tags, str_slug(get_called_class()));

$tags = $this->addTagsWhenCalledFromCachedBuilder($tags);
$cache = $cache->tags($tags);
}

return $cache;
}

protected function addTagsWhenCalledFromCachedBuilder(array $tags) : array
{
$usesCachableTrait = collect(class_uses($this))
->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable");

if (! $usesCachableTrait) {
array_push($tags, str_slug(get_called_class()));
}

return $tags;
}

public function disableCache()
{
cache()->forever(self::$isCachableKey, true);
Expand Down

0 comments on commit cf08e6d

Please sign in to comment.