generated from vormkracht10/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
80 additions
and
29 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
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,15 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\PermanentCache\Events; | ||
|
||
use Illuminate\Foundation\Events\Dispatchable; | ||
|
||
class UpdatingPermanentCacheEvent | ||
{ | ||
use Dispatchable; | ||
|
||
public function __construct() | ||
{ | ||
// | ||
} | ||
} |
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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\PermamentCache\Facades; | ||
namespace Vormkracht10\PermanentCache\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* @see \Vormkracht10\PermamentCache\PermamentCache | ||
* @see \Vormkracht10\PermanentCache\PermanentCache | ||
*/ | ||
class PermamentCache extends Facade | ||
class PermanentCache extends Facade | ||
{ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return \Vormkracht10\PermamentCache\PermamentCache::class; | ||
return \Vormkracht10\PermanentCache\PermanentCache::class; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,20 +1,56 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\PermamentCache; | ||
namespace Vormkracht10\PermanentCache; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Event; | ||
use Vormkracht10\PermanentCache\Events\UpdatingPermanentCacheEvent; | ||
|
||
class PermanentCache | ||
{ | ||
protected array $caches = []; | ||
protected array $cachers = []; | ||
|
||
public function caches(array $caches): self | ||
public function caches(array $cachers): self | ||
{ | ||
$this->caches = array_merge($this->caches, $caches); | ||
foreach ($cachers as $cacher) { | ||
$event = $this->resolveEventType($cacher) | ||
?: UpdatingPermanentCacheEvent::class; | ||
|
||
$resolved[$event][] = $cacher; | ||
|
||
Event::listen($event, $cacher); | ||
} | ||
|
||
$this->cachers = array_merge($this->cachers, $resolved ?? []); | ||
|
||
return $this; | ||
} | ||
|
||
public function configuredCaches() | ||
/** | ||
* @return class-string|false | ||
* @throws \ReflectionException | ||
* @throws \Exception | ||
*/ | ||
protected function resolveEventType(string $class): string|false | ||
{ | ||
if (! method_exists($class, 'run')) { | ||
throw new \Exception('Every cacher needs a run method.'); | ||
} | ||
|
||
return ((new \ReflectionClass($class)) | ||
->getMethod('run') | ||
->getParameters() | ||
[0] ?? null) | ||
?->getType() | ||
?->getName() | ||
?? false; | ||
} | ||
|
||
/** | ||
* @return Collection<class-string, array<class-string>> | ||
*/ | ||
public function configuredCaches(): Collection | ||
{ | ||
return collect($this->caches); | ||
return collect($this->cachers); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?php | ||
|
||
use Vormkracht10\PermamentCache\Tests\TestCase; | ||
use Vormkracht10\PermanentCache\Tests\TestCase; | ||
|
||
uses(TestCase::class)->in(__DIR__); |
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