Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
david-d-h committed Dec 8, 2023
1 parent 851a100 commit 6c99e10
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ return [
## Usage

```php
$permamentCache = new Vormkracht10\PermamentCache();
echo $permamentCache->echoPhrase('Hello, Vormkracht10!');
$permanentCache = new Vormkracht10\PermanentCache();
echo $permanentCache->echoPhrase('Hello, Vormkracht10!');
```

## Testing
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
},
"autoload": {
"psr-4": {
"Vormkracht10\\PermamentCache\\": "src/",
"Vormkracht10\\PermamentCache\\Database\\Factories\\": "database/factories/"
"Vormkracht10\\PermanentCache\\": "src/",
"Vormkracht10\\PermanentCache\\Database\\Factories\\": "database/factories/"
}
},
"autoload-dev": {
"psr-4": {
"Vormkracht10\\PermamentCache\\Tests\\": "tests/"
"Vormkracht10\\PermanentCache\\Tests\\": "tests/"
}
},
"scripts": {
Expand All @@ -60,10 +60,10 @@
"extra": {
"laravel": {
"providers": [
"Vormkracht10\\PermamentCache\\PermamentCacheServiceProvider"
"Vormkracht10\\PermanentCache\\PermanentCacheServiceProvider"
],
"aliases": {
"PermamentCache": "Vormkracht10\\PermamentCache\\Facades\\PermamentCache"
"PermanentCache": "Vormkracht10\\PermanentCache\\PermanentCache"
}
}
},
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/UpdatePermanentCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Illuminate\Console\Command;
use Illuminate\Console\Concerns\InteractsWithIO;
use Vormkracht10\PermamentCache\Events\PermanentCacheUpdated;
use Vormkracht10\PermamentCache\Events\PermanentCacheUpdateFailed;
use Vormkracht10\PermamentCache\Facades\PermanentCache;
use Vormkracht10\permanentCache\Events\PermanentCacheUpdated;
use Vormkracht10\permanentCache\Events\PermanentCacheUpdateFailed;
use Vormkracht10\permanentCache\Facades\PermanentCache;
use Vormkracht10\PermanentCache\Exceptions\CouldNotUpdatePermanentCache;

class UpdatePermanentCacheCommand extends Command
Expand Down
2 changes: 1 addition & 1 deletion src/Events/PermanentCacheUpdateFailed.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Vormkracht10\PermamentCache\Events;
namespace Vormkracht10\PermanentCache\Events;

class PermanentCacheUpdateFailed
{
Expand Down
2 changes: 1 addition & 1 deletion src/Events/PermanentCacheUpdated.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Vormkracht10\PermamentCache\Events;
namespace Vormkracht10\PermanentCache\Events;

class PermanentCacheUpdated
{
Expand Down
15 changes: 15 additions & 0 deletions src/Events/UpdatingPermanentCacheEvent.php
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()
{
//
}
}
8 changes: 4 additions & 4 deletions src/Facades/PermanentCache.php
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;
}
}
48 changes: 42 additions & 6 deletions src/PermanentCache.php
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);
}
}
4 changes: 2 additions & 2 deletions src/PermanentCacheServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Vormkracht10\PermamentCache;
namespace Vormkracht10\PermanentCache;

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Vormkracht10\PermanentCache\Commands\UpdatePermanentCacheCommand;

class PermamentCacheServiceProvider extends PackageServiceProvider
class PermanentCacheServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
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__);
8 changes: 4 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Vormkracht10\PermamentCache\Tests;
namespace Vormkracht10\PermanentCache\Tests;

use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;
use Vormkracht10\PermamentCache\PermamentCacheServiceProvider;
use Vormkracht10\PermanentCache\PermanentCacheServiceProvider;

class TestCase extends Orchestra
{
Expand All @@ -13,14 +13,14 @@ protected function setUp(): void
parent::setUp();

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Vormkracht10\\PermamentCache\\Database\\Factories\\'.class_basename($modelName).'Factory'
fn (string $modelName) => 'Vormkracht10\\PermanentCache\\Database\\Factories\\'.class_basename($modelName).'Factory'
);
}

protected function getPackageProviders($app)
{
return [
PermamentCacheServiceProvider::class,
PermanentCacheServiceProvider::class,
];
}

Expand Down

0 comments on commit 6c99e10

Please sign in to comment.