generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #359 from patrickomeara/main
Add conditional query watcher and convenience methods for update, delete, insert and select queries
- Loading branch information
Showing
11 changed files
with
412 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelRay\Watchers; | ||
|
||
use BadMethodCallException; | ||
use Closure; | ||
use Illuminate\Database\Events\QueryExecuted; | ||
use Illuminate\Support\Facades\Event; | ||
use Spatie\LaravelRay\Payloads\ExecutedQueryPayload; | ||
use Spatie\LaravelRay\Ray; | ||
|
||
class ConditionalQueryWatcher extends QueryWatcher | ||
{ | ||
protected $conditionalCallback; | ||
|
||
public static function buildWatcherForName(Closure $condition, $name) | ||
{ | ||
$watcher = new static(); | ||
$watcher->setConditionalCallback($condition); | ||
|
||
return app()->instance(static::abstractName($name), $watcher); | ||
} | ||
|
||
public static function abstractName(string $name) | ||
{ | ||
return static::class.':'.$name; | ||
} | ||
|
||
public function setConditionalCallback($conditionalCallback) | ||
{ | ||
$this->conditionalCallback = $conditionalCallback; | ||
|
||
$this->listen(); | ||
} | ||
|
||
public function register(): void | ||
{ | ||
throw new BadMethodCallException('ConditionalQueryWatcher cannot be registered. Only its child classes.'); | ||
} | ||
|
||
public function listen(): void | ||
{ | ||
Event::listen(QueryExecuted::class, function (QueryExecuted $query) { | ||
if (! $this->enabled()) { | ||
return; | ||
} | ||
|
||
if (! $this->conditionalCallback) { | ||
return; | ||
} | ||
|
||
$ray = app(Ray::class); | ||
|
||
if (($this->conditionalCallback)($query)) { | ||
$payload = new ExecutedQueryPayload($query); | ||
|
||
$ray->sendRequest($payload); | ||
} | ||
|
||
optional($this->rayProxy)->applyCalledMethods($ray); | ||
}); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelRay\Watchers; | ||
|
||
use Illuminate\Database\Events\QueryExecuted; | ||
use Illuminate\Support\Str; | ||
use Spatie\Ray\Settings\Settings; | ||
|
||
class DeleteQueryWatcher extends ConditionalQueryWatcher | ||
{ | ||
public function register(): void | ||
{ | ||
$settings = app(Settings::class); | ||
|
||
$this->enabled = $settings->send_delete_queries_to_ray ?? false; | ||
|
||
$this->setConditionalCallback(function (QueryExecuted $query) { | ||
return Str::startsWith(strtolower($query->sql), 'delete'); | ||
}); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelRay\Watchers; | ||
|
||
use Illuminate\Database\Events\QueryExecuted; | ||
use Illuminate\Support\Str; | ||
use Spatie\Ray\Settings\Settings; | ||
|
||
class InsertQueryWatcher extends ConditionalQueryWatcher | ||
{ | ||
public function register(): void | ||
{ | ||
$settings = app(Settings::class); | ||
|
||
$this->enabled = $settings->send_insert_queries_to_ray ?? false; | ||
|
||
$this->setConditionalCallback(function (QueryExecuted $query) { | ||
return Str::startsWith(strtolower($query->sql), 'insert'); | ||
}); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelRay\Watchers; | ||
|
||
use Illuminate\Database\Events\QueryExecuted; | ||
use Illuminate\Support\Str; | ||
use Spatie\Ray\Settings\Settings; | ||
|
||
class SelectQueryWatcher extends ConditionalQueryWatcher | ||
{ | ||
public function register(): void | ||
{ | ||
$settings = app(Settings::class); | ||
|
||
$this->enabled = $settings->send_select_queries_to_ray ?? false; | ||
|
||
$this->setConditionalCallback(function (QueryExecuted $query) { | ||
return Str::startsWith(strtolower($query->sql), 'select'); | ||
}); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelRay\Watchers; | ||
|
||
use Illuminate\Database\Events\QueryExecuted; | ||
use Illuminate\Support\Str; | ||
use Spatie\Ray\Settings\Settings; | ||
|
||
class UpdateQueryWatcher extends ConditionalQueryWatcher | ||
{ | ||
public function register(): void | ||
{ | ||
$settings = app(Settings::class); | ||
|
||
$this->enabled = $settings->send_update_queries_to_ray ?? false; | ||
|
||
$this->setConditionalCallback(function (QueryExecuted $query) { | ||
return Str::startsWith(strtolower($query->sql), 'update'); | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.