Skip to content

Commit 4221b1e

Browse files
committed
“ People find pleasure in different ways. I find it in keeping my mind clear. ”
— Marcus Aurelius
1 parent 082d58d commit 4221b1e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

config/ecow.php

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
return [
4+
5+
/*
6+
* Enable or disable the event listeners.
7+
*/
8+
'enabled' => env('ECOW_ENABLED', true),
9+
410
/*
511
* The model used to store saved models.
612
*/

src/Ecow.php

+35-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Ecow
2929

3030
public array $modelsBeingSaved = [];
3131

32+
public bool $disabled = false;
33+
3234
public function modelClass(): string
3335
{
3436
$modelClass = config('ecow.model');
@@ -155,6 +157,14 @@ public function getAttributes(mixed $model): array
155157

156158
public function bootListeners(): void
157159
{
160+
if ($this->isDisabled()) {
161+
return;
162+
}
163+
164+
if (false === config('ecow.enabled', true)) {
165+
return;
166+
}
167+
158168
$this->listenForCreatingEvents();
159169
$this->listenForUpdatingEvents();
160170
$this->listenForDeletingEvents();
@@ -211,8 +221,12 @@ public function listen(string $event, array $pipes): void
211221
});
212222
}
213223

214-
protected function eventPipeline(string $event, array $payload, $events): mixed
224+
protected function eventPipeline(string $event, array $payload, $events)
215225
{
226+
if ($this->isDisabled()) {
227+
return;
228+
}
229+
216230
$model = $payload[0];
217231

218232
$data = (object) [
@@ -369,4 +383,24 @@ protected function info($message)
369383
{
370384
event('ecow.info', ['payload' => ['message' => $message]]);
371385
}
386+
387+
public function disable(): void
388+
{
389+
$this->disabled = true;
390+
}
391+
392+
public function enable(): void
393+
{
394+
$this->disabled = false;
395+
}
396+
397+
public function isDisabled(): bool
398+
{
399+
return $this->disabled;
400+
}
401+
402+
public function isNotDisabled(): bool
403+
{
404+
return ! $this->isDisabled();
405+
}
372406
}

0 commit comments

Comments
 (0)