首页 >php教程 >php手册 >Pfinal框架更新

Pfinal框架更新

WBOY
WBOY原创
2016-06-06 19:35:411011浏览

本次更新点: 1.添加了扩展机制,可以自定义扩展 通过提供一个{$extensionName}Context.php的类,在启动配置中将上述扩展添加到系统,则可以实现扩展功能 2.添加了RPC模块,Pfinal_Remoting_Curl,提供基于权重的RR轮训调用策略 3.PfinalModelAbstract中添加

本次更新点:
1. 添加了扩展机制,可以自定义扩展
通过提供一个{$extensionName}Context.php的类,在启动配置中将上述扩展添加到系统,则可以实现扩展功能
2. 添加了RPC模块,Pfinal_Remoting_Curl,提供基于权重的RR轮训调用策略
3. PfinalModelAbstract中添加了数据库分布式策略,默认提供了一个MasterSlave策略,方便读写分离,可自行实现分库 PFinal
<?php
class CaptchaConfig extends Pfinal_Config_Base{
	/* (non-PHPdoc)
	 * @see Pfinal_Config_Base::configConstant()
	 */
	public function configConstant(Pfinal_Config_Constant $me) {
		// TODO Auto-generated method stub
		$me->setDevMode(Pfinal_Config_Constant::MODE_PRODUCT);
	}

	/* (non-PHPdoc)
	 * @see Pfinal_Config_Base::configRoute()
	 */
	public function configRoute(Pfinal_Config_Route $me) {
		// TODO Auto-generated method stub
		
	}

	/* (non-PHPdoc)
	 * @see Pfinal_Config_Base::configPlugin()
	 */
	public function configPlugin(Pfinal_Config_Plugin $me) {
		// TODO Auto-generated method stub
		$conf = array(
			'master'=>array(
				array('host'=>'127.0.0.1','port'=>3306,'user'=>'root','database'=>'test','passwd'=>'121'),
				array('host'=>'127.0.0.1','port'=>3306,'user'=>'root','database'=>'test','passwd'=>'567'),
			),
			'slave'=>array(
				array('host'=>'127.0.0.1','port'=>3306,'user'=>'root','database'=>'test','passwd'=>'aaa'),
				array('host'=>'127.0.0.1','port'=>3306,'user'=>'root','database'=>'test','passwd'=>'bbb'),
			),
		);
		$dbTest = new Pfinal_Plugin_DbConnection(new Pfinal_Model_Proxy_MasterSlave($conf));
		$active = new Pfinal_Plugin_ActiveRecord();
		$active->add('test', $dbTest);
		$me->add($dbTest);
		$me->add($active);
	}

	/* (non-PHPdoc)
	 * @see Pfinal_Config_Base::configExtension()
	 */
	public function configExtension(Pfinal_Config_Extension $me) {
		// TODO Auto-generated method stub
		$me->addExtension(LIB.DIRECTORY_SEPARATOR.'common');
	}

	/* (non-PHPdoc)
	 * @see Pfinal_Config_Base::configInterceptor()
	 */
	public function configInterceptor() {
		// TODO Auto-generated method stub
		
	}

	/* (non-PHPdoc)
	 * @see Pfinal_Config_Base::configHandler()
	 */
	public function configHandler() {
		// TODO Auto-generated method stub
		
	}

	
}
<?php 
class Pfinal_Model_Proxy_MasterSlave implements Pfinal_Model_Proxy_Strategy{
	
	protected $masterAdapters;
	
	protected $slaveAdapters;
	
	protected $adapter;
	
	public function __construct($dbConf,$adapter = 'mysql'){
		if (empty($dbConf)){
			throw new Pfinal_Exception_Argument("invalid master/slave dbconf", -1);
		}
		$this->adapter = $adapter;
		
		if (isset($dbConf['master'])&&!empty($dbConf['master'])){
			foreach ($dbConf['master'] as $conf){
				$this->cacheConnection($conf,$this->masterAdapters);
			}
		}
		if (isset($dbConf['slave'])&&!empty($dbConf['slave'])){
			foreach ($dbConf['slave'] as $conf){
				$this->cacheConnection($conf,$this->slaveAdapters);
			}
		}
		
	}
	
	/* (non-PHPdoc)
	 * @see Pfinal_Model_Proxy_Strategy::getAdapterProxy()
	 */
	public function getAdapterProxy(Pfinal_Model_Statement $stm) {
		// TODO Auto-generated method stub
		if ($stm instanceof Pfinal_Model_Selector){
			shuffle($this->slaveAdapters);
			$adapter = end($this->slaveAdapters);
			
		}
		
		if ($stm instanceof Pfinal_Model_Update || $stm instanceof Pfinal_Model_Insert || $stm instanceof Pfinal_Model_DDL){
			shuffle($this->masterAdapters);
			$adapter = end($this->masterAdapters);
		}
		return $adapter;
	}

	protected function cacheConnection($conf,&$pool){
		if (!is_array($pool)){
			$pool = array();
		}
		$host = $conf['host'];
		$port = $conf['port'];
		$user = $conf['user'];
		$passwd = $conf['passwd'];
		$database = $conf['database'];
		$instance = null;
		switch ($this->adapter){
			case 'mysql':
			default:
				$instance = new Pfinal_Model_Adapter_RMDBS();
				$instance->init($conf);
				break;
			
		}
		if (null===$instance){
			throw new Pfinal_Exception_Argument("unknow adaptername:{$this->adapter}", -1);
		}
		$key = md5(sprintf('%s:%s:%s:%s:%s',$host,$port,$user,$passwd,$database));
		$pool[$key] = $instance;
	}
	
}
<?php
/**
 * permision模块
 */
define ( 'EXTENSION_NAME', 'common' );
define ( 'VERSION', '1.0' );

define ( 'COMMON_DIR', dirname ( __FILE__ ) );

set_include_path ( get_include_path () . PATH_SEPARATOR . implode ( PATH_SEPARATOR, array (
COMMON_DIR
) ) );

class CommonContext implements Pfinal_Extension_Interface{
	/* (non-PHPdoc)
	 * @see Pfinal_Extension_Interface::bootstrap()
	 */
	public function bootstrap() {
		// TODO Auto-generated method stub
		
	}

	/* (non-PHPdoc)
	 * @see Pfinal_Extension_Interface::autoload()
	 */
	public function autoload($classname) {
		// TODO Auto-generated method stub
		$dirs = array(
				COMMON_DIR.DIRECTORY_SEPARATOR.'util',
				COMMON_DIR.DIRECTORY_SEPARATOR.'interceptor',
		);
		foreach ($dirs as $dir){
			$file = $dir.DIRECTORY_SEPARATOR.$classname.'.php';
			if (is_file($file)){
				require_once $file;
				return true;
			}
		}
		return false;
	}

	
}
<?php
class Test_Model extends PfinalModelAbstract{
	
	public function __construct($ormEnable){
		$this->tableName = 'test';
		$this->databaseName = 'test';
		parent::__construct($ormEnable);
	}
	
	public function add(){
		$this
			->set('id', 3)
			->set('name', StringUtil::MysqlEscapeString("adad'\\\n231'"))
			->save();
	}
	
	public function getXX($id){
		return $this->findById($id);
	}
}
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn