diff --git a/src/Automod/ModAction/ComplexRuleAction.php b/src/Automod/ModAction/ComplexRuleAction.php index 9610d64..bb47c49 100644 --- a/src/Automod/ModAction/ComplexRuleAction.php +++ b/src/Automod/ModAction/ComplexRuleAction.php @@ -10,6 +10,7 @@ use App\Enum\RunConfiguration; use App\Repository\ComplexRuleRepository; use App\Service\Expression\ExpressionLanguage; +use App\Service\Expression\ExpressionLanguageNotifier; use LogicException; use Rikudou\LemmyApi\LemmyApi; use Rikudou\LemmyApi\Response\Model\Person; @@ -31,6 +32,7 @@ public function __construct( private ComplexRuleRepository $ruleRepository, private ExpressionLanguage $expressionLanguage, private LemmyApi $api, + private ExpressionLanguageNotifier $notifier, ) { } @@ -58,15 +60,7 @@ public function shouldRun(object $object): bool public function takeAction(object $object, Context $context = new Context()): FurtherAction { - $this->expressionLanguage->addFunction(new ExpressionFunction( - 'notify', - fn () => throw new LogicException('Uncompilable function'), - function (array $expressionContext, string $message) use ($context): bool { - $context->addMessage($message); - return true; - } - )); - + $this->notifier->currentContext = $context; $type = ComplexRuleType::fromClass(get_class($object)); $canContinue = true; diff --git a/src/Service/Expression/ExpressionLanguageFunctions.php b/src/Service/Expression/ExpressionLanguageFunctions.php index d588785..628633e 100644 --- a/src/Service/Expression/ExpressionLanguageFunctions.php +++ b/src/Service/Expression/ExpressionLanguageFunctions.php @@ -12,6 +12,7 @@ public function __construct( private MessageBusInterface $messageBus, private ExpressionLanguage $expressionLanguage, + private ExpressionLanguageNotifier $notifier, ) { } @@ -48,6 +49,11 @@ public function getFunctions(): array $this->uncompilableFunction(), $this->catchErrorFunction(...), ), + new ExpressionFunction( + 'notify', + $this->uncompilableFunction(), + $this->notifyFunction(...), + ), ]; } @@ -114,4 +120,11 @@ private function catchErrorFunction(array $context, string $expression, string $ return $this->expressionLanguage->evaluate($onErrorExpression, $context); } } + + private function notifyFunction(array $context, string $message): bool + { + $this->notifier->currentContext?->addMessage($message); + + return true; + } } diff --git a/src/Service/Expression/ExpressionLanguageNotifier.php b/src/Service/Expression/ExpressionLanguageNotifier.php new file mode 100644 index 0000000..c20e185 --- /dev/null +++ b/src/Service/Expression/ExpressionLanguageNotifier.php @@ -0,0 +1,10 @@ +