-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCacheQueryServiceProvider.php
More file actions
125 lines (105 loc) · 3.86 KB
/
CacheQueryServiceProvider.php
File metadata and controls
125 lines (105 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Laragear\CacheQuery;
use Closure;
use DateInterval as Interval;
use DateTimeInterface as DateTime;
use Illuminate\Database\ConnectionInterface as DBConnection;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use function app;
use function base64_encode;
use function explode;
use function implode;
use function md5;
use function rtrim;
use function sort;
/**
* @internal
*/
class CacheQueryServiceProvider extends ServiceProvider
{
public const CONFIG = __DIR__.'/../config/cache-query.php';
public const STUBS = __DIR__.'/../.stubs/stubs';
/**
* Register the service provider.
*
* @return void
*/
public function register(): void
{
$this->mergeConfigFrom(static::CONFIG, 'cache-query');
}
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot(): void
{
if (! Builder::hasMacro('cache')) {
Builder::macro('cache', $this->macro());
}
if (! EloquentBuilder::hasGlobalMacro('cache')) {
EloquentBuilder::macro('cache', $this->eloquentMacro());
}
if ($this->app->runningInConsole()) {
$this->publishes([static::CONFIG => $this->app->configPath('cache-query.php')], 'config');
$this->publishes([static::STUBS => $this->app->basePath('.stubs/cache-query.php')], 'phpstorm');
$this->commands([
Console\Commands\CacheQuery\Forget::class,
]);
}
Proxy::$queryHasher = static function (DBConnection $connection, string $query, array $bindings): string {
// If the commutative operations is enabled, we will normalize the query and bindings.
if (app('config')->get('cache-query.commutative')) {
$query = Collection::make(explode(' ', $query))->sort()->implode('');
sort($bindings);
}
return rtrim(base64_encode(md5($connection->getDatabaseName().$query.implode('', $bindings), true)), '=');
};
}
/**
* Creates a macro for the base Query Builder.
*
* @return \Closure
*/
protected function macro(): Closure
{
return function (DateTime|Interval|Closure|Cache|int|bool|array|string|null $ttl = 60): Builder {
/** @var \Illuminate\Database\Query\Builder $this */
// Avoid re-wrapping the connection into another proxy.
if ($this->connection instanceof Proxy) { // @phpstan-ignore-line
$this->connection = $this->connection->connection;
}
$cache = new Cache();
match (true) {
$ttl instanceof Closure => $ttl($cache),
! $ttl instanceof Cache => $cache->ttl($ttl),
default => $cache = $ttl
};
// Normalize the TTL argument to a Cache instance.
$this->connection = Proxy::crateNewInstance($this->connection, $cache);
return $this;
};
}
/**
* Creates a macro for the base Query Builder.
*
* @return \Closure
*/
protected function eloquentMacro(): Closure
{
return function (DateTime|Interval|Closure|Cache|int|bool|array|string|null $ttl = 60): EloquentBuilder {
/** @var \Illuminate\Database\Eloquent\Builder $this */
$this->getQuery()->cache($ttl); // @phpstan-ignore-line
// @phpstan-ignore-next-line
if ($this->getQuery()->getConnection()->getCacheHelperInstance()->saveNestedQueries) {
// This global scope is responsible for caching eager loaded relations.
$this->withGlobalScope(Scopes\CacheRelations::class, new Scopes\CacheRelations());
}
return $this;
};
}
}