public function callback(AMQPMessage $msg) { $routingKey = $msg->delivery_info['routing_key']; $method = 'read' . Inflector::camelize($routingKey); if (!isset($this->interpreters[$this->exchange])) { $interpreter = $this; } elseif (class_exists($this->interpreters[$this->exchange])) { $interpreter = new $this->interpreters[$this->exchange](); if (!$interpreter instanceof AmqpInterpreter) { throw new Exception(sprintf("Class '%s' is not correct interpreter class.", $this->interpreters[$this->exchange])); } } else { throw new Exception(sprintf("Interpreter class '%s' was not found.", $this->interpreters[$this->exchange])); } if (method_exists($interpreter, $method)) { $info = ['exchange' => $msg->get('exchange'), 'routing_key' => $msg->get('routing_key'), 'reply_to' => $msg->has('reply_to') ? $msg->get('reply_to') : null]; $interpreter->{$method}(Json::decode($msg->body, true), $info); } else { if (!isset($this->interpreters[$this->exchange])) { $interpreter = new AmqpInterpreter(); } $interpreter->log(sprintf("Unknown routing key '%s' for exchange '%s'.", $routingKey, $this->exchange), $interpreter::MESSAGE_ERROR); // debug the message $interpreter->log(print_r(Json::decode($msg->body, true), true), $interpreter::MESSAGE_INFO); } }
public function callback(AMQPMessage $msg) { $routingKey = $msg->get('routing_key'); $method = 'read' . Inflector::camelize($routingKey); $interpreter = isset($this->interpreters[$this->exchange]) ? $this->interpreters[$this->exchange] : (isset($this->interpreters['*']) ? $this->interpreters['*'] : null); if ($interpreter === null) { $interpreter = $this; } else { if (class_exists($interpreter)) { $interpreter = new $interpreter(); if (!$interpreter instanceof AmqpInterpreter) { throw new Exception(sprintf("Class '%s' is not correct interpreter class.", $interpreter)); } } else { throw new Exception(sprintf("Interpreter class '%s' was not found.", $interpreter)); } } if (method_exists($interpreter, $method) || is_callable([$interpreter, $method])) { $info = ['exchange' => $this->exchange, 'routing_key' => $routingKey, 'reply_to' => $msg->has('reply_to') ? $msg->get('reply_to') : null, 'delivery_tag' => $msg->get('delivery_tag')]; try { $body = Json::decode($msg->body, true); } catch (\Exception $e) { $body = $msg->body; } $interpreter->{$method}($body, $info, $this->amqp->channel); } else { if (!$interpreter instanceof AmqpInterpreter) { $interpreter = new AmqpInterpreter(); } $interpreter->log(sprintf("Unknown routing key '%s' for exchange '%s'.", $routingKey, $this->exchange), $interpreter::MESSAGE_ERROR); // debug the message $interpreter->log(print_r(Json::decode($msg->body, true), true), $interpreter::MESSAGE_INFO); } }