Zend Framework教程之Application用法实例详解
本文实例讲述了zend framework教程之application用法。分享给大家供大家参考,具体如下:
zend_application是zend framework的核心组件。zend_application为zend framework应用程序提供基本功能,是程序的入口点。它的主要功能有两个:装载配置php环境(包括自动加载),并引导应用程序。
通常情况下,通过配置选项配置zend_application构造器,但也可以完全使用自定义方法配置。以下是两个使用用例。
zend_application配置选项
构造函数:
/** * constructor * * initialize application. potentially initializes include_paths, php * settings, and bootstrap class. * * @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'; $this->_autoloader = zend_loader_autoloader::getinstance(); 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); } }
zend_application配置方法
1.使用配置文件
2.使用配置数组
常见配置选项
option | description |
---|---|
phpsettings |
用于配置php.ini选项,要求是数组,数组的键应该是选项的的key. |
includepaths |
把附加的路径加入到include_path,要求是数组 |
autoloadernamespaces |
给zend_loader_autoloader注册附加命名空间,为数组 |
bootstrap |
可以是设置bootstrap引导类的路径的字符串,也可以是数组,数组元素要求为 'path' 和 'class' |
注意:
选项名称不区分大小写。
zend_application的方法
method | return value | parameters | description |
---|---|---|---|
__construct( $environment, $options = null) |
void |
|
构造函数。 用于初始化配置对象。 实例化zend_loader_autoloader。 通过传递给构造函数选项然后传递给setoptions()方法。 |
getenvironment() | string | n/a |
获取环境配置 |
getautoloader() | zend_loader_autoloader | n/a |
获取zend_loader_autoloader实例 |
setoptions(array $options) | zend_application |
|
所有选项都存储在引用内部,并多次调用该方法来合并选项。 会根据选项生产对于的setter方法。 例如,选项“phpsettings”对应setphpsettings()。 (选项名称不区分大小写。) |
getoptions() | array | n/a |
|
hasoption($key) | boolean |
|
key不区分大小写。 |
getoption($key) | mixed |
|
key不区分大小写。如果不存在返回 null |
setphpsettings(array $settings, $prefix = '') | zend_application |
|
|
setautoloadernamespaces(array $namespaces) | zend_application |
|
|
setbootstrap($path, $class = null) | zend_application |
|
|
getbootstrap() | null |zend_application_bootstrap_bootstrapper | n/a |
获取注册的bootstrap实例. |
bootstrap() | void | n/a |
调用 bootstrap的bootstrap() 引导应用. |
run() | void | n/a |
调用bootstrap的 run()运行应用 |
配置举例:
默认:
// create application, bootstrap, and run $application = new zend_application( application_env, application_path . '/configs/application.ini' ); $application->bootstrap() ->run();
源代码
<?php class zend_application { /** * constructor * * initialize application. potentially initializes include_paths, php * settings, and bootstrap class. * * @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'; $this->_autoloader = zend_loader_autoloader::getinstance(); 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'])) { $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'); } } 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) { } /** * 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) { // 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); if (!$this->_bootstrap instanceof zend_application_bootstrap_bootstrapper) { throw new zend_application_exception('bootstrap class does not implement zend_application_bootstrap_bootstrapper'); } return $this; } /** * get bootstrap object * * @return zend_application_bootstrap_bootstrapabstract */ public function getbootstrap() { if (null === $this->_bootstrap) { $this->_bootstrap = new zend_application_bootstrap_bootstrap($this); } 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 = pathinfo($file, pathinfo_extension); $suffix = ($suffix === 'dist') ? pathinfo(basename($file, ".$suffix"), pathinfo_extension) : $suffix; switch (strtolower($suffix)) { case 'ini': $config = new zend_config_ini($file, $environment); break; case 'xml': $config = new zend_config_xml($file, $environment); break; case 'json': $config = new zend_config_json($file, $environment); break; case 'yaml': case 'yml': $config = new zend_config_yaml($file, $environment); break; case 'php': 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(); } }
更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
推荐阅读
-
Zend Framework教程之Application用法实例详解
-
Zend Framework入门教程之Zend_Registry组件用法详解
-
Zend Framework入门教程之Zend_Config组件用法详解
-
Zend Framework教程之Loader以及PluginLoader用法详解
-
Zend Framework教程之Autoloading用法详解
-
Zend Framework教程之资源(Resources)用法实例详解
-
Zend Framework教程之前端控制器Zend_Controller_Front用法详解
-
Zend Framework路由器用法实例详解
-
Zend Framework教程之Resource Autoloading用法实例
-
Zend Framework教程之Application和Bootstrap用法详解