for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace WillRy\RabbitRun\Connections;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
/**
* Class Connect Singleton Pattern
*/
class Connect
{
* @const array
private static $opt = [];
/** @var AMQPStreamConnection */
private static $instance;
/** @var AMQPChannel */
private static $channel;
* Connect constructor. Private singleton
private function __construct()
}
* Connect clone. Private singleton
private function __clone()
public static function getInstance()
if (empty(self::$instance) || (!empty(self::$instance) && !self::$instance->isConnected())) {
self::$instance = new AMQPStreamConnection(
self::$opt["host"],
self::$opt["port"],
self::$opt["user"],
self::$opt["pass"]
);
return self::$instance;
public static function getChannel()
if (empty(self::$channel) || (!empty(self::$channel) && !self::$channel->is_open())) {
try {
self::$channel = self::getInstance()->channel();
} catch (\Exception $exception) {
die('Connection error RabbitMQ' . $exception->getMessage());
exit
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.
return self::$channel;
public static function closeInstance()
if (!empty(self::$instance) && self::$instance->isConnected()) {
self::$instance->close();
public static function closeChannel()
if (!empty(self::$channel) && self::$channel->is_open()) {
self::$channel->close();
public static function config($host, $port, $user, $pass, $vhost)
self::$opt = [
'host' => $host,
'port' => $port,
'user' => $user,
'pass' => $pass,
'vhost' => $vhost,
];
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.