WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 2ef2c6e

Browse files
authored
Merge branch 'GeneaLabs:master' into master
2 parents efc1a0d + 631bb7f commit 2ef2c6e

File tree

14 files changed

+176
-127
lines changed

14 files changed

+176
-127
lines changed

.github/workflows/laravel.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ jobs:
1414
strategy:
1515
fail-fast: true
1616
matrix:
17-
php: [8.0, 8.1]
17+
php: [8.2, 8.1]
1818

1919
name: PHP ${{ matrix.php }}
2020

2121
steps:
22-
- uses: actions/checkout@v2
22+
- name: Checkout code
23+
uses: actions/checkout@v3
2324

2425
- name: Cache dependencies
2526
uses: actions/cache@v1

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.12.5] - 2022-07-30
8+
### Added
9+
- pivotSynced event listener to flush cache when performing BelongsToMany::sync
10+
11+
[...]
12+
713
## [0.10.3] - 2021-03-16
814
### Changed
915
- The from part of the query ($query->from) instead of the table name of the model ($model->table)

composer.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
}
1616
],
1717
"require": {
18-
"php": "^8.0",
19-
"genealabs/laravel-pivot-events": "^9.0",
20-
"illuminate/cache": "^9.0",
21-
"illuminate/config": "^9.0",
22-
"illuminate/console": "^9.0",
23-
"illuminate/container": "^9.0",
24-
"illuminate/database": "^9.0",
25-
"illuminate/http": "^9.0",
26-
"illuminate/support": "^9.0"
18+
"php": "^8.1",
19+
"genealabs/laravel-pivot-events": "^10.0",
20+
"illuminate/cache": "^10.0",
21+
"illuminate/config": "^10.0",
22+
"illuminate/console": "^10.0",
23+
"illuminate/container": "^10.0",
24+
"illuminate/database": "^10.0",
25+
"illuminate/http": "^10.0",
26+
"illuminate/support": "^10.0"
2727
},
2828
"require-dev": {
2929
"doctrine/dbal": "^3.3",
3030
"fakerphp/faker": "^1.11",
3131
"laravel/nova": "^3.9",
32-
"orchestra/testbench-browser-kit": "^7.0",
33-
"orchestra/testbench": "^7.0",
32+
"orchestra/testbench-browser-kit": "^8.0",
33+
"orchestra/testbench": "^8.0",
3434
"php-coveralls/php-coveralls" : "^2.2",
3535
"phpmd/phpmd": "^2.11",
3636
"phpunit/phpunit": "^9.5",

src/CacheKey.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Exception;
44
use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
5+
use Illuminate\Database\Query\Expression;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Str;
@@ -101,6 +102,14 @@ protected function getColumnClauses(array $where) : string
101102
if ($where["type"] !== "Column") {
102103
return "";
103104
}
105+
106+
if ($where["first"] instanceof Expression) {
107+
$where["first"] = $this->expressionToString($where["first"]);
108+
}
109+
110+
if ($where["second"] instanceof Expression) {
111+
$where["second"] = $this->expressionToString($where["second"]);
112+
}
104113

105114
return "-{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}";
106115
}
@@ -205,14 +214,19 @@ protected function getOrderByClauses() : string
205214
}
206215

207216
$orders = collect($this->query->orders);
208-
217+
209218
return $orders
210219
->reduce(function ($carry, $order) {
211220
if (($order["type"] ?? "") === "Raw") {
212221
return $carry . "_orderByRaw_" . (new Str)->slug($order["sql"]);
213222
}
214223

215-
return $carry . "_orderBy_" . $order["column"] . "_" . $order["direction"];
224+
return sprintf(
225+
'%s_orderBy_%s_%s',
226+
$carry,
227+
$this->expressionToString($order["column"]),
228+
$order["direction"]
229+
);
216230
})
217231
?: "";
218232
}
@@ -246,7 +260,11 @@ protected function getQueryColumns(array $columns) : string
246260
if (property_exists($this->query, "columns")
247261
&& $this->query->columns
248262
) {
249-
return "_" . implode("_", $this->query->columns);
263+
$columns = array_map(function ($column) {
264+
return $this->expressionToString($column);
265+
}, $this->query->columns);
266+
267+
return "_" . implode("_", $columns);
250268
}
251269

252270
return "_" . implode("_", $columns);
@@ -315,14 +333,18 @@ protected function getValuesFromWhere(array $where) : string
315333
}
316334

317335
if (is_array((new Arr)->get($where, "values"))) {
318-
return implode("_", collect($where["values"])->flatten()->toArray());
336+
$values = collect($where["values"])->flatten()->toArray();
337+
return implode("_", $this->processEnums($values));
319338
}
320339

321340
if (is_array((new Arr)->get($where, "value"))) {
322-
return implode("_", collect($where["value"])->flatten()->toArray());
341+
$values = collect($where["value"])->flatten()->toArray();
342+
return implode("_", $this->processEnums($values));
323343
}
324344

325-
return (new Arr)->get($where, "value", "");
345+
$value = (new Arr)->get($where, "value", "");
346+
347+
return $this->processEnum($value);
326348
}
327349

328350
protected function getValuesFromBindings(array $where, string $values) : string
@@ -423,4 +445,31 @@ protected function recursiveImplode(array $items, string $glue = ",") : string
423445

424446
return $result;
425447
}
448+
449+
private function processEnum(\BackedEnum|\UnitEnum|Expression|string $value): string
450+
{
451+
if ($value instanceof \BackedEnum) {
452+
return $value->value;
453+
} elseif ($value instanceof \UnitEnum) {
454+
return $value->name;
455+
} elseif ($value instanceof Expression) {
456+
return $this->expressionToString($value);
457+
}
458+
459+
return $value;
460+
}
461+
462+
private function processEnums(array $values): array
463+
{
464+
return array_map(fn($value) => $this->processEnum($value), $values);
465+
}
466+
467+
private function expressionToString(Expression|string $value): string
468+
{
469+
if (is_string($value)) {
470+
return $value;
471+
}
472+
473+
return $value->getValue($this->query->getConnection()->getQueryGrammar());
474+
}
426475
}

src/Traits/Buildable.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ public function paginate(
158158
$perPage = null,
159159
$columns = ["*"],
160160
$pageName = "page",
161-
$page = null
161+
$page = null,
162+
$total = null
162163
) {
163164
if (! $this->isCachable()) {
164165
return parent::paginate($perPage, $columns, $pageName, $page);
@@ -169,8 +170,18 @@ public function paginate(
169170
if (is_array($page)) {
170171
$page = $this->recursiveImplodeWithKey($page);
171172
}
173+
172174
$columns = collect($columns)->toArray();
173-
$cacheKey = $this->makeCacheKey($columns, null, "-paginate_by_{$perPage}_{$pageName}_{$page}");
175+
$keyDifferentiator = "-paginate_by_{$perPage}_{$pageName}_{$page}";
176+
177+
if ($total !== null) {
178+
$total = value($total);
179+
$keyDifferentiator .= $total !== null
180+
? "_{$total}"
181+
: "";
182+
}
183+
184+
$cacheKey = $this->makeCacheKey($columns, null, $keyDifferentiator);
174185

175186
return $this->cachedValue(func_get_args(), $cacheKey);
176187
}

src/Traits/ModelCaching.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,19 @@ public static function bootCachable()
7575
// $instance->checkCooldownAndFlushAfterPersisting($instance);
7676
// });
7777

78-
static::pivotAttached(function ($instance, $secondInstance, $relationship) {
78+
static::pivotSynced(function ($instance, $relationship) {
7979
$instance->checkCooldownAndFlushAfterPersisting($instance, $relationship);
8080
});
8181

82-
static::pivotDetached(function ($instance, $secondInstance, $relationship) {
82+
static::pivotAttached(function ($instance, $relationship) {
8383
$instance->checkCooldownAndFlushAfterPersisting($instance, $relationship);
8484
});
8585

86-
static::pivotUpdated(function ($instance, $secondInstance, $relationship) {
86+
static::pivotDetached(function ($instance, $relationship) {
87+
$instance->checkCooldownAndFlushAfterPersisting($instance, $relationship);
88+
});
89+
90+
static::pivotUpdated(function ($instance, $relationship) {
8791
$instance->checkCooldownAndFlushAfterPersisting($instance, $relationship);
8892
});
8993
}

tests/CreatesApplication.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,19 @@ protected function getEnvironmentSetUp($app)
138138
'pagination' => 'simple',
139139
]);
140140
}
141+
142+
public function appVersionEightAndUp(): bool
143+
{
144+
return version_compare(app()->version(), '8.0.0', '>=');
145+
}
146+
147+
public function appVersionFiveBetweenSeven(): bool
148+
{
149+
return version_compare(app()->version(), '5.6.0', '>=') && version_compare(app()->version(), '8.0.0', '<');
150+
}
151+
152+
public function appVersionOld(): bool
153+
{
154+
return version_compare(app()->version(), '5.4.0', '>=') && version_compare(app()->version(), '5.6.0', '<');
155+
}
141156
}

tests/Feature/PaginationTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ class PaginationTest extends FeatureTestCase
88
public function testPaginationProvidesDifferentLinksOnDifferentPages()
99
{
1010
// Checking the version start with 8.0.
11-
if (preg_match("/^([8|9]\.)/", app()->version())) {
11+
if ($this->appVersionEightAndUp()) {
1212
$page1ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">1</span>';
1313
$page2ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">2</span>';
1414
}
1515

16-
// Checking the version start with 5.6, 5.7, 5.8 or 6.
17-
if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) {
16+
// Checking the version start with 5.6, 5.7, 5.8, 6 or 7.
17+
if ($this->appVersionFiveBetweenSeven()) {
1818
$page1ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
1919
$page2ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
2020
}
2121

2222
// Checking the version 5.4 and 5.5
23-
if (preg_match("/^5\.[4-5]/", app()->version())) {
23+
if ($this->appVersionOld()) {
2424
$page1ActiveLink = '<li class="active"><span>1</span></li>';
2525
$page2ActiveLink = '<li class="active"><span>2</span></li>';
2626
}
@@ -40,17 +40,17 @@ public function testPaginationProvidesDifferentLinksOnDifferentPages()
4040
public function testAdvancedPagination()
4141
{
4242
// Checking the version start with 8.0.
43-
if (preg_match("/^([8|9]\.)/", app()->version())) {
43+
if ($this->appVersionEightAndUp()) {
4444
$page1ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">1</span>';
4545
$page2ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">2</span>';
4646
}
4747

48-
if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) {
48+
if ($this->appVersionFiveBetweenSeven()) {
4949
$page1ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
5050
$page2ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
5151
}
5252

53-
if (preg_match("/^5\.[4-5]/", app()->version())) {
53+
if ($this->appVersionOld()) {
5454
$page1ActiveLink = '<li class="active"><span>1</span></li>';
5555
$page2ActiveLink = '<li class="active"><span>2</span></li>';
5656
}
@@ -63,17 +63,17 @@ public function testAdvancedPagination()
6363
public function testCustomPagination()
6464
{
6565
// Checking the version start with 8.0.
66-
if (preg_match("/^([8|9]\.)/", app()->version())) {
66+
if ($this->appVersionEightAndUp()) {
6767
$page1ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">1</span>';
6868
$page2ActiveLink = '<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5">2</span>';
6969
}
7070

71-
if (preg_match("/^((5\.[6-8])|(6\.)|(7\.))/", app()->version())) {
71+
if ($this->appVersionFiveBetweenSeven()) {
7272
$page1ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
7373
$page2ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
7474
}
7575

76-
if (preg_match("/^5\.[4-5]/", app()->version())) {
76+
if ($this->appVersionOld()) {
7777
$page1ActiveLink = '<li class="active"><span>1</span></li>';
7878
$page2ActiveLink = '<li class="active"><span>2</span></li>';
7979
}

tests/Fixtures/Book.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ class Book extends Model
1414

1515
protected $casts = [
1616
'price' => 'float',
17+
'published_at' => 'datetime',
1718
];
18-
protected $dates = [
19-
'published_at',
20-
];
19+
2120
protected $fillable = [
2221
"author_id",
2322
'description',

tests/Fixtures/BookWithUncachedStore.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ class BookWithUncachedStore extends Model
1414

1515
protected $casts = [
1616
'price' => 'float',
17+
'published_at' => 'datetime',
1718
];
18-
protected $dates = [
19-
'published_at',
20-
];
19+
2120
protected $fillable = [
2221
"author_id",
2322
'description',

0 commit comments

Comments
 (0)