• 从框架代码看zendframework支持配置项


     

    配置项生效的主要方法:Zend_Application的setOptions()方法

    代码片段如下:

            if (!empty($options['phpsettings'])) {
                $this->setPhpSettings($options['phpsettings']);
            }
    
            if (!empty($options['includepaths'])) {
                $this->setIncludePaths($options['includepaths']);
            }
    
            if (!empty($options['autoloadernamespaces'])) {
                $this->setAutoloaderNamespaces($options['autoloadernamespaces']);
            }
    
            if (!empty($options['autoloaderzfpath'])) {
                $autoloader = $this->getAutoloader();
                if (method_exists($autoloader, 'setZfPath')) {
                    $zfPath    = $options['autoloaderzfpath'];
                    $zfVersion = !empty($options['autoloaderzfversion'])
                               ? $options['autoloaderzfversion']
                               : 'latest';
                    $autoloader->setZfPath($zfPath, $zfVersion);
                }
            }
    
            if (!empty($options['bootstrap'])) {
                $bootstrap = $options['bootstrap'];
    
                if (is_string($bootstrap)) {
                    $this->setBootstrap($bootstrap);
                } elseif (is_array($bootstrap)) {
                    if (empty($bootstrap['path'])) {
                        throw new Zend_Application_Exception('No bootstrap path provided');
                    }
    
                    $path  = $bootstrap['path'];
                    $class = null;
    
                    if (!empty($bootstrap['class'])) {
                        $class = $bootstrap['class'];
                    }
    
                    $this->setBootstrap($path, $class);
                } else {
                    throw new Zend_Application_Exception('Invalid bootstrap information provided');
                }
            }
    

    setPhpSettings()方法

        public function setPhpSettings(array $settings, $prefix = '')
        {
            foreach ($settings as $key => $value) {
                $key = empty($prefix) ? $key : $prefix . $key;
                if (is_scalar($value)) {
                    ini_set($key, $value);
                } elseif (is_array($value)) {
                    $this->setPhpSettings($value, $key . '.');
                }
            }
    
            return $this;
        }
    

     假如我们想在config.ini 配置文件中打开php错误显示,那么可以配置

    phpSettings.display_errors  = 1
    

    setIncludePaths方法:

        public function setIncludePaths(array $paths)
        {
            $path = implode(PATH_SEPARATOR, $paths);
            set_include_path($path . PATH_SEPARATOR . get_include_path());
            return $this;
        }
    

    如果想将项目根目录下的foo/bar 目录加入进来。config.ini配置文件可以这样写

    includepaths[]=APPLICATION_PATH "/../foo/bar"
    

     打印下看看是否已加入

    var_dump(get_include_path());exit;
    

    setAutoloaderNamespaces()

        public function setAutoloaderNamespaces(array $namespaces)
        {
            $autoloader = $this->getAutoloader();
    
            foreach ($namespaces as $namespace) {
                $autoloader->registerNamespace($namespace);
            }
    
            return $this;
        }
    

    config.ini配置文件中可以这样写 

    autoloadernamespaces[]="Foo_Bar"
    

     

     
  • 相关阅读:
    quartz 定时调度持久化数据库配置文件
    springboot项目下mvnw文件的作用
    mysql安装版卸载,解压版安装
    idea提示,格式化代码,清除不使用的包快捷键,maven自动导jar包
    JavaScript中call,apply,bind方法
    彻底理解js中this的指向
    Gradle系列之从init.gradle说起
    响应式网页设计简单入门
    开启MySQL远程访问权限 允许远程连接
    https原理
  • 原文地址:https://www.cnblogs.com/kala00k/p/16184193.html
Copyright © 2020-2023  润新知