Skip to content

Commit

Permalink
Ensure dispatcher exists
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Mar 5, 2024
1 parent 97bb182 commit 7c5beb5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "putyourlightson/craft-snaptcha",
"description": "Automatically validates forms and prevents spam bots from submitting to your site.",
"version": "4.0.1",
"version": "4.0.2",
"type": "craft-plugin",
"homepage": "https://putyourlightson.com/plugins/snaptcha",
"license": "proprietary",
Expand Down
29 changes: 16 additions & 13 deletions src/Snaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use yii\base\ActionEvent;
use yii\base\Controller as BaseController;
use yii\base\Event;
use yii\log\Dispatcher;
use yii\log\Logger;

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public function log(string $message, array $params = [], int $type = Logger::LEV
private function _registerVariables(): void
{
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT,
function (Event $event) {
function(Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('snaptcha', SnaptchaVariable::class);
Expand All @@ -108,17 +109,19 @@ function (Event $event) {
*/
private function _registerLogTarget(): void
{
Craft::getLogger()->dispatcher->targets[] = new MonologTarget([
'name' => 'snaptcha',
'categories' => ['snaptcha'],
'level' => LogLevel::INFO,
'logContext' => false,
'allowLineBreaks' => false,
'formatter' => new LineFormatter(
format: "[%datetime%] %message%\n",
dateFormat: 'Y-m-d H:i:s',
),
]);
if (Craft::getLogger()->dispatcher instanceof Dispatcher) {
Craft::getLogger()->dispatcher->targets[] = new MonologTarget([
'name' => 'snaptcha',
'categories' => ['snaptcha'],
'level' => LogLevel::INFO,
'logContext' => false,
'allowLineBreaks' => false,
'formatter' => new LineFormatter(
format: "[%datetime%] %message%\n",
dateFormat: 'Y-m-d H:i:s',
),
]);
}
}

/**
Expand All @@ -128,7 +131,7 @@ private function _registerActionEvents(): void
{
// Register action event
Event::on(Controller::class, BaseController::EVENT_BEFORE_ACTION,
function (ActionEvent $event) {
function(ActionEvent $event) {
$this->validateField($event);
}
);
Expand Down

0 comments on commit 7c5beb5

Please sign in to comment.