laravel 4 启用firephp 支持
目前项目中选用的开发框架是laravel4,经过几天的阅读源码以及看社区教程,发现是比较先进和强大的框架.
?
只是对新手入门这块做的很不给力,虽然官方文档在中文化上比较完整,但是相应的文档都是浅尝辄止,项目中用到的东西讲到的都比较少....
?
为了方便开发测试,所以把firephp予以集成,具体读源码找文档的时间就不说了,大致如下,在 app/start/global.php 文件中加入行:
log::getmonolog()->pushhandler(new firephphandler(logger::debug));
?然后在代码中使用log类就能输出结果...
?
另外对其db查询器使用不是很赶趟,项目组中的妹子也不会用,所以我把aertdb单独领出来做成了插件形式...
?
dbo = $dbo;		$this->ds = new aert_db_datasource( $dbo->getpdo() );		$this->actor = new aert_db_actor($this->ds);	}		/**	 * 返回数据源对象	 * 	 * @return aert_db_datasource	 */	function getdatasource()	{		return $this->ds;	}	    /**     * 从表中检索符合条件的一条记录     *     * @param string $table     * @param mixed $cond     * @param string $fields     * @param string $sort     *      * @return array     */    function selectrow($table ,$cond=null ,$fields='*', $sort=null)	{		$cond = aert_db_sqlhelper::parsecond($this->ds,$cond);		if ($cond) $cond = where {$cond};		if ($sort) $sort = order by {$sort};				$qfields = aert_db_sqlhelper::qfields($fields,$table);				return $this->actor->read(aert_db_actor::mode_read_getrow, array(				select {$qfields} from {$table} {$cond} {$sort}			));	}    	/**	 * 从表中检索符合条件的多条记录	 *	 * @param string $table	 * @param mixed $cond	 * @param string $fields	 * @param string $sort	 * @param int|array $limit 数组的话遵循格式 ( offset,length ) 	 * @param bool $calc_total 计算总个数 	 * 	 * @return array	 */	function select($table, $cond=null, $fields='*', $sort=null, $limit=null, $calc_total=false)	{				$cond = aert_db_sqlhelper::parsecond($this->ds,$cond);		if ($cond) $cond = where {$cond};		if ($sort) $sort = order by {$sort};				$qfields = aert_db_sqlhelper::qfields($fields,$table);		$table = aert_db_sqlhelper::qtable($table);				return $this->actor->read(aert_db_actor::mode_read_getall, array(				select {$qfields} from {$table} {$cond} {$sort},				empty($limit) ? false : $limit,				$calc_total			));	}	    /**     * 统计符合条件的记录的总数     *     * @param string $table     * @param mixed $cond     * @param string|array $fields     * @param boolean $distinct     *     * @return int     */    function count($table, $cond=null, $fields='*', $distinct=false)	{    	if ($distinct) $distinct = 'distinct ';    		    	$cond = aert_db_sqlhelper::parsecond($this->ds,$cond);    	if ($cond) $cond = where {$cond};		    	if (is_null($fields) || trim($fields) == '*') {            $fields = '*';        }         else {            $fields = aert_db_sqlhelper::qfields($fields,$table);        }                $table = aert_db_sqlhelper::qtable($table);                return (int) $this->actor->read(aert_db_actor::mode_read_getone, array(				select count({$distinct}{$fields}) from {$table} {$cond}			));    }    /**     * 插入一条记录     *     * @param string $table     * @param array $row     * @param bool $pkval 是否获取插入的主键值     *     * @return mixed     */    function insert($table, array $row, $pkval=false)	{		list($holders, $values) = aert_db_sqlhelper::placeholder($row);        $holders = implode(',', $holders);                $fields = aert_db_sqlhelper::qfields(array_keys($values));                $table = aert_db_sqlhelper::qtable($table);		        return $this->actor->write(aert_db_actor::mode_write_insert, array(				aert_db_sqlhelper::bind($this->ds, insert into {$table} ({$fields}) values ({$holders}), $row),				$pkval			));	}    /**	 * 更新表中记录	 *	 * @param string $table	 * @param array $row	 * @param mixed $cond 条件	 * 	 * @return int	 */	function update($table, array $row, $cond=null)	{		if ( empty($row) ) return false;					        list($pairs, $values) = aert_db_sqlhelper::placeholderpair($row);        $pairs = implode(',', $pairs);                $table = aert_db_sqlhelper::qtable($table);		        $sql = aert_db_sqlhelper::bind($this->ds, update {$table} set {$pairs}, $row);                $cond = aert_db_sqlhelper::parsecond($this->ds, $cond);        if ($cond) $sql .=  where {$cond};                return $this->actor->write(aert_db_actor::mode_write_update, array(			 $sql		));			}    /**	 * 删除 表中记录	 * 	 * @param string $table	 * @param mixed $cond	 * 	 * @return int	 */	function del($table, $cond=null)	{		$cond = aert_db_sqlhelper::parsecond($this->ds, $cond);		$table = aert_db_sqlhelper::qtable($table);				$sql = delete from {$table}  . (empty($cond) ? '' : where {$cond});				return $this->actor->write(aert_db_actor::mode_write_delete, array(				$sql			));	}			/**	 * 向表中 某字段的值做 加运算	 *	 * @param string $table	 * @param string $field	 * @param int $incr	 * @param mixed $cond	 * 	 * @return int	 */    function incrfield($table, $field, $incr=1, $cond=null)	{		if ( empty($field) ) return false;				$table = aert_db_sqlhelper::qtable($table);		$field = aert_db_sqlhelper::qfield($field);				$sql = update {$table} set {$field}={$field}+{$incr};				$cond = aert_db_sqlhelper::parsecond($this->ds, $cond);        if ($cond) $sql .=  where {$cond};                return $this->actor->write(aert_db_actor::mode_write_update, array(			 $sql		));		}	}class aert_db_actor{			/**	 * 读 记录集	 */	const mode_read_getall = 1;		/**	 * 读 第一条记录	 */	const mode_read_getrow = 2;		/**	 * 读 第一条记录的第一个字段	 */	const mode_read_getone = 3;		/**	 * 读 记录集的指定列	 */	const mode_read_getcol = 4;		/**	 * 读 记录集的总个数	 */	const mode_read_allcount = 5;		/**	 * 写 (插入) 操作	 */	const mode_write_insert = 11;		/**	 * 写 (更新) 操作	 */	const mode_write_update = 12;		/**	 * 写 (删除) 操作	 */	const mode_write_delete = 13;		/**	 * 数据源对象	 * 	 * @var aert_db_datasource	 */	private $ds;		function __construct(aert_db_datasource $ds)	{		$this->ds = $ds;	}					/**	 * 执行 读 操作	 * 	 * @param string $mode 模式 [mode_read_getall,mode_read_getrow,mode_read_getone,mode_read_getcol]	 * @param mixed $arguments 参数[不同模式参数不同,缺省为sql字符串]	 * @param callback $cb 查询记录集的回调处理函数	 * 	 * @return mixed	 */	function read($mode, $arguments, $cb=null){				$arguments = (array) $arguments;				$sql = array_shift($arguments);// 缺省第一个参数是sql字符串				switch ($mode){			case self::mode_read_getall: // array(sql,limit,counter),如果sql里面带了limit则不能使用counter				$limit = array_shift($arguments);				$counter = array_shift($arguments);								$result = null;				if ($counter)				{					$result = array(						'total' => $this->ds->count($sql),					);				}				if ($limit) $sql = $this->ds->sql_limit($sql, $limit);								if (is_array($result))				{					$result['rows'] = ($result['total'] == 0) ? array() : $this->ds->all($sql);				}				else				{					$result = $this->ds->all($sql);				}				break;			case self::mode_read_getcol:// array(sql,col,limit,counter) col 下标从 0开始 为第一列				$col = (int) array_shift($arguments);				$limit = array_shift($arguments);				$counter = array_shift($arguments);								$result = null;				if ($counter)				{					$result = array(						'total' => $this->ds->count($sql),					);				}				if ($limit) $sql = $this->ds->sql_limit($sql, $limit);				if (is_array($result))				{					$result['rows'] = ($result['total'] == 0) ? array() : $this->ds->col($sql,$col);				}				else				{					$result = $this->ds->col($sql,$col);				}				break;			case self::mode_read_getrow:				$result = $this->ds->row($sql);				break;			case self::mode_read_getone:				$result = $this->ds->one($sql);				break;						case self::mode_read_allcount:				$result = $this->ds->count($sql);				break;			default:				throw exception(无效[r]: {$mode});		}				return (empty($cb) || !is_callable($cb)) ? $result : call_user_func_array($cb,array($result));	}		/**	 * 执行 更新/删除 操作	 * 	 * @param string $mode 模式 [mode_write_insert,mode_write_update,mode_write_delete]	 * @param mixed $arguments 参数[不同模式参数不同,缺省为sql字符串]	 * @param callback $cb 查询结果集的回调处理函数	 * 	 * @return mixed	 */	function write($mode, $arguments, $cb=null){						$arguments = (array) $arguments;				$sql = array_shift($arguments);// 缺省第一个参数是sql字符串				$this->ds->execute($sql);				switch ($mode){						case self::mode_write_insert: // 插入操作可选 得到主键标识				$id = array_shift($arguments);				$result = $id ? $this->ds->insert_id() : $this->ds->affected_rows();				break;			case self::mode_write_update:			case self::mode_write_delete:				$result = $this->ds->affected_rows();				break;			default:				throw exception(无效[w]: {$mode});		}				return (empty($cb) || !is_callable($cb)) ? $result : call_user_func_array($cb,array($result));	}		}/** * db 数据源 */class aert_db_datasource{		/**     * @var int     */    private $query_count = 0;    /**     * @var pdo     */    private $db;        /**     * @var int     */    protected $affected_rows = 0;    function __construct(pdo $db)    {    	$this->db = $db;    }        function begin()    {        $this->db->begintransaction();    }    function commit()    {        $this->db->commit();    }    function rollback()    {        $this->db->rollback();    }	    function qstr($value)	{		if (is_int($value) || is_float($value)) { return $value; }		if (is_bool($value)) { return $value ? 1 : 0; }		if (is_null($value)) { return 'null'; }				return $this->db->quote($value);	}		function insert_id()	{		return $this->db->lastinsertid();	}	    function affected_rows()    {    	return $this->affected_rows;    }	    function execute($sql, array $args = null)    {    	$this->affected_rows = 0;    	       	if (!empty($args)) {       		$sql = aert_db_sqlhelper::bind($this, $sql, $args);		}        log::debug($sql);        $result = $this->db->exec($sql);                $this->query_count++;        if ($result === false)        {        	$error = $this->db->errorinfo();	    	throw new exception(query_failed: {$error[0]}/{$error[1]}, {$error[2]}\n{$sql});        }                $this->affected_rows = $result;    	    }        /**     * @return pdostatement     */    private function query($sql)    {    	    	log::debug($sql);    	$statement = $this->db->query($sql);        $this->query_count++;                if ($statement !== false) return $statement;            	$error = $this->db->errorinfo();	    throw new exception(query_failed: {$error[0]}/{$error[1]}, {$error[2]}\n{$sql});    }    		function all($sql)    {        $res = $this->query($sql);        /* @var $res pdostatement */                $rowset = $res->fetchall(pdo::fetch_assoc);        $res = null;        return $rowset;    }	    function one($sql)    {    	$res = $this->query($sql);        /* @var $res pdostatement */    	    	$val = $res->fetchcolumn(0);    	$res = null;        return $val;    }        function row($sql)    {    	$res = $this->query($sql);        /* @var $res pdostatement */    	    	$row = $res->fetch(pdo::fetch_assoc);    	        $res = null;        return $row;    }	    function col($sql, $col=0)    {        $res = $this->query($sql);        /* @var $res pdostatement */                $rowset = $res->fetchall(pdo::fetch_assoc,$col);        $res = null;                return $rowset;    }	function count($sql)	{		return (int) $this->one(select count(*) from ( $sql ) as t);	}		function sql_limit($sql, $limit)	{		if (empty($limit)) return $sql;						if (is_array($limit))		{			list($skip, $l) = $limit;	        $skip = intval($skip);          	$limit = intval($l);	    }	    else	    {	      	$skip = 0;	       	$limit = intval($limit);	    }			    return {$sql} limit {$skip}, {$limit};	}	}class aert_db_sqlhelper{		/**     * 根据 sql 语句和提供的参数数组,生成最终的 sql 语句     *     * @param aert_db_datasource $ds     * @param string $sql     * @param array $inputarr     *     * @return string     */	static function bind(aert_db_datasource $ds, $sql, array $inputarr)	{		$arr = explode('?', $sql);        $sql = array_shift($arr);        foreach ($inputarr as $value) {            if (isset($arr[0])) {                $sql .= $ds->qstr($value) . array_shift($arr);            }        }        return $sql;	}		/**	 * 解析查询条件	 * 	 * @param aert_db_datasource $ds	 * @param mixed $cond	 * @param bool $dash 是否使用括号将返回的条件包括起来	 *	 * @return string	 */	static function parsecond(aert_db_datasource $ds, $cond, $dash=false)	{				if (empty($cond)) return ''; 				// 如果是字符串,则假定为自定义条件        if (is_string($cond)) return $cond;	        // 如果不是数组,说明提供的查询条件有误        if (!is_array($cond)) return '';                static $equalin = array('=','in','not in');        static $betweenand = array('between_and','not_between_and');         		$where = '';$expr = ''; 		 		/**         * 不过何种条件形式,一律为  字段名 => (值, 操作, 连接运算符, 值是否是sql命令) 的形式         */ 		foreach ($cond as $field => $d) { 			 			$expr = 'and';             			if (!is_string($field)) { 				continue; 			} 			if (!is_array($d)) {                // 字段名 => 值            	$d = array($d);            }            reset($d);            // 第一个元素是值 			if (!isset($d[1])) { $d[1] = '='; }            if (!isset($d[2])) { $d[2] = $expr; }            if (!isset($d[3])) { $d[3] = false; }			            list($value, $op, $expr, $iscommand) = $d;                        $op = strtoupper(trim($op));                        $expr = strtoupper(trim($expr));                        if (is_array($value)){ 				 				do { 					if (in_array($op, $equalin)){ 						if ($op == '=') $op = 'in'; 						$value = '(' . implode(',',array_map(array($ds, 'qstr'),$value)) . ')'; 						break; 					} 					 						 				if (in_array($op, $betweenand)){	 						 					$between = array_shift($value);	 						 					$and = array_shift($value);	 					$value = sprintf('between %s and %s',$ds->qstr($between),$ds->qstr($and));	 					$op = 'not_between_and' == $op ? 'not' : '';// 此处已经串在 $value 中了	 					break;	 				} 						 				// 一个字段对应 多组条件 的实现,比如 a > 15 or a  array(  array( array(15,'>','or'),array(5,' $v){ 							$kv[:+{$k}+:] = $v; 						} 						$value = self::parseconditions($ds,$kv,true); 						 						foreach(array_keys($kv) as $k){ 							$value = str_ireplace($k,$field,$value); 						} 						 						$field = $op = '';// 此处已经串在 $value 中了	 					break; 					} 					 				} while(false); 				 				$iscommand = true; 			} 			 			if (!$iscommand) {				$value = $ds->qstr($value);			}			$where .= {$field} {$op} {$value} {$expr} ; 		} 		        $where = substr($where, 0, - (strlen($expr) + 2));        return $dash ? ({$where}) : $where;	}			static function qtable($table)	{		return `{$table}`;	}		static function qfield($fieldname, $table = null)	{		$fieldname = ($fieldname == '*') ? '*' : `{$fieldname}`;		return $table != '' ? self::qtable($table) . '.' . $fieldname : $fieldname;	}		    static function qfields($fields, $table = null, $returnarray = false)    {        if (!is_array($fields)) {            $fields = explode(',', $fields);            $fields = array_map('trim', $fields);        }        $return = array();        foreach ($fields as $fieldname) {            $return[] = self::qfield($fieldname, $table);        }               return $returnarray ? $return : implode(', ', $return);    }	    static function placeholder(& $inputarr, $fields = null)    {        $holders = array();        $values = array();        if (is_array($fields)) {            $fields = array_change_key_case(array_flip($fields), case_lower);            foreach (array_keys($inputarr) as $key) {                if (!isset($fields[strtolower($key)])) { continue; }                $holders[] = '?';                $values[$key] =& $inputarr[$key];            }        } else {            foreach (array_keys($inputarr) as $key) {                $holders[] = '?';                $values[$key] =& $inputarr[$key];            }        }        return array($holders, $values);    }        static function placeholderpair(& $inputarr, $fields = null)    {        $pairs = array();        $values = array();        if (is_array($fields)) {            $fields = array_change_key_case(array_flip($fields), case_lower);            foreach (array_keys($inputarr) as $key) {                if (!isset($fields[strtolower($key)])) { continue; }                $qkey = self::qfield($key);                $pairs[] = {$qkey}=?;                $values[$key] =& $inputarr[$key];            }        } else {            foreach (array_keys($inputarr) as $key) {                $qkey = self::qfield($key);                $pairs[] = {$qkey}=?;                $values[$key] =& $inputarr[$key];            }        }        return array($pairs, $values);    }    }?
querynode = $query_node;			$list[$table]->table = $table;		}		return $list[$table];	}		/**	 * 从表中检索符合条件的多条记录	 *	 * @param mixed $cond	 * @param string $fields	 * @param string $sort	 * @param int|array $limit 数组的话遵循格式 ( offset,length ) 	 * @param bool $calc_total 计算总个数 	 * 	 * @return array	 */	function getall($cond=null, $fields='*', $sort=null, $limit=null, $calc_total=false)	{		return aert_dbrepo::querynode($this->querynode)->select($this->table,$cond, $fields, $sort, $limit, $calc_total);	}		/**     * 从表中检索符合条件的一条记录     *     * @param mixed $cond     * @param string $fields     * @param string $sort     *      * @return array     */	function getone($cond=null ,$fields='*', $sort=null)	{		return aert_dbrepo::querynode($this->querynode)->selectrow($this->table,$cond, $fields, $sort);	}		/**     * 统计符合条件的记录的总数     *     * @param mixed $cond     * @param string|array $fields     * @param boolean $distinct     *     * @return int     */	function count($cond=null, $fields='*', $distinct=false)	{		return aert_dbrepo::querynode($this->querynode)->count($this->table,$cond, $fields, $distinct);	}		/**     * 插入一条记录     *     * @param array $row     * @param bool $pkval 是否获取插入的主键值     *     * @return mixed     */	function insert(array $row, $pkval=false)	{		return aert_dbrepo::querynode($this->querynode)->insert($this->table, $row, $pkval);	}		/**	 * 更新表中记录,返回表中被更新行数	 *	 * @param array $row	 * @param mixed $cond 条件	 * 	 * @return int	 */	function update(array $row, $cond=null)	{		return aert_dbrepo::querynode($this->querynode)->update($this->table, $row, $cond);	}		/**	 * 删除 表中记录,返回表中被删除行数	 * 	 * @param mixed $cond	 * 	 * @return int	 */	function del($cond=null)	{		return aert_dbrepo::querynode($this->querynode)->del($this->table, $cond);	}		/**	 * 向表中 某字段的值做 加运算	 *	 * @param string $field	 * @param int $incr	 * @param mixed $cond	 * 	 * @return int	 */	function incrfield($field, $incr=1, $cond=null)	{		return aert_dbrepo::querynode($this->querynode)->incrfield($this->table, $field, $incr, $cond);	}		/**	 * 向表中 某字段的值做 减运算	 *	 * @param string $field	 * @param int $incr	 * @param mixed $cond	 * 	 * @return int	 */	function decrfield($field, $decr=1, $cond=null)	{		return aert_dbrepo::querynode($this->querynode)->incrfield($this->table, $field, (-1 * $decr), $cond);	}		}
?
功能虽然木有那么强大,但是也应该能方便不少
   
 
   