/** * @param Exchange $exchange * * @return $this */ public function addExchange(Exchange $exchange) { if ($this->hasExchange($exchange->name())) { throw new \InvalidArgumentException(sprintf('Exchange "%s" already defined', $exchange->name())); } $this->exchanges[$exchange->name()] = $exchange; return $this; }
/** */ public function initialize() { if ($this->initialized) { return; } $this->initialized = true; $this->exchange->initialize(); $this->queue->initialize(); $this->channel->queue_bind($this->queue->name(), $this->exchange->name()); }
/** * {@inheritdoc} */ public function register(Container $pimple) { $pimple['rabbitmq'] = array(); $pimple['rabbitmq.config'] = function () use($pimple) { return array_merge(array('host' => 'localhost', 'port' => 5672, 'user' => 'guest', 'password' => 'guest', 'vhost' => '/', 'prefetch_count' => null), $pimple['rabbitmq']); }; $pimple['rabbitmq.connection'] = function () use($pimple) { $config = $pimple['rabbitmq.config']; return new Connection($config['host'], $config['port'], $config['user'], $config['password'], $config['vhost']); }; $pimple['rabbitmq.channel'] = function () use($pimple) { $config = $pimple['rabbitmq.config']; $channel = $pimple['rabbitmq.connection']->channel(); if ($config['prefetch_count'] !== null) { $channel->basic_qos(null, $config['prefetch_count'], true); } return $channel; }; $pimple['rabbitmq.manager'] = function () use($pimple) { $manager = new Manager($pimple['rabbitmq.channel']); $config = array_merge(array('exchanges' => array(), 'queues' => array(), 'bindings' => array()), $pimple['rabbitmq']); foreach ($config['exchanges'] as $name => $parameters) { $manager->addExchange(Exchange::configure($pimple['rabbitmq.channel'], $name, $parameters ?: array())); } foreach ($config['queues'] as $name => $parameters) { $manager->addQueue(Queue::configure($pimple['rabbitmq.channel'], $name, $parameters ?: array())); } foreach ($config['bindings'] as $parameters) { if (!isset($parameters['exchange']) || !isset($parameters['queue'])) { throw new \InvalidArgumentException("Every binding should have 'exchange' and 'queue' parameters"); } $manager->bind($parameters['exchange'], $parameters['queue']); } return $manager; }; }