zoukankan      html  css  js  c++  java
  • yaf框架流程二

    这篇讲讲yaf的配置文件,首先上我的配置代码:

    [common]
    ;必选配置
    ;application.directory    String    应用的绝对目录路径
    
    ;可选配置
    ;名称    值类型    默认值    说明
    ;application.ext    String    php    PHP脚本的扩展名
    ;application.bootstrap    String    Bootstrapplication.php    Bootstrap路径(绝对路径)
    ;application.library    String    application.directory + "/library"    本地(自身)类库的绝对目录地址
    ;application.baseUri    String    NULL    在路由中, 需要忽略的路径前缀, 一般不需要设置, Yaf会自动判断.
    ;application.dispatcher.defaultModule    String    index    默认的模块
    ;application.dispatcher.throwException    Bool    True    在出错的时候, 是否抛出异常
    ;application.dispatcher.catchException    Bool    False    是否使用默认的异常捕获Controller, 如果开启, 在有未捕获的异常的时候, 控制权会交给ErrorController的errorAction方法, 可以通过$request->getException()获得此异常对象
    ;application.dispatcher.defaultController    String    index    默认的控制器
    ;application.dispatcher.defaultAction    String    index    默认的动作
    ;application.view.ext    String    phtml    视图模板扩展名
    ;application.modules    String    Index    声明存在的模块名, 请注意, 如果你要定义这个值, 一定要定义Index Module
    ;application.system.*    String    *    通过这个属性, 可以修改yaf的runtime configure, 比如application.system.lowcase_path, 但是请注意只有PHP_INI_ALL的配置项才可以在这里被修改, 此选项从2.2.0开始引入
    ;---------------------------------------------------------------------------------------------------------------------
    
    ;PHP.ini文件可以配置的项
    ;选项名称    默认值    可修改范围    更新记录
    ;yaf.environ    product    PHP_INI_ALL    环境名称, 当用INI作为Yaf的配置文件时, 这个指明了Yaf将要在INI配置中读取的节的名字
    ;yaf.library    NULL    PHP_INI_ALL    全局类库的目录路径
    ;yaf.cache_config    0    PHP_INI_SYSTEM    是否缓存配置文件(只针对INI配置文件生效), 打开此选项可在复杂配置的情况下提高性能
    ;yaf.name_suffix    1    PHP_INI_ALL    在处理Controller, Action, Plugin, Model的时候, 类名中关键信息是否是后缀式, 比如UserModel, 而在前缀模式下则是ModelUser
    ;yaf.name_separator    ""    PHP_INI_ALL    在处理Controller, Action, Plugin, Model的时候, 前缀和名字之间的分隔符, 默认为空, 也就是UserPlugin, 加入设置为"_", 则判断的依据就会变成:"User_Plugin", 这个主要是为了兼容ST已有的命名规范
    ;yaf.forward_limit    5    PHP_INI_ALL    forward最大嵌套深度
    ;yaf.use_namespace    0    PHP_INI_SYSTEM    开启的情况下, Yaf将会使用命名空间方式注册自己的类, 比如Yaf_Application将会变成YafApplication
    ;yaf.use_spl_autoload    0    PHP_INI_ALL    开启的情况下, Yaf在加载不成功的情况下, 会继续让PHP的自动加载函数加载, 从性能考虑, 除非特殊情况, 否则保持这个选项关闭
    
    
    ;application
    application.directory = APPLICATION_PATH"/application"
    application.library = APPLICATION_PATH"/library"
    ;如果配置模块,index为填项
    application.modules = index,admin,user
    application.dispatcher.catchException = True
    application.dispatcher.defaultModule = index
    application.dispatcher.defaultController = index
    application.dispatcher.defaultAction = index
    application.view.ext=phtml
    
    ;框架里都没有做unix嵌套子连接的判断,全部是基于ip的
    ;database
    ;程序实现的mysql负载均衡,没有failover线上环境需谨慎
    database.mysql_cache_enable=True
    database.mysql_log_error=True
    database.pdotype="mysql"
    ;master
    database.master.host = "127.0.0.1"
    database.master.port = 3306
    database.master.user="root"
    database.master.password=""
    database.master.database="test"
    ;slaves
    database.slaves.1.host = "127.0.0.1"
    database.slaves.1.port="3306"
    database.slaves.1.user = "yaftest"
    database.slaves.1.password = "123456"
    database.slaves.1.database="test"
    ;cache
    database.cache.caching_system="CacheMemcache"
    database.cache.cache_exclude_table=""
    ;600秒
    database.cache.lifetime=600
    database.cache.prefix="cye_"
    database.cache.object_cache_enable=True
    
    ;smarty
    smarty.template_dir = APPLICATION_PATH"/application/views"
    smarty.compile_dir = APPLICATION_PATH"/cache/smarty/compile"
    smarty.cache_dir = APPLICATION_PATH"/cache/smarty/cache"
    smarty.caching = FALSE
    smarty.force_compile = FALSE
    smarty.compile_check = True
    
    ;routes 最后定义的最优先
    ;index.php/test/--->index.php/index/index/index/name/test
    routes.index.type = "regex"
    routes.index.match = "#^/([a-zA-Z]+)/?#"
    routes.index.route.module = Index
    routes.index.route.controller = Index
    routes.index.route.action = Index
    routes.index.map.1 = name
    
    ;自定义路由
    ;顺序很重要
    ;index.php/list/test/abc--->index.php/index/index/index/name/test/value/abc
    routes.regex.type="regex"
    routes.regex.match="#^list/([^/]*)/([^/]*)#"
    routes.regex.route.controller=Index
    routes.regex.route.action=action
    routes.regex.map.1=name
    routes.regex.map.2=value
    
    ;index.php?m=m&c=c&a=a&test=555--->index.php/m/c/a/test/555
    routes.simple.type="simple"
    routes.simple.module=m
    routes.simple.controller=c
    routes.simple.action=a
    
    ;index.php?r=m/c/a&test=555--->index.php/m/c/a/test/555
    routes.supervar.type="supervar"
    routes.supervar.varname=r
    ;默认的Yaf_Application
    ;将会读取此节配置
    
    ;补充路由定义
    ;index.php/product/test/555--->index.php/index/product/info/name/test/value/555
    routes.rewrite.type="rewrite"
    routes.rewrite.match="/product/:name/:value"
    routes.rewrite.route.controller=product
    routes.rewrite.route.action=info
    
    ;resource
    resource.js=APPLICATION_PATH"/public/js"
    resource.css=APPLICATION_PATH"/public/css"
    resource.img=APPLICATION_PATH"/public/img"
    
    
    ;mongodb
    
    
    ;redis
    
    
    ;httpsqs
    
    ;memcache
    memcache.1.host="192.168.1.188"
    memcache.1.port=11211
    
    ;smtp
    smtp.server="smtp.sample.com"
    smtp.ssl=True
    smtp.username="sample@sample.com"
    smtp.password="password"
    smtp.helo="www.sample.com"
    
    ;customer
    customer.debug = True
    customer.benchmark = True
    
    [product : common]
    ;smarty.caching = TRUE
    ;smarty.force_compile = FALSE
    ;smarty.compile_check = TRUE
    
    
    ;cookie
    cookie.prefix="cookie_prefix"
    cookie.domain="dev.com"
    
    ;session
    ;使用系统的
    ;session.save_handler = files
    ;session.save_path = "E:/wamp/tmp/test"
    ;带有tcp的为memcache,不带的为memcached,后面可以加?persistent=1&weight=2等参数
    session.save_handler = memcache
    ;session.save_path = "tcp://192.168.1.188:11211"
    ;这里的memcache是自己实现的,也可以是redis,mysql等扩展要自己写
    session.save_path.1.host = "192.168.1.188"
    session.save_path.1.port = 11211
    
    ;使用系统的时候单位是分钟,使用memcached的时候单位是秒,数据是基于秒的
    session.expire = 1800
    session.name = "phpsessname"
    session.is_json = False
    
    session.cookie_lifetime=1800
    session.cookie_path="/"
    session.cookie_domain = "dev.com"
    session.cookie_secure = False
    session.cookie_httponly = False

    以;开头的是注释语句,前面两段是主要配置信息以及说明,整个配置文件其实只要第一句,配置了application的directory就可以运行了。

    [common]括起来的是配置段名称,可以通过:继承,例如[product:common]。

    整个配置文件会被解析存储为一个config对象,通过.分割的子对象,这个对象可以通过Yaf_Application::app()->getConfig()方法获取,例如:
    $config = Yaf_Application::app()->getConfig();$session = $config->session;然后$session->is_json就可以取到False值。这里很简单就不多说了。

    下面看看bootstrap文件的写法,这一步其实不是必须的,但是实用的项目肯定需要这个文件,在里面可以初始化一些必须的操作。

    <?php
    
    class Bootstrap extends Yaf_Bootstrap_Abstract {
        protected $config;
        
        //注册配置对象到config,可以全局使用
        public function _initConfig(Yaf_Dispatcher $dispatcher) {}
        
        //注册自定义类库的命名空间
        public function _initLocalName() {}
        
        //配置是否报错
        public function _initError(Yaf_Dispatcher $dispatcher) {}
    
        //注册插件
        public function _initPlugin(Yaf_Dispatcher $dispatcher) {}
    
        //配置路由
        public function _initRoute(Yaf_Dispatcher $dispatcher) {}
    
        //配置memcache,类似的mongodb,redis,httpsqs等如果需要也可以统一配置
        //如果在程序里面使用的话,尽量放到Yaf_Registry里
        public function _initMemcache(Yaf_Dispatcher $dispatcher) {}
    
        //配置session
        public function _initSession(Yaf_Dispatcher $dispatcher) {}
    
        //配置数据库
        //这里做的数据库负载均衡
        public function _initDatabase(Yaf_Dispatcher $dispatcher) {}
    
        //配置邮件服务器
        public function _initMailer(Yaf_Dispatcher $dispatcher) {}
    
        //配置smarty模板引擎
        public function _initSmarty(Yaf_Dispatcher $dispatcher) {}
    }

    以上例举了可以通过bootstrap初始化的功能,按每个方法分解

    //注册配置对象到config,可以全局使用
        public function _initConfig(Yaf_Dispatcher $dispatcher) {
            $this->config = Yaf_Application::app()->getConfig();
            Yaf_Registry::set('config', $this->config);
        }
    //注册自定义类库的命名空间
        public function _initLocalName() {
             Yaf_Loader::getInstance()->registerLocalNamespace(array(
                 'Smarty', 'Foo',
             ));
        }
    //配置是否报错
        public function _initError(Yaf_Dispatcher $dispatcher) {
            if ($this->config->customer->debug){
                define('DEBUG_MODE', false);
                ini_set('display_errors', 'On');
            }else{
                define('DEBUG_MODE', false);
                ini_set('display_errors', 'Off');
            }
        }
    //注册插件
        public function _initPlugin(Yaf_Dispatcher $dispatcher) {
            if (isset($this->config->customer->benchmark) && $this->config->customer->benchmark == true){
                $benchmark = new BenchmarkPlugin();
                $dispatcher->registerPlugin($benchmark);
            }
        }
    //配置路由
        public function _initRoute(Yaf_Dispatcher $dispatcher) {
            //配置文件添加
            $routes = $this->config->routes;
            if (!empty($routes)){
                $router = $dispatcher->getRouter();
                $router->addConfig($routes);
            }
            //语句添加
            $route  = new Yaf_Route_Rewrite(
                "/product/list/:id/:name",
                array(
                    "controller" => "product",
                    "action"     => "info",
                )
            );
    
            $router->addRoute('dummy', $route);
        }
    //配置memcache,类似的mongodb,redis,httpsqs等如果需要也可以统一配置
        //如果在程序里面使用的话,尽量放到Yaf_Registry里
        public function _initMemcache(Yaf_Dispatcher $dispatcher) {
            if (!empty($this->config->memcache)){
                $memcache_client = Bootstrap::memFaction($this->config->memcache);
                Yaf_Registry::set('memcache', $memcache_client);
            }
        }
    //根据配置文件返回memcache实例
        public static function memFaction($configArray){
            $flag = extension_loaded("memcached");
            if($flag){
                $memA = array();
                $memcache_client = new memcached();
            } else {
                $memcache_client = new memcache();
            }
            foreach($configArray as $mem_server){
                if($flag){
                    $memA[] = array($mem_server['host'] , $mem_server['port']);
                }else{
                    $re = $memcache_client->addServer($mem_server['host'] , $mem_server['port']);
                    if($re == true){
                        $Result = true;
                    }
                }
            }
            if($flag){
                $Result = $memcache_client->addServers($memA);
            }
            if(!$Result){
                exit('can not connect memcache server');
            }
            return $memcache_client;
        }
    public function _initSession(Yaf_Dispatcher $dispatcher) {
            if (!empty($this->config->session))
            {
                $session = $this->config->session;
                session_set_cookie_params(
                    $session->cookie_lifetime,
                    $session->cookie_path, 
                    $session->cookie_domain, 
                    $session->cookie_secure,
                    $session->cookie_httponly
                );
                session_name($session->name);
                if(!empty($session->save_handler)){
                    switch($session->save_handler){
                        case 'memcache':
                            //使用系统的
                            if(is_string($session->save_path)){
                                if(strpos($session->save_path, "://")){
                                    session_module_name('memcache');
                                }else{
                                    session_module_name('memcached');
                                }
                                session_save_path($session->save_path);
                                session_cache_expire($session->expire/60);
                            }else{
                                //使用自定义的配置不同
                                SessionMemcache::init($session->save_path,$session->expire,$session->is_json);
                            }
                            
                            break;
                        case 'files':
                            if($session->save_path){
                                if(!file_exists($session->save_path)){
                                    mkdir($session->save_path, true);
                                    if(!file_exists($session->save_path)){
                                        exit('session_path is not exits');
                                    }
                                }
                                session_module_name('files');
                                session_save_path($session->save_path);
                                session_cache_expire($session->expire/60);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    //配置数据库
        //这里做的数据库负载均衡
        public function _initDatabase(Yaf_Dispatcher $dispatcher) {
            $servers = array();
            $database = $this->config->database;
            $servers[] = $database->master->toArray();
            $slaves = $database->slaves;
            if (!empty($slaves))
            {
                foreach ($slaves as $key => $slave_server)
                {
                    $slavesA[] = $slave_server->toArray();
                }
                $servers[] = $slavesA[array_rand($slavesA)];
                if(count($servers) != 2)
                {
                    exit("slaves config wrong");
                }
            }
            Yaf_Registry::set('database', $servers);    
            if (isset($database->mysql_cache_enable) && $database->mysql_cache_enable && !defined('MYSQL_CACHE_ENABLE'))
            {
                define('MYSQL_CACHE_ENABLE', true);
            }
            if (isset($database->mysql_log_error) && $database->mysql_log_error && !defined('MYSQL_LOG_ERROR'))
            {
                define('MYSQL_LOG_ERROR', true);
            }
            if (isset($database->pdotype) && $database->pdotype && !defined('PDO_TYPE'))
            {
                define('PDO_TYPE', $database->pdotype);
            }
    
            // $pdo = new PDO(PDO_TYPE . ":dbname={$servers[0]['database']};host={$servers[0]['host']};port={$servers[0]['port']}", $servers[0]['user'], $servers[0]['password']);
            // $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
            // $pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
            // $fpdo = new FluentPDO($pdo);
            // $query = $fpdo->from('article');
            // print_r($query->fetchAll());
            // exit;
            //以前的
            //Yaf_Loader::import(APPLICATION_PATH . '/library/Db/Db.php');
            //Yaf_Loader::import(APPLICATION_PATH . '/library/Db/DbQuery.php');
        }
    //配置smarty模板引擎
        public function _initSmarty(Yaf_Dispatcher $dispatcher) {
            $smarty = new Smarty_Adapter(null, $this->config->smarty);
            $smarty->registerFunction('function', 'truncate', array('Tools', 'truncate'));
            //注册到smarty
            Yaf_Registry::set("smarty", $smarty);
            //直接替换原始模板对象
            //$dispatcher->setView($smarty);
        }

     这些方法执行顺序是从上到下了,那个方法先定义就先执行,里面有点复杂session,db后面在做介绍

  • 相关阅读:
    [笔记]一道C语言面试题:IPv4字符串转为UInt整数
    linux内核代码注释 赵炯 第三章引导启动程序
    bcd码
    2章 perl标量变量
    1章 perl入门
    perl第三章 列表和数组
    浮动 float
    文字与图像
    3.深入理解盒子模型
    4.盒子的浮动和定位
  • 原文地址:https://www.cnblogs.com/kudosharry/p/3768107.html
Copyright © 2011-2022 走看看