Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eloquence 11.0 #112

Merged
merged 27 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d7c5ffb
Removed the UUID code.
kirkbushell Aug 26, 2023
15b8852
Removed now redundant logic check in the getAttribute method.
kirkbushell Aug 26, 2023
af24d0c
Made Eloquence auto-discoverable by Laravel.
kirkbushell Aug 26, 2023
c24a0f3
Updated the readme for package auto-discovery.
kirkbushell Aug 26, 2023
f49a0b0
Updated the cache count system to use the model objects instead of th…
kirkbushell Aug 26, 2023
5045704
Moved config method to cacheable, started setting up summable refactor.
kirkbushell Aug 26, 2023
bec7a64
Sum cache now uses new system.
kirkbushell Aug 26, 2023
4f3f330
Got model restoration working on sumcache.
kirkbushell Aug 26, 2023
6f273db
Updated slug test wording.
kirkbushell Aug 26, 2023
18362db
Added additional tests for the sum cache.
kirkbushell Aug 27, 2023
4a4de05
Fixing a few bugs, cleaning up tests by adding factories, making it m…
kirkbushell Aug 27, 2023
4268ae6
Setting up the query log utility feature, making it super easy for de…
kirkbushell Aug 27, 2023
ea43099
Got PHP attributes working as a cleaner approach.
kirkbushell Aug 27, 2023
52201f2
Refactored sum cache test to use factories.
kirkbushell Aug 27, 2023
5096d9e
More styling updates, removed redundant code, updated readme.
kirkbushell Aug 27, 2023
aa3884e
Little more updates to the readme for ultra-clear explanation of use.
kirkbushell Aug 27, 2023
7c7a80c
Added sum and count cache rebuild feature.
kirkbushell Nov 29, 2023
a75c626
Cache rebuild command added.
kirkbushell Nov 29, 2023
d19f31e
Some fixes on the rebuild cache command.
kirkbushell Nov 29, 2023
f8e478f
Fixed a bug in the rebuild caches test, addressed feedback and sugges…
kirkbushell Jan 31, 2024
f43eba5
Removed ramsey/uuid and removed unused code.
kirkbushell Jan 31, 2024
8674e5f
Renamed traits, fixed tests, removed redundant commentary.
kirkbushell Jan 31, 2024
7a0954a
Removed empty test.
kirkbushell Jan 31, 2024
200e303
Updated the readme.
kirkbushell Jan 31, 2024
5cab32b
Removed unneccessary code.
kirkbushell Jan 31, 2024
e2f92a8
Updated documentation with details for the upgrade guide.
kirkbushell Jan 31, 2024
8ae46f4
Ran a linter and fixed any PSR issues.
kirkbushell Jan 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Got PHP attributes working as a cleaner approach.
  • Loading branch information
kirkbushell committed Aug 27, 2023
commit ea43099bad6139382e848675e57f9c3b8d067d0d
23 changes: 23 additions & 0 deletions src/Behaviours/Cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
namespace Eloquence\Behaviours;

use Closure;
use Eloquence\Behaviours\SumCache\SummedBy;
use Illuminate\Database\Eloquent\Model;
use ReflectionClass;
use ReflectionMethod;

/**
* The cacheable trait is concerned with the related models.
Expand All @@ -24,6 +27,26 @@ public static function for(Model $model): self
return new self($model);
}

public function reflect(string $attributeClass, \Closure $fn)
{
$reflect = new ReflectionClass($this->model);

// This behemoth cycles through all valid methods, and then gets only the attributes we care about,
// formatting it in a way that is usable by our various aggregate service classes.
return collect($reflect->getMethods())
->filter(fn(ReflectionMethod $method) => count($method->getAttributes($attributeClass)) > 0)
->flatten()
->map(function(ReflectionMethod $method) use ($attributeClass) {
return collect($method->getAttributes($attributeClass))->map(fn(\ReflectionAttribute $attribute) => [
'name' => $method->name,
'attribute' => $attribute->newInstance(),
])->toArray();
})
->flatten(1)
->mapWithKeys($fn)
->toArray();
}

/**
* Applies the provided function using the relevant configuration to all configured relations. Configuration
* would be one of countedBy, summedBy, averagedBy.etc.
Expand Down
15 changes: 7 additions & 8 deletions src/Behaviours/CountCache/CountCache.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

namespace Eloquence\Behaviours\CountCache;

use Eloquence\Behaviours\Cacheable;
use Eloquence\Behaviours\CacheConfig;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

/**
Expand All @@ -12,11 +14,14 @@ class CountCache
{
use Cacheable;

private function __construct(private Countable $model) {}
private function __construct(private Model $model) {}

private function configuration(): array
{
return $this->model->countedBy();
return $this->reflect(CountedBy::class, function(array $config) {
$aggregateField = $config['attribute']->as ?? Str::lower(Str::snake(class_basename($this->model))).'_count';
return [$config['name'] => $aggregateField];
});
}

/**
Expand Down Expand Up @@ -68,12 +73,6 @@ public function decrement(): void
*/
protected function config($key, string $value): CacheConfig
{
// If the key is numeric, it means only the relationship method has been referenced.
if (is_numeric($key)) {
$key = $value;
$value = Str::lower(Str::snake(class_basename($this->model))).'_count';
}

return new CacheConfig($key, $value);
}
}
21 changes: 0 additions & 21 deletions src/Behaviours/CountCache/Countable.php

This file was deleted.

9 changes: 9 additions & 0 deletions src/Behaviours/CountCache/CountedBy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Eloquence\Behaviours\CountCache;

#[\Attribute]
class CountedBy
{
public function __construct(readonly ?string $as = null) {}
}
5 changes: 1 addition & 4 deletions src/Behaviours/CountCache/Observer.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php
namespace Eloquence\Behaviours\CountCache;

use Closure;

/**
* The Observer is used for watching for model updates and making the appropriate changes
* as required. This includes watching for created, deleted, updated and restored events.
*/
class Observer
{

/**
* When the model has been created, increment the count cache by 1.
*
Expand Down Expand Up @@ -55,7 +52,7 @@ public function restored($model): void
*
* @param string $operation + or -
*/
private function update(Countable $model, string $operation): void
private function update($model, string $operation): void
{
$countCache = CountCache::for($model);

Expand Down
10 changes: 5 additions & 5 deletions src/Behaviours/SumCache/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

class Observer
{
public function created(Summable $model)
public function created($model)
{
SumCache::for($model)->increase();
}

public function updated(Summable $model)
public function updated($model)
{
SumCache::for($model)->update();
}

public function deleted(Summable $model)
public function deleted($model)
{
SumCache::for($model)->decrease();
}

public function restored(Summable $model)
public function restored($model)
{

// @TODO
}
}
22 changes: 10 additions & 12 deletions src/Behaviours/SumCache/SumCache.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<?php

namespace Eloquence\Behaviours\SumCache;

use Eloquence\Behaviours\Cacheable;
use Eloquence\Behaviours\CacheConfig;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use ReflectionMethod;

class SumCache
{
use Cacheable;

private function __construct(private Summable $model) {}
private function __construct(private Model $model) {}

private function configuration(): array
{
return $this->model->summedBy();
return $this->reflect(SummedBy::class, function(array $config) {
return [$config['name'] => [$config['attribute']->as => $config['attribute']->from]];
});
}

/**
Expand Down Expand Up @@ -68,14 +70,10 @@ public function update(): void
*/
protected function config($relation, string|array $sourceField): CacheConfig
{
if (is_array($sourceField)) {
$keys = array_keys($sourceField);
$aggregateField = $keys[0];
$sourceField = $sourceField[$aggregateField];
}
else {
$aggregateField = 'total_'.$sourceField;
}
$keys = array_keys($sourceField);

$aggregateField = $keys[0];
$sourceField = $sourceField[$aggregateField];

return new CacheConfig($relation, $aggregateField, $sourceField);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Behaviours/SumCache/SummedBy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Eloquence\Behaviours\SumCache;

#[\Attribute]
class SummedBy
{
public function __construct(readonly string $from, readonly string $as) {}
}
6 changes: 3 additions & 3 deletions src/Commands/FindCacheableClasses.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace Eloquence\Commands;
<?php

namespace Eloquence\Commands;

use Eloquence\Behaviours\CountCache\Countable;
use Eloquence\Behaviours\SumCache\Summable;
use hanneskod\classtools\Iterator\ClassIterator;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\Finder\Finder;
Expand Down
25 changes: 12 additions & 13 deletions src/Commands/RebuildCaches.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

namespace Eloquence\Commands;

use Eloquence\Behaviours\CountCache\Countable;
use Eloquence\Behaviours\CountCache\CountCache;
use Eloquence\Behaviours\SumCache\SumCache;
use Eloquence\Behaviours\SumCache\Summable;
use hanneskod\classtools\Iterator\ClassIterator;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -48,17 +47,17 @@ private function rebuild(string $className): void
{
$model = new $className;

if ($model instanceof Countable) {
$this->info("Rebuilding [$className] count caches");
$countCache = CountCache::for($model);
$countCache->rebuild();
}

if ($model instanceof Summable) {
$this->info("Rebuilding [$className] sum caches");
$sumCache = SumCache::for($model);
$sumCache->rebuild();
}
// if ($model instanceof Countable) {
// $this->info("Rebuilding [$className] count caches");
// $countCache = CountCache::for($model);
// $countCache->rebuild();
// }
//
// if ($model instanceof Summable) {
// $this->info("Rebuilding [$className] sum caches");
// $sumCache = SumCache::for($model);
// $sumCache->rebuild();
// }
}

/**
Expand Down
1 change: 0 additions & 1 deletion tests/Acceptance/CountCacheTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Tests\Acceptance;

use Tests\Acceptance\Models\Category;
use Tests\Acceptance\Models\Comment;
use Tests\Acceptance\Models\Post;
use Tests\Acceptance\Models\User;
Expand Down
11 changes: 4 additions & 7 deletions tests/Acceptance/Models/Comment.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Tests\Acceptance\Models;

use Eloquence\Behaviours\CountCache\Countable;
use Eloquence\Behaviours\CountCache\CountedBy;
use Eloquence\Behaviours\CountCache\HasCounts;
use Eloquence\Behaviours\CamelCased;
use Illuminate\Database\Eloquent\Factories\Factory;
Expand All @@ -10,7 +10,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Comment extends Model implements Countable
class Comment extends Model
{
use CamelCased;
use HasCounts;
Expand All @@ -22,21 +22,18 @@ class Comment extends Model implements Countable
'post_id',
];

#[CountedBy]
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}

#[CountedBy]
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

public function countedBy(): array
{
return ['post', 'user'];
}

protected static function newFactory(): Factory
{
return CommentFactory::new();
Expand Down
10 changes: 3 additions & 7 deletions tests/Acceptance/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@

use Eloquence\Behaviours\SumCache\HasSums;
use Eloquence\Behaviours\CamelCased;
use Eloquence\Behaviours\SumCache\Summable;
use Eloquence\Behaviours\SumCache\SummedBy;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Item extends Model implements Summable
class Item extends Model
{
use CamelCased;
use HasSums;
use HasFactory;

public function summedBy(): array
{
return ['order' => 'amount'];
}

#[SummedBy('amount', 'total_amount')]
public function order()
{
return $this->belongsTo(Order::class);
Expand Down
Loading