Skip to content

Commit

Permalink
add amqp wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
zweifisch committed Jan 11, 2014
1 parent 09777c9 commit fdc6fc1
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions zf/components/AMQP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace zf\components;

use Exception;
use ArrayAccess;
use AMQPQueue;
use AMQPChannel;
use AMQPExchange;
use AMQPConnection;

class AMQP implements ArrayAccess
{
private $_config;
private $_queues;
private $_exchanges;
private $_channel;
private $_type;

public function __construct($host, $port, $login, $password)
{
$this->_config = compact('host', 'port', 'login', 'password');
}

public function queue($queueName)
{
if (empty($this->_queues[$queueName]))
{
$this->_queues[$queueName] = new AMQPQueue($this->channel());
$this->_queues[$queueName]->setName($queueName);
}
return $this->_queues[$queueName];
}

public function exchange($exchangeName)
{
if(empty($this->_exchanges[$exchangeName]))
{
$this->_exchange = new AMQPExchange($this->channel());
$this->_exchange->setName($exchangeName);
}
return $this->_exchange;
}

public function channel()
{
return $this->_channel
? $this->_channel
: $this->_channel = new AMQPChannel($this->connection());
}

public function connection()
{
if(empty($this->_connection))
{
$this->_connection = new AMQPConnection($this->_config);
$this->_connection->connect();
}
return $this->_connection;
}

public function __get($key)
{
if ('queue' == $key || 'exchange' == $key)
{
$this->_type = $key;
return $this;
}
else
{
$type = $this->getType($key);
return $this->$type($key);
}
}

public function __call($name, $args)
{
// $type = $this->getType();
}

private function getType($key)
{
if (!$this->_type)
{
throw new Exception("can't decide type of $key', queue or exchange?");
}
$type = $this->_type;
$this->_type = null;
return $type;
}

public function offsetGet($offset)
{
$type = $this->getType($offset);
return $this->$type($offset);
}

public function offsetExists($offset)
{
$type = $this->getType($offset);
return array_key_exists($offset, $this->{'_'.$type});
}

public function offsetSet($offset, $value) { }

public function offsetUnset($offset)
{
$type = $this->getType($offset);
unset($this->{'_'.$type}[$key]);
}
}

0 comments on commit fdc6fc1

Please sign in to comment.