-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some code cleanup, and added the value cache.
- Loading branch information
1 parent
42ae6c8
commit f179a24
Showing
13 changed files
with
149 additions
and
14 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,16 @@ | ||
<?php | ||
|
||
namespace Eloquence\Behaviours\ValueCache; | ||
|
||
trait HasValues | ||
{ | ||
public static function bootHasValues(): void | ||
{ | ||
static::observe(Observer::class); | ||
} | ||
|
||
public static function rebuildValueCache(): void | ||
{ | ||
ValueCache::for(new self())->rebuild(); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Eloquence\Behaviours\ValueCache; | ||
|
||
class Observer | ||
{ | ||
public function created($model): void | ||
{ | ||
ValueCache::for($model)->updateRelated(true); | ||
} | ||
|
||
public function updated($model): void | ||
{ | ||
ValueCache::for($model)->updateRelated(false); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
namespace Eloquence\Behaviours\ValueCache; | ||
|
||
use Eloquence\Behaviours\Cacheable; | ||
use Eloquence\Behaviours\CacheConfig; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ValueCache | ||
{ | ||
use Cacheable; | ||
|
||
private function __construct(private Model $model) | ||
{ | ||
} | ||
|
||
protected function config($relationName, $sourceField): CacheConfig | ||
{ | ||
$keys = array_keys($sourceField); | ||
|
||
$aggregateField = $keys[0]; | ||
$sourceField = $sourceField[$aggregateField]; | ||
|
||
return new CacheConfig($relationName, $aggregateField, $sourceField); | ||
} | ||
|
||
public function rebuild() | ||
{ | ||
|
||
} | ||
|
||
public function updateRelated(bool $new): void | ||
{ | ||
$this->apply(function(CacheConfig $config) use ($new) { | ||
$foreignKey = $config->foreignKeyName($this->model); | ||
|
||
// We only do work if the model previously existed and the source field has changed, or the model was newly created in the database. | ||
if (!($new || $this->model->wasChanged($config->sourceField))) { | ||
return; | ||
} | ||
|
||
$relatedModel = $config->emptyRelatedModel($this->model)->find($this->model->$foreignKey); | ||
|
||
$relatedModel->{$config->aggregateField} = $this->model->{$config->sourceField}; | ||
$relatedModel->save(); | ||
}); | ||
} | ||
|
||
private function configuration(): array | ||
{ | ||
return $this->reflect(ValuedBy::class, function (array $config) { | ||
return [$config['name'] => [$config['attribute']->as => $config['attribute']->from]]; | ||
}); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Eloquence\Behaviours\ValueCache; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD)] | ||
class ValuedBy | ||
{ | ||
public function __construct(readonly string $from, readonly ?string $as) | ||
{ | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Tests\Acceptance; | ||
|
||
class ModelTest | ||
{ | ||
|
||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Tests\Acceptance; | ||
|
||
use Tests\Acceptance\Models\Category; | ||
use Tests\Acceptance\Models\Post; | ||
|
||
class ValueCacheTest extends AcceptanceTestCase | ||
{ | ||
function test_values_from_related_models_are_cached() | ||
{ | ||
$category = Category::factory()->create(); | ||
|
||
$this->assertNull($category->last_activity_at); | ||
|
||
$post = Post::factory()->create(['category_id' => $category->id, 'publish_at' => now()->subDays(mt_rand(1, 10))]); | ||
|
||
$this->assertEquals($category->fresh()->last_activity_at, $post->publish_at); | ||
} | ||
} |