设为首页 收藏本站
查看: 351|回复: 0

[经验分享] zend源代码分析Application.php

[复制链接]

尚未签到

发表于 2015-8-24 09:01:33 | 显示全部楼层 |阅读模式
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category   Zend
* @package    Zend_Application
* @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license    http://framework.zend.com/license/new-bsd     New BSD License
* @version    $Id: Application.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @category   Zend
* @package    Zend_Application
* @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license    http://framework.zend.com/license/new-bsd     New BSD License
*/
class Zend_Application
{
/**
* Autoloader to use
*
* @var Zend_Loader_Autoloader
*/
protected $_autoloader;//自动加载
/**
* Bootstrap
*
* @var Zend_Application_Bootstrap_BootstrapAbstract
*/
protected $_bootstrap;//bootstrap
/**
* Application environment
*
* @var string
*/
protected $_environment;//环境
/**
* Flattened (lowercase) option keys
*
* @var array
*/
protected $_optionKeys = array();//选项key数组
/**
* Options for Zend_Application
*
* @var array
*/
protected $_options = array();//选项数组
/**
* Constructor构造器
*
* Initialize application. Potentially initializes include_paths, PHP
* settings, and bootstrap class.
* 应用初始化。初始化include_path,php相关inisetting,和bootstrap类
*
* @param  string                   $environment
* @param  string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
* @throws Zend_Application_Exception When invalid options are provided
* @return void
*/
public function __construct($environment, $options = null)
{
$this->_environment = (string) $environment;
require_once 'Zend/Loader/Autoloader.php';//加载zend_loader类
$this->_autoloader = Zend_Loader_Autoloader::getInstance();//单例模式实例化autoloader对象
if (null !== $options) {//选项数组不为空时
if (is_string($options)) {//字符串类型选项数组
$options = $this->_loadConfig($options);//加载选项数组
} elseif ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (!is_array($options)) {
throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');
}
$this->setOptions($options);
}
}
/**
* Retrieve current environment
* 获取当前环境
* @return string
*/
public function getEnvironment()
{
return $this->_environment;
}
/**
* Retrieve autoloader instance
*
* @return Zend_Loader_Autoloader
*/
public function getAutoloader()
{
return $this->_autoloader;
}
/**
* Set application options
*
* @param  array $options
* @throws Zend_Application_Exception When no bootstrap path is provided
* @throws Zend_Application_Exception When invalid bootstrap information are provided
* @return Zend_Application
*/
public function setOptions(array $options)//设置选项
{
if (!empty($options['config'])) {
if (is_array($options['config'])) {
$_options = array();
foreach ($options['config'] as $tmp) {
$_options = $this->mergeOptions($_options, $this->_loadConfig($tmp));
}
$options = $this->mergeOptions($_options, $options);
} else {
$options = $this->mergeOptions($this->_loadConfig($options['config']), $options);
}
}
$this->_options = $options;
$options = array_change_key_case($options, CASE_LOWER);
$this->_optionKeys = array_keys($options);
if (!empty($options['phpsettings'])) {//option中有phpsetting节点
$this->setPhpSettings($options['phpsettings']);//做phpini设置
}
if (!empty($options['includepaths'])) {//option中有includepath节点
$this->setIncludePaths($options['includepaths']);//设置includepath
}
if (!empty($options['autoloadernamespaces'])) {//option中有名字空间节点
$this->setAutoloaderNamespaces($options['autoloadernamespaces']);//设置自动加载名字空间
}
if (!empty($options['autoloaderzfpath'])) {//option中有自动加载zf路径
$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
$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');
}
}
return $this;
}
/**
* Retrieve application options (for caching)
*
* @return array
*/
public function getOptions()
{
return $this->_options;
}
/**
* Is an option present?
*
* @param  string $key
* @return bool
*/
public function hasOption($key)
{
return in_array(strtolower($key), $this->_optionKeys);
}
/**
* Retrieve a single option
*
* @param  string $key
* @return mixed
*/
public function getOption($key)
{
if ($this->hasOption($key)) {
$options = $this->getOptions();
$options = array_change_key_case($options, CASE_LOWER);
return $options[strtolower($key)];
}
return null;
}
/**
* Merge options recursively
*
* @param  array $array1
* @param  mixed $array2
* @return array
*/
public function mergeOptions(array $array1, $array2 = null)
{
if (is_array($array2)) {
foreach ($array2 as $key => $val) {
if (is_array($array2[$key])) {
$array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
? $this->mergeOptions($array1[$key], $array2[$key])
: $array2[$key];
} else {
$array1[$key] = $val;
}
}
}
return $array1;
}
/**
* Set PHP configuration settings
*
* @param  array $settings
* @param  string $prefix Key prefix to prepend to array values (used to map . separated INI values)
* @return Zend_Application
*/
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;
}
/**
* Set include path
*
* @param  array $paths
* @return Zend_Application
*/
public function setIncludePaths(array $paths)
{
$path = implode(PATH_SEPARATOR, $paths);
set_include_path($path . PATH_SEPARATOR . get_include_path());
return $this;
}
/**
* Set autoloader namespaces
*
* @param  array $namespaces
* @return Zend_Application
*/
public function setAutoloaderNamespaces(array $namespaces)
{
$autoloader = $this->getAutoloader();
foreach ($namespaces as $namespace) {
$autoloader->registerNamespace($namespace);
}
return $this;
}
/**
* Set bootstrap path/class
*
* @param  string $path
* @param  string $class
* @return Zend_Application
*/
public function setBootstrap($path, $class = null)//设置bootstrap
{
// setOptions() can potentially send a null value; specify default
// here
if (null === $class) {
$class = 'Bootstrap';
}
if (!class_exists($class, false)) {
require_once $path;
if (!class_exists($class, false)) {
throw new Zend_Application_Exception('Bootstrap class not found');
}
}
$this->_bootstrap = new $class($this);//实例化一个bootstrap对象,设置为application对象的属性
if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {
throw new Zend_Application_Exception('Bootstrap class does not implement Zend_Application_Bootstrap_Bootstrapper');
}
return $this;//返回bootstrap对象
}
/**
* Get bootstrap object
*
* @return Zend_Application_Bootstrap_BootstrapAbstract
*/
public function getBootstrap()//获取bootstrap类
{
if (null === $this->_bootstrap) {//当前bootstrap类为空
$this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);//实例化bootstrap类,传入参数当前的application对象
}
return $this->_bootstrap;
}
/**
* Bootstrap application
*
* @param  null|string|array $resource
* @return Zend_Application
*/
public function bootstrap($resource = null)
{
$this->getBootstrap()->bootstrap($resource);
return $this;
}
/**
* Run the application
*
* @return void
*/
public function run()
{
$this->getBootstrap()->run();
}
/**
* Load configuration file of options
* 加载配置文件中的选项
* @param  string $file
* @throws Zend_Application_Exception When invalid configuration file is provided
* @return array
*/
protected function _loadConfig($file)
{
$environment = $this->getEnvironment();//加载当前工作环境到变量
$suffix      = strtolower(pathinfo($file, PATHINFO_EXTENSION));//获取config文件的后缀名
switch ($suffix) {
case 'ini':
$config = new Zend_Config_Ini($file, $environment);//加载ini类型的配置文件
break;
case 'xml':
$config = new Zend_Config_Xml($file, $environment);//加载xml类型的配置文件
break;
case 'php'://与inc格式的配置文件相同
case 'inc':
$config = include $file;//直接包含文件
if (!is_array($config)) {
throw new Zend_Application_Exception('Invalid configuration file provided; PHP file does not return array value');//文件采用数组配置,若非数组,则抛出异常
}
return $config;
break;
default:
throw new Zend_Application_Exception('Invalid configuration file provided; unknown config type');
}
return $config->toArray();
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-103244-1-1.html 上篇帖子: 编译PHP并与Ngnix整合 下篇帖子: PHP 程序员学数据结构与算法之《栈》
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表