@param mixed $message
@param string $expr
@param callable $handler

@internal
320667
public function handleSubscribeCallback($message, $expr, callable $handler)
    {
        $event = $this->translator->translate($message);
        if ($event) {
            // we were able to translate the message into an event
            if ($event->matches($expr)) {
                // the event matches the listen expression
                call_user_func($handler, $event);
            } else {
                // pass to listen expr fail handler?
                if ($this->listenExprFailHandler) {
                    call_user_func($this->listenExprFailHandler, $event, $expr);
                }
            }
        } else {
            // pass to translate fail handler?
            if ($this->translateFailHandler) {
                call_user_func($this->translateFailHandler, $message);
            }
        }
    }
914314
public function setExpression(string $expr) {
		$this->expression = $expr;
		$this->hasExpression = true;

		return $this;
	}
676822
protected function checkExpression(DOMNode $node, $expr, Tag $tag)
	{
		$this->checkContext($node);

		if (preg_match('/^\\$(\\w+)$/', $expr, $m))
		{
			// Either this expression came from a variable that is considered safe, or it's a
			// stylesheet parameters, which are considered safe by default
			$this->checkVariable($node, $tag, $m[1]);
		}
		elseif (preg_match('/^@[-\\w]+(?:\\s*\\|\\s*@[-\\w]+)*$/', $expr))
		{
			$this->checkAttributeExpression($node, $tag, $expr);
		}
		elseif (!$this->isExpressionSafe($expr))
		{
			throw new UnsafeTemplateException("Cannot assess the safety of expression '" . $expr . "'", $node);
		}
	}
77567
protected function handleMessage($message)
    {
        $handler = $this->messageHandler;

        if (!is_callable($handler)) {
            Log::debug('No handler enabled.');

            return null;
        }

        Log::debug('Message detail:', $message->all());

        $type = $this->messageTypeMapping[ $message->get('InfoType') ];

        $response = null;

        if ($type) {
            $response = call_user_func_array($handler, [$message]);
        }

        return $response;
    }
1015068
private function invokeCallback ( Request $request, $handler ) {
      try {
        return call_user_func_array( $handler, array( $this, $request ) );
      } catch ( \Exception $ex ) {
        $this->exception = $ex;
        return call_user_func_array( $this->onException, array( $this, $request ) );
      }
    }
243624
public function handle(
    Handler $handler, $sendResponse = true, $catchExceptions = true, $flushHeaders = true, $throwEventExceptions = false
  )
  {
    $this->_eventChannel->setShouldThrowExceptions($throwEventExceptions);
    $c = $this->getContext();
    if($handler instanceof ContextAware)
    {
      $handler->setContext($c);
    }

    try
    {
      $this->_eventChannel->trigger(PreExecuteEvent::i($c, $handler));
      $r = $handler->handle($c);
      $this->_eventChannel->trigger(ResponsePrepareEvent::i($c, $handler, $r));
      $r->prepare($c->request());
      $this->_eventChannel->trigger(ResponsePreparedEvent::i($c, $handler, $r));
    }
    catch(\Throwable $e)
    {
      if(!$catchExceptions)
      {
        throw $e;
      }
      $this->getLogger()->error($e->getMessage());
      $r = (new ExceptionHandler($e))->handle($c);
      $r->prepare($c->request());
    }
    try
    {
      if($sendResponse)
      {
        $this->_eventChannel->trigger(ResponsePreSendHeadersEvent::i($c, $handler, $r));
        $r->sendHeaders();
        if($flushHeaders)
        {
          flush();
        }
        $this->_eventChannel->trigger(ResponsePreSendContentEvent::i($c, $handler, $r));
        $r->send();
      }
      $this->_eventChannel->trigger(HandleCompleteEvent::i($c, $handler, $r));
    }
    catch(\Throwable $e)
    {
      if(!$catchExceptions)
      {
        throw $e;
      }
      $this->getLogger()->error($e->getMessage());
    }
    return $r;
  }
231295
protected function setGenericMessageHandler(GenericMessageHandler $handler): void
	{
		$handler->setBot($this);
		$this->genericMessageHandler = $handler;
	}
443959
protected function addRecord($level, $message, array $context = array())
    {
        if ($this->handlers->count() < 1) {
            throw new LoggerException('To log a record you must add at least one AbstractHandler object to handle the messages.'
            );
        }

        $record = new Record();
        $record->setLoggerName($this->name);
        $record->setMessage((string)$message);
        $record->setContext($context);
        $record->setLevel($level);
        $record->setDatetime($this->datetime("now"));

        // check if any handler will handle this message
        $canHandle = false;
        foreach ($this->handlers as $handler) {
            if ($handler->canHandle($record)) {
                $canHandle = true;
                break;
            }
        }

        if (!$canHandle) {
            return false;
        }

        /* @var $handler Webiny\HandlerAbstract */
        foreach ($this->handlers as $handler) {
            if ($handler->canHandle($record)) {
                $bubble = $handler->process(clone $record);
                if (!$bubble) {
                    break;
                }
            }
        }

        return true;
    }
297658
public function onText($regexp, \Closure $handler)
    {
        $this->managers[] = new Manager(function (Event $event) use ($regexp) {
            return (
                $event instanceof \Viber\Api\Event\Message
                && $event->getMessage() instanceof \Viber\Api\Message\Text
                && preg_match($regexp, $event->getMessage()->getText())
            );
        }, $handler);

        return $this;
    }
740470
public function selectHandler($handler)
    {
        if (!is_callable($handler)) {
            Assert::string($handler, 'The selected handler must be a callable or a string. Got: %s');

            if (!isset($this->handlers[$handler])) {
                throw new InvalidArgumentException(sprintf(
                    'The handler "%s" does not exist.',
                    $handler
                ));
            }
        }

        $this->selectedHandler = $handler;
    }
802397
public function subscribe($channel, callable $handler)
    {
        $loop = $this->client->pubSubLoop();

        $loop->subscribe($channel);

        foreach ($loop as $message) {
            /** @var \stdClass $message */
            if ($message->kind === 'message') {
                call_user_func($handler, Utils::unserializeMessagePayload($message->payload));
            }
        }

        unset($loop);
    }
954
public function callApi(Message $req, $method, $uri, $handler)
    {
        // check handler, php before 5.4 doesn't support Type Hinting of callable
        if (!is_callable($handler)) {
            throw new GeneralException("Can not find response handler.");
        }

        $data = [
            'json' => (object) $req->to_array(),
            'http_errors' => false,
        ];
        $response = $this->request($method, $uri, $data);
        $rawContent = $response->getBody()->getContents();

        $content = json_decode($rawContent, true);
        if (!$content) {
            throw new GeneralException("Response is not json data: " . $rawContent);
        }

        $statusCode = $response->getStatusCode();
        switch ($statusCode) {
            case 200:
                // happy path
                if (isset($content)) {
                    return $handler($content, "", "");
                } else {
                    throw new GeneralException("Cannot find response body: " . $rawContent);
                }
                break;
            case 400:
                // biz error
                if (isset($content)) {
                    return $handler("", $content, "");
                }
                throw new GeneralException("Cannot find Biz Error body: " . $rawContent);
                break;
            case 420:
                // common error
                if (isset($content)) {
                    return $handler("", "", $content);
                }
                throw new GeneralException("Cannot find Common Error body: " . $rawContent);
                break;
            case 500:
                // internal error
                throw new InternalServerErrorException("Internal server error: " . $rawContent);
                break;
        }
    }
574676
protected function callHandler($handler_id, $method, array $arguments)
    {
        try {
            $handlers = $this->getHandlers();
            return Handler::call($handlers, $handler_id, $method, $arguments);
        } catch (Exception $ex) {
            return $ex->getMessage();
        }
    }
1124601
public function listen($event, callable $handler)
    {
        if (empty($this->handlers[$event])) {
            $this->handlers[$event] = [];
        }

        $this->handlers[$event][] = $handler;
    }
666857
protected function handle(Handler $handler, Notification $notification)
    {
        try {
            $handler->handle($notification);
            $this->write("RESULT 2\nOK");
        } catch (EventHandlingFailed $e) {
            $this->write("RESULT 4\nFAIL");
        } catch (StopListener $e) {
            $this->listen = false;
        }
    }
382998
protected function throwValidationException(string $message, string $prefix = null)
    {
        throw new ValidationException($message, $this->name, $prefix);
    }
624720
public function incorrectQueryHandler($key, $value, $expr)
    {
        if (!is_string($value)) {
            return null;
        }
        if (strpos($value, '&&') && strpos($value, '||')) {
            throw new QueryParseException('Mixed && and || operators are not supported', 1);
        }
    }