Zend Framework教程之Application用法實(shí)例詳解
本文實(shí)例講述了Zend Framework教程之Application用法。分享給大家供大家參考,具體如下:
Zend_Application是Zend Framework的核心組件。Zend_Application為Zend Framework應(yīng)用程序提供基本功能,是程序的入口點(diǎn)。它的主要功能有兩個(gè):裝載配置PHP環(huán)境(包括自動(dòng)加載),并引導(dǎo)應(yīng)用程序。
通常情況下,通過配置選項(xiàng)配置Zend_Application構(gòu)造器,但也可以完全使用自定義方法配置。以下是兩個(gè)使用用例。
Zend_Application配置選項(xiàng)
構(gòu)造函數(shù):
/**
* 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.使用配置數(shù)組
常見配置選項(xiàng)
| Option | Description |
|---|---|
| phpSettings |
用于配置php.ini選項(xiàng),要求是數(shù)組,數(shù)組的鍵應(yīng)該是選項(xiàng)的的key. |
| includePaths |
把附加的路徑加入到include_path,要求是數(shù)組 |
| autoloaderNamespaces |
給Zend_Loader_Autoloader注冊(cè)附加命名空間,為數(shù)組 |
| bootstrap |
可以是設(shè)置bootstrap引導(dǎo)類的路徑的字符串,也可以是數(shù)組,數(shù)組元素要求為 'path' 和 'class' |
注意:
選項(xiàng)名稱不區(qū)分大小寫。
Zend_Application的方法
| Method | Return Value | Parameters | Description |
|---|---|---|---|
| __construct( $environment, $options = null) |
Void |
|
構(gòu)造函數(shù)。 用于初始化配置對(duì)象。 實(shí)例化Zend_Loader_Autoloader。 通過傳遞給構(gòu)造函數(shù)選項(xiàng)然后傳遞給setOptions()方法。 |
| getEnvironment() | String | N/A |
獲取環(huán)境配置 |
| getAutoloader() | Zend_Loader_Autoloader | N/A |
獲取Zend_Loader_Autoloader實(shí)例 |
| setOptions(array $options) | Zend_Application |
|
所有選項(xiàng)都存儲(chǔ)在引用內(nèi)部,并多次調(diào)用該方法來合并選項(xiàng)。 會(huì)根據(jù)選項(xiàng)生產(chǎn)對(duì)于的setter方法。 例如,選項(xiàng)“phpSettings”對(duì)應(yīng)setPhpSettings()。 (選項(xiàng)名稱不區(qū)分大小寫。) |
| getOptions() | Array | N/A |
|
| hasOption($key) | Boolean |
|
key不區(qū)分大小寫。 |
| getOption($key) | Mixed |
|
key不區(qū)分大小寫。如果不存在返回 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 |
獲取注冊(cè)的bootstrap實(shí)例. |
| bootstrap() | Void | N/A |
調(diào)用 bootstrap的bootstrap() 引導(dǎo)應(yīng)用. |
| run() | Void | N/A |
調(diào)用bootstrap的 run()運(yùn)行應(yīng)用 |
配置舉例:
默認(rèn):
// 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();
}
}
更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP中關(guān)于PDO數(shù)據(jù)訪問抽象層的功能操作實(shí)例
下面小編就為大家?guī)硪黄狿HP中關(guān)于PDO數(shù)據(jù)訪問抽象層的功能操作實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
ThinkPHP5中如何實(shí)現(xiàn)模板完全靜態(tài)化詳解
這篇文章主要為大家介紹了ThinkPHP5中如何實(shí)現(xiàn)模板完全靜態(tài)化詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
PHP和MYSQL實(shí)現(xiàn)分頁導(dǎo)航思路詳解
這篇文章主要介紹了PHP和MYSQL實(shí)現(xiàn)分頁導(dǎo)航思路詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04
利用中國(guó)天氣預(yù)報(bào)接口實(shí)現(xiàn)簡(jiǎn)單天氣預(yù)報(bào)
這篇文章主要介紹了利用中國(guó)天氣預(yù)報(bào)接口實(shí)現(xiàn)簡(jiǎn)單天氣預(yù)報(bào)的示例,大家參考使用吧2014-01-01
PHP如何通過傳引用的思想實(shí)現(xiàn)無限分類(代碼簡(jiǎn)單)
本文給大家介紹php引用傳遞,如何通過引用出的思想實(shí)現(xiàn)無限分類,代碼超簡(jiǎn)單,需要的朋友可以借鑒下2015-10-10

