Tuesday, July 6, 2010

Proxy Class

/**
 * Wrapper around Zend_Db connection that performs logging
 */
class Zend_Db_LogWrapper {
    private $conn;
    private $logger;

    public function __construct($conn, $logFilename) {
        $this->conn = $conn;
        $this->logger = new Logger($logFilename);
    }

    public function insert($table, array $bind) {
        $this->logger->logDb($table, 'insert', $bind);
        return $this->conn->insert($table, $bind);
    }

    public function update($table, array $bind, $where = '') {
        $this->logger->logDb($table, 'update', $bind, $where);
        return $this->conn->update($table, $bind, $where);
    }

    public function delete($table, $where = '') {
        $this->logger->logDb($table, 'delete', '', $where);
        return $this->conn->delete($table, $where);
    }

    /**
     * Redirect all other calls to the wrapped connection
     */
    public function __call($name, $arguments) {
        return call_user_func_array(array($this->conn, $name), $arguments);
    }
}