/**
  * 创建数据库连接实例
  * @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];
 }
Example #2
0
 /**
  * 创建缓存
  * @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);
     }
 }
Example #3
0
 /**
  * 创建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());
     }
 }
 * Copyright (c) 2013-now http://blog518.com All rights reserved.
 * ---------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ---------------------------------------------------------------------
 * Author: <*****@*****.**>
 * @version 1.2.1
 *-----------------------------------------------------------------------*/
namespace herosphp\model;

use herosphp\core\Loader;
use herosphp\core\WebApplication;
use herosphp\db\DBFactory;
use herosphp\exception\UnSupportedOperationException;
use herosphp\filter\Filter;
use herosphp\string\StringUtils;
Loader::import('model.IModel', IMPORT_FRAME);
class MongoModel implements IModel
{
    /**
     * 数据库连接资源
     * @var \herosphp\db\mongo\MongoDB
     */
    private $db;
    /**
     * 数据表名称
     * @var string
     */
    private $table = '';
    /**
     * 数据过滤规则
     * @var array
<?php

namespace mobile\action;

use herosphp\bean\Beans;
use herosphp\core\Loader;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\utils\ArrayUtils;
Loader::import('mobile.action.CommonAction', IMPORT_APP);
/**
 * 驼牛网M站
 * @package mobile\action
 */
class IndexAction extends CommonAction
{
    /**
     * 首页
     * @param HttpRequest $request
     */
    public function index(HttpRequest $request)
    {
        //获取导航
        $service = Beans::get('admin.chanel.service');
        $chanels = $service->getItems('pid = 0', 'id,name', 'sort_num ASC');
        foreach ($chanels as $key => $value) {
            $chanels[$key]['url'] = url("/mobile_index_channel/?id={$value['id']}");
        }
        $this->assign('chanels', $chanels);
        //获取推荐文章
        $this->getArticlePosition('index_carousel');
<?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
{
}
<?php

\herosphp\core\Loader::import('tasks.DataTransferTask', IMPORT_CLIENT);
$task = new \tasks\DataTransferTask();
$task->run();
<?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

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
{
}
<?php

namespace tasks;

use herosphp\utils\FileUtils;
use tasks\interfaces\ITask;
use herosphp\core\Loader;
Loader::import('tasks.interfaces.ITask', IMPORT_CLIENT);
/**
 * 处理图片文件夹
 * 1. 将媒体logo, 文章裁剪的缩略图替换原图
 * 2. 删除缩略图缓存
 * @author yangjian102621@163.com
 */
class ImageTask implements ITask
{
    /**
     * 要操作的目录
     * @var string
     */
    private static $dir = null;
    /**
     * 日志文件
     * @var string
     */
    private static $logFile = './logs/image.log';
    /**
     * 总共删除缩略图缓存的文件个数
     * @var int
     */
    private static $delNum = 0;
<?php

namespace test\service;

use common\service\CommonService;
use herosphp\core\Loader;
use test\service\interfaces\IUserService;
Loader::import('test.service.interfaces.IUserService', IMPORT_APP);
/**
 * 用户服务实现
 * Class UserService
 * @package test\service
 */
class UserService extends CommonService implements IUserService
{
    public function register()
    {
        __print("调用了 UserService::register 方法");
    }
    public function login()
    {
        __print("调用了 UserService::login 方法");
    }
}
<?php

namespace site\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;
use herosphp\core\Loader;
Loader::import('site.action.CommonAction', IMPORT_APP);
/**
 * 媒体服务 Action
 * @author          yangjian<*****@*****.**>
 */
class MediaAction extends CommonAction
{
    public function index(HttpRequest $request)
    {
        $id = $request->getParameter("id", "intval");
        $url = url("/site_service_index/?id={$id}&media_id=" . $this->loginMedia['id']);
        page301($url);
    }
}
<?php

namespace herosphp\lock;

/*---------------------------------------------------------------------
 * 同步锁,通过系统信号量加锁方式实现
 * ---------------------------------------------------------------------
 * Copyright (c) 2013-now http://blog518.com All rights reserved.
 * ---------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ---------------------------------------------------------------------
 * Author: <*****@*****.**>
 *-----------------------------------------------------------------------*/
use herosphp\core\Loader;
use herosphp\lock\interfaces\ISynLock;
Loader::import('lock.interfaces.ISynLock', IMPORT_FRAME);
class SemSynLock implements ISynLock
{
    private $ipc_signal = null;
    //系统信号量
    public function __construct($key)
    {
        if (!is_long($key)) {
            E("传入了非法的信号量key");
        }
        $this->ipc_signal = sem_get($key);
    }
    /**
     * 尝试去获取锁,成功返回false并且一直阻塞
     * @throws \herosphp\exception\HeroException
     */
 * ---------------------------------------------------------------------
 * 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\Idb;
use herosphp\exception\DBException;
use PDO;
use PDOException;
Loader::import('db.interfaces.Idb', IMPORT_FRAME);
class SingleDB implements Idb
{
    /**
     * PDO 数据库连接实例
     * @var \PDO
     */
    private $link;
    /**
     * 数据库配置参数
     * @var array
     */
    private $config = array();
    /**
     * 事务的级数,解决事务的嵌套问题
     * @var int
Example #15
0
<?php

namespace test\dao;

use common\dao\CommonDao;
use herosphp\core\Loader;
use test\dao\interfaces\ITestDao;
Loader::import('common.dao.CommonDao', IMPORT_APP);
Loader::import('test.dao.interfaces.ITestDao', IMPORT_APP);
/**
 * 测试(DAO)接口的通用实现
 * Class CommonDao
 * @package test\dao
 * @author yangjian102621@163.com
 */
class TestDao extends CommonDao implements ITestDao
{
}
<?php

namespace common\dao;

use common\dao\interfaces\IACommonDao;
use herosphp\core\Loader;
Loader::import('common.dao.interfaces.IACommonDao', IMPORT_APP);
/**
 * 通用记录访问对象(DAO)接口的通用实现
 * Class CommonDao
 * @package common\dao
 * @author yangjian102621@163.com
 */
abstract class ACommonDao implements IACommonDao
{
    /**
     * 数据库操作模型
     * @var \herosphp\model\C_Model
     */
    protected $modelDao;
    /**
     * 构造函数,初始化modelDao
     * @param $model
     */
    public function __construct($model)
    {
        $this->setModelDao(Loader::model($model));
    }
    /**
     * @param \herosphp\model\C_Model $modelDao
     */
 * HerosPHP 应用程序实例类,单例模式
 *  @package herosphp\core
 * ---------------------------------------------------------------------
 * Copyright (c) 2013-now http://blog518.com All rights reserved.
 * ---------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ---------------------------------------------------------------------
 * Author: <*****@*****.**>
 * @version 1.2.1
 *-----------------------------------------------------------------------*/
namespace herosphp\core;

use herosphp\bean\Beans;
use herosphp\core\interfaces\IApplication;
use herosphp\http\HttpRequest;
Loader::import('core.interfaces.IApplication', IMPORT_FRAME);
class WebApplication implements IApplication
{
    /**
     * http 请求对象
     * @var \herosphp\http\HttpRequest
     */
    private $httpRequest;
    /**
     * 系统配置信息
     * @var array
     */
    private $configs = array();
    /**
     * action 实例
     * @var Object
<?php

namespace article\dao;

use article\dao\interfaces\IArtoneDao;
use common\dao\CommonDao;
use herosphp\core\Loader;
Loader::import('article.dao.interfaces.IArtoneDao');
Loader::import('common.dao.CommonDao');
/**
 * 单文章(DAO)接口实现
 * Class ArtoneDao
 * @package article\dao
 * @author yangjian102621@163.com
 */
class ArtoneDao extends CommonDao implements IArtoneDao
{
    /**
     * 单文章内容dao
     * @var \herosphp\model\C_Model
     */
    private $dataDao = null;
    public function __construct($articleModel, $dataModel)
    {
        $this->setModelDao(Loader::model($articleModel));
        $this->dataDao = Loader::model($dataModel);
    }
    /**
     * 添加文章
     * @see \article\dao\interfaces\IArtoneDao::add()
     */
<?php

namespace site\action;

use article\action\ArticleCommonAction;
use herosphp\bean\Beans;
use herosphp\core\WebApplication;
use herosphp\core\Loader;
use herosphp\utils\ArrayUtils;
Loader::import('article.action.ArticleCommonAction', IMPORT_APP);
/**
 * Index Action
 * @author          yangjian<*****@*****.**>
 */
class CommonAction extends ArticleCommonAction
{
    /**
     * 当前媒体信息
     * @var array
     */
    protected $loginMedia = array();
    /**
     * 当前媒体配置
     * @var array
     */
    protected $mediaConfigs = array();
    /**
     * 初始化方法
     */
    public function C_start()
    {
Example #20
0
<?php

namespace client;

use herosphp\core\Loader;
use tasks\GmodelTask;
Loader::import('tasks.GmodelTask', IMPORT_CLIENT);
$task = new GmodelTask();
$task->run();
 * ---------------------------------------------------------------------
 * 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;
    /* 当前写连接服务器 */
    /**
Example #22
0
<?php

namespace test\dao;

use test\dao\interfaces\IArticleDao;
use common\dao\CommonDao;
use herosphp\core\Loader;
use test\dao\interfaces\IUserDao;
Loader::import('test.dao.interfaces.IUserDao');
/**
 * @author yangjian102621@gmail.com
 */
class UserDao extends CommonDao implements IUserDao
{
}
<?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()
Example #24
0
<?php

\herosphp\core\Loader::import('tasks.ImageTask', IMPORT_CLIENT);
$task = new \tasks\ImageTask();
$task->run();
Example #25
0
/*---------------------------------------------------------------------
 * 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)
Example #26
0
 /**
  * 自动加载系统框架类和app公共类
  * @param $className
  */
 public static function autoLoad($className)
 {
     echo "\n-----------";
     echo " class: " . __CLASS__ . "    method: " . __METHOD__ . "\n";
     echo " file:" . __FILE__ . "\n";
     print_r(array("className" => $className));
     echo "-----------\n";
     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);
     }
 }
 /**
  * 自动加载系统框架类和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);
     }
 }
Example #28
0
/*---------------------------------------------------------------------
 * 创建&管理Bean对象
 * ---------------------------------------------------------------------
 * Copyright (c) 2013-now http://blog518.com All rights reserved.
 * ---------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ---------------------------------------------------------------------
 * Author: <*****@*****.**>
 *-----------------------------------------------------------------------*/
namespace herosphp\bean;

use herosphp\core\Loader;
use herosphp\exception\BeanException;
Loader::import('exception.BeanException', IMPORT_FRAME);
Loader::import('bean.BeanUtil', IMPORT_FRAME);
abstract class Beans
{
    /**
     * Bean的类型:对象
     * @var string
     */
    const BEAN_OBJECT = 'OBJECT';
    /**
     * Bean的类型:对象数组
     * @var string
     */
    const BEAN_OBJECT_ARRAY = 'OB_ARRAY';
    /**
     * 应用程序监听器bean key
     * @var string
<?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
{
}
Example #30
0
/*---------------------------------------------------------------------
 * session工厂
 * ---------------------------------------------------------------------
 * 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;
Loader::import('session.FileSession', IMPORT_FRAME);
Loader::import('session.MemSession', IMPORT_FRAME);
class Session
{
    /**
     * 开启session
     */
    public static function start()
    {
        //如果已经开启了SESSION则直接返回
        if (isset($_SESSION)) {
            return true;
        }
        //loading session configures
        $configs = Loader::config('session', 'session');
        switch (SESSION_HANDLER) {
            case 'file':