/** * 初始化数据库连接 * @param string $table 数据表 * @param array $config 数据库配置信息 */ public function __construct($table, $config = null) { //加载数据表配置 $dbConfigPath = 'db'; if (DB_CFG_PATH != false) { $dbConfigPath = DB_CFG_PATH; } $tableConfig = Loader::config('table', $dbConfigPath); $this->table = $tableConfig[$table]; //初始化数据库配置 if (!$config) { //默认使用第一个数据库服务器配置 $dbConfigs = Loader::config('hosts', $dbConfigPath); $db_config = $dbConfigs[DB_TYPE]; if (DB_ACCESS == DB_ACCESS_SINGLE) { //单台服务器 $config = $db_config[0]; } else { if (DB_ACCESS == DB_ACCESS_CLUSTERS) { //多台服务器 $config = $db_config; } } } //创建数据库 $this->db = DBFactory::createDB(DB_ACCESS, $config); }
public function index(HttpRequest $request) { print_r($request->getParameter()); ////----DB 操作 //DB: 原生query操作 $model = Loader::model('user'); $result = $model->query("select * from user"); $rowArr = $result->fetchAll(); print_r($rowArr); //DB: 查询 //DB: 插入 /* $data = array( "id" => "", "name" => "xm" ); $model->insert($data); */ //DB: 更新 $data = array("id" => "12", "name" => "yy"); $id = 2; $model->update($data, $id); //DB: 删除 $id = 1; $model->delete($id); //DB: 事物 $items = array("id" => 1, "title" => "hello title2"); $this->assign('items', $items); $this->assign('var1', "yu"); //$this->display('index_test2');//这里不要用display这个方法,流程不对,用view $this->setView('index_test'); //die(); }
public function run() { tprintOk("Hello, world!"); $model = Loader::model('article'); $conditions = array("id" => ">300"); $items = $model->getItems($conditions, "id, url, title", null, 1, 20); tprintOk($items[0]['title']); }
/** * @param HttpRequest $request */ public function delete(HttpRequest $request) { $id = $request->getParameter('id', 'intval'); $model = Loader::model('article'); if ($id > 0) { var_dump($model->delete($id)); } die; }
/** * 初始化数据库连接 * @param string $table 数据表 * @param array $config 数据库配置信息 */ public function __construct($table, $config = null) { //初始化数据库配置 if (!$config) { $congfig = Loader::config('db'); } //创建数据库 $this->db = DBFactory::createDB('mongo', $congfig['mongo']); $this->table = $table; }
/** * 创建数据库连接实例 * @param int $accessType 连接方式(连接单个服务器还是连接集群) * @param array $config 数据库的配置信息 * @return Idb */ public static function createDB($accessType = DB_ACCESS_SINGLE, &$config = null) { //获取包含路径 $classPath = self::$DB_DRIVER[$accessType]['path']; Loader::import($classPath, IMPORT_FRAME); $className = self::$DB_DRIVER[$accessType]['class']; $key = md5($className . $config['flag']); if (!isset(self::$DB_POOL[$key])) { self::$DB_POOL[$key] = new $className($config); } return self::$DB_POOL[$key]; }
/** * @return bool|null|\Redis */ public static function getInstance() { if (is_null(self::$redis)) { $configs = Loader::config('redis', 'cache'); if (!$configs) { return false; } self::$redis = new \Redis(); self::$redis->connect($configs['host'], $configs['port']); } return self::$redis; }
/** * 更改角色权限 * @param HttpRequest $request */ public function permission(HttpRequest $request) { $id = $request->getParameter('id', 'intval'); if ($id <= 0) { $this->showMessage('danger', '参数不合法!'); } $service = Beans::get($this->getServiceBean()); $item = $service->getItem($id); $permissions = cn_json_decode($item['permissions']); //加载权限选项 $permissionOptions = Loader::config('admin', 'permission'); $this->assign('permissions', $permissions); $this->assign('permissionOptions', $permissionOptions); $this->assign('item', $item); $this->setView('admin/role_permission'); }
/** * 创建缓存 * @param $key * @param bool $single 是否单例模式 * @return \herosphp\cache\interfaces\ICache */ public static function create($key = 'file', $single = true) { //如果缓存对象已经创建,则则直接返回 if (isset(self::$CACHE_SET[$key]) && $single == false) { return self::$CACHE_SET[$key]; } $configs = Loader::config($key, 'cache'); $className = self::$CACHE_BEAN[$key]['class']; Loader::import(self::$CACHE_BEAN[$key]['path'], IMPORT_FRAME); if ($single) { self::$CACHE_SET[$key] = new $className($configs); return self::$CACHE_SET[$key]; } else { return $className($configs); } }
/** * 获取指定ID的Bean * @param string $key Bean的key * @param boolean $new 是否创建新的bean,新创建的Bean不会放到容器中,如果要放到容器中,请使用set方法。 * @return Object 返回指定ID的Bean,如果Bean不存在则返回null */ public static function get($key, $new = false) { if (empty(self::$CONFIGS)) { self::$CONFIGS = Loader::config('*', 'beans'); if (!self::$CONFIGS) { return null; } } $beanConfig = self::$CONFIGS[$key]; if ($new || $beanConfig['@single'] === false) { return self::build($beanConfig); } if (!isset(self::$BEAN_POOL[$key])) { self::$BEAN_POOL[$key] = self::build($beanConfig); } return self::$BEAN_POOL[$key]; }
public function run() { // $lock = SynLockFactory::getFileSynLock(0x1234); // tprintError("try to get the lock...."); // $lock->tryLock(); // tprintOk("get the lock."); // sleep(10); // tprintWarning("release the lock."); // $lock->unlock(); $model = Loader::model("news"); $timer = timer(); for ($i = 0; $i < 1000000; $i++) { $data = array('name' => 'xiaoming', 'pass' => 'xiaoming_pass', 'address' => 'china ShenZhen'); $model->insert($data); } tprintOk("插入完成,耗时:" . (timer() - $timer) . " 秒"); }
/** * 开启session */ public static function start() { //如果已经开启了SESSION则直接返回 if (isset($_SESSION)) { return true; } //loading session configures $configs = Loader::config('session', 'session'); switch (SESSION_HANDLER) { case 'file': FileSession::start($configs[SESSION_HANDLER]); break; case 'memo': MemSession::start($configs[SESSION_HANDLER]); break; } }
public function html(HttpRequest $request) { $CACHER = CacheFactory::create('html'); $CACHER->baseKey('article')->ftype('detail')->factor('299'); $item = $CACHER->get(null); if (!$item) { $model = Loader::model('article'); $item = $model->getItem(299); $this->assign('item', $item); $html = $this->getExecutedHtml(); if ($CACHER->set(null, $html)) { __print("生成静态缓存成功!"); } } else { echo $item; } die; }
/** * 编辑媒体管理员角色界面 * @param HttpRequest $request */ public function edit(HttpRequest $request) { parent::edit($request); $item = $this->getTemplateVar('item'); //获取媒体类型 $mediaTypeService = Beans::get('media.type.service'); $mediaType = $mediaTypeService->getItem($item['media_type']); $permissionTpl = $mediaType['permission_tpl']; //加载权限选项 $permissions = Loader::config($permissionTpl, 'permission'); $this->assign('permissions', $permissions); $item['permission'] = cn_json_decode($item['permission']); $this->assign('item', $item); $this->assign("seoTitle", "媒体中心后台管理-角色编辑"); $this->assign("seoDesc", "角色管理-角色编辑"); $this->assign("seoKwords", "媒体中心后台管理 角色管理 角色编辑"); $this->setView('role/add'); }
/** * 创建Bean实例 * @param string $classPath 要创建的对象的类路径 * @param array $params 参数列表,可以是数组或者单个参数 * @return object|ReflectionClass * @throws \Exception */ public static function builtInstance($classPath, $params = null) { if (!is_string($classPath)) { return null; } try { $importPath = str_replace('\\', '.', $classPath); Loader::import($importPath, IMPORT_APP, EXT_PHP); $instance = new ReflectionClass($classPath); if (is_array($params)) { return $instance->newInstanceArgs($params); } elseif ($params) { return $instance->newInstance($params); } else { return $instance->newInstance(); } } catch (\Exception $e) { E($e->getMessage()); } }
/** * 初始化数据库连接 * @param string $table 数据表 * @param array $config 数据库配置信息 */ public function __construct($table, $config = null) { //初始化数据库配置 if (!$config) { //默认使用第一个数据库服务器配置 $dbConfigs = Loader::config('db'); $db_config = $dbConfigs[DB_TYPE]; if (DB_ACCESS == DB_ACCESS_SINGLE) { //单台服务器 $config = $db_config[0]; } else { if (DB_ACCESS == DB_ACCESS_CLUSTERS) { //多台服务器 $config = $db_config; } } } $this->table = $config['table_prefix'] . $table; //创建数据库 $this->db = DBFactory::createDB(DB_ACCESS, $config); }
/** * 开启session */ public static function start() { //如果已经开启了SESSION则直接返回 if (isset($_SESSION)) { return true; } //loading session configures $configs = Loader::config('session'); $session_configs = $configs[$configs['session_handler']]; switch ($configs['session_handler']) { case 'file': FileSession::start($session_configs); break; case 'memo': MemSession::start($session_configs); break; case 'redis': RedisSession::start($session_configs); break; } }
/** * 文章推荐位列表 * @param HttpRequest $request */ public function index(HttpRequest $request) { $__configs = Loader::config('media', 'data'); $service = Beans::get($this->getServiceBean()); $condi = array('media_id' => $this->loginMedia['id']); $items = $service->getItems($condi); $items = ArrayUtils::changeArrayKey($items, 'position'); $__configs = $__configs['media_article_rec']; $__items = array(); foreach ($__configs as $key => $value) { $__items[$key]['name'] = $value; $__items[$key]['id'] = $key; //判断是否开启此推荐位 if (!empty($items[$key])) { $__items[$key]['open'] = $items[$key]['status']; } else { $__items[$key]['open'] = 0; } } $this->assign('items', $__items); $this->assign("seoTitle", "媒体中心后台管理-文章推荐位"); $this->setView('article/recommend_index'); }
/** * 首页方法 * @param HttpRequest $request */ public function index(HttpRequest $request) { $page = $request->getParameter('page', 'intval'); $pagesize = 10; if ($page <= 0) { $page = 1; } $model = Loader::model('article'); $conditions = array("id" => ">300"); $total = $model->count($conditions); $items = $model->getItems($conditions, "id, url, title", null, $page, $pagesize); //初始化分页类 $pageHandler = new Page($total, $pagesize, $page); //获取分页数据 $pageData = $pageHandler->getPageData(DEFAULT_PAGE_STYLE); //组合分页HTML代码 if ($pageData) { $pagemenu = '<ul class="pagination">'; $pagemenu .= '<li><a href="' . $pageData['prev'] . '" aria-label="Previous"><span aria-hidden="true">«</span></a></li>'; foreach ($pageData['list'] as $key => $value) { if ($key == $page) { $pagemenu .= '<li class="active"><a href="#fakelink">' . $key . '</a></li> '; } else { $pagemenu .= '<li><a href="' . $value . '">' . $key . '</a></li> '; } } $pagemenu .= '<li><a href="' . $pageData['next'] . '" aria-label="Next"><span aria-hidden="true">»</span></a></li>'; $pagemenu .= '</ul>'; $pagemenu .= '<div class="page-input"><input type="text" class="form-control input-sm" value="' . $this->page . '"> '; $pagemenu .= '<a href="javascript:void(0);" class="btn btn-primary btn-sm" url="' . $pageData['url'] . '" id="page-goto">确定</a></div> '; } $this->assign('pagemenu', $pagemenu); $this->assign('items', $items); //设置视图 $this->setView('article_page'); }
<?php namespace media\service\interfaces; use common\service\interfaces\ICommonService; use herosphp\core\Loader; Loader::import('common.service.interfaces.ICommonService', IMPORT_APP); /** * 媒体类型服务接口 * Interface IMediaTypeService */ interface IMediaTypeService extends ICommonService { }
<?php namespace admin\service; use admin\service\interfaces\IKeywordsService; use common\service\CommonService; use herosphp\core\Loader; Loader::import('admin.service.interfaces.IKeywordsService', IMPORT_APP); Loader::import('common.service.CommonService', IMPORT_APP); /** * 系统保留字实现 * Class DomainService * @package admin\service */ class KeywordsService extends CommonService implements IKeywordsService { }
/** * 自动加载系统框架类和app公共类 * @param $className */ public static function autoLoad($className) { if (self::$LIB_CLASS[$className]) { Loader::import(self::$LIB_CLASS[$className], IMPORT_FRAME, EXT_PHP); } else { Loader::import(self::$APP_CLASS[$className], IMPORT_APP, EXT_PHP); } }
<?php namespace media\dao\interfaces; use common\dao\interfaces\ICommonDao; use herosphp\core\Loader; Loader::import("common.dao.interfaces.ICommonDao", IMPORT_APP); /** * 媒体推荐位(DAO)接口 * Interface IMediaRecDao * @package media\dao\interfaces * @author yangjian102621@163.com */ interface IMediaRecDao extends ICommonDao { }
/*--------------------------------------------------------------------- * session handler for memcache. save session data to memcache by user * --------------------------------------------------------------------- * Copyright (c) 2013-now http://blog518.com All rights reserved. * --------------------------------------------------------------------- * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) * --------------------------------------------------------------------- * Author: <*****@*****.**> * @since 2015.02.20 *-----------------------------------------------------------------------*/ namespace herosphp\session; use herosphp\core\Loader; use herosphp\session\interfaces\ISession; Loader::import('session.interfaces.ISession', IMPORT_FRAME); class MemSession implements ISession { /** * handler for memcache server * @var Object */ private static $handler = NULL; /** * @var array 配置信息 */ private static $config; /** * @see \herosphp\session\interfaces\ISession::start(). */ public static function start($config = NULL)
<?php /** * 媒体管理员Model * @author yangjian <*****@*****.**> */ namespace models; use herosphp\core\Loader; use herosphp\model\C_Model; Loader::import('filter.Filter', IMPORT_FRAME); class MediaManagerModel extends C_Model { public function __construct() { parent::__construct('media_manager'); $this->setPrimaryKey('id'); //初始化数据模型过滤器 $filterMap = array('email' => array(DFILTER_EMAIL, array(1, 30), DFILTER_SANITIZE_TRIM, '电子邮箱')); $this->setFilterMap($filterMap); } }
<?php /*--------------------------------------------------------------------- * 应用程序生命周期匹配器 * @package herosphp\listener * --------------------------------------------------------------------- * Copyright (c) 2013-now http://blog518.com All rights reserved. * --------------------------------------------------------------------- * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) * --------------------------------------------------------------------- * Author: <*****@*****.**> *-----------------------------------------------------------------------*/ namespace herosphp\listener; use herosphp\core\Loader; Loader::import('listener.IWebAplicationListener', IMPORT_FRAME); abstract class WebApplicationListenerMatcher implements IWebAplicationListener { /** * 请求初始化之前 * @return mixed */ public function beforeRequestInit() { // TODO: Implement beforeRequestInit() method. } /** * action 方法调用之前 * @return mixed */ public function beforeActionInvoke()
public function __construct($articleModel, $dataModel) { $this->setModelDao(Loader::model($articleModel)); $this->dataDao = Loader::model($dataModel); }
<?php namespace user\dao; use common\dao\CommonDao; use herosphp\core\Loader; use user\dao\interfaces\IUserMessageDao; Loader::import('user.dao.interfaces.IUserMessageDao'); Loader::import('common.dao.CommonDao'); /** * 用户站内信(DAO)接口实现 * Class UserMessageDao * @package user\dao * @author yangjian102621@163.com */ class UserMessageDao extends CommonDao implements IUserMessageDao { }
* --------------------------------------------------------------------- * Copyright (c) 2013-now http://blog518.com All rights reserved. * --------------------------------------------------------------------- * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) * --------------------------------------------------------------------- * Author: <*****@*****.**> *-----------------------------------------------------------------------*/ namespace herosphp\db\mysql; use herosphp\core\Debug; use herosphp\core\Loader; use herosphp\db\interfaces\ICusterDB; use herosphp\exception\DBException; use PDO; use PDOException; Loader::import('db.interfaces.ICusterDB', IMPORT_FRAME); /** * 多数据库连接操作类 * @author yangjian102621@163.com */ class ClusterDB implements ICusterDB { protected static $_READ_POOL = array(); /* 读服务器连接池 */ protected static $_WRITE_POOL = array(); /* 写服务器连接池 */ protected $currentReadServer = null; /* 当前读连接服务器 */ protected $currentWriteServer = null; /* 当前写连接服务器 */ /**
/** * 构造函数,初始化modelDao * @param $model */ public function __construct($model) { $this->setModelDao(Loader::model($model)); }