Zend_Application 是 Zend Framework 1.x 應用的總入口,承擔「環境初始化、配置加載、自動加載引導、Bootstrap 編排、應用運行」五大核心職責。精通其構造選項、配置加載策略與擴展點,是構建健壯應用的基石。
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(); // Zend_Config 對象
} elseif (!is_array($options)) {
throw new Zend_Application_Exception('Invalid options');
}
$this->setOptions($options);
}
}
$app = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
支援格式:.ini、.xml、.json、.yaml/.yml、.php/.inc(返回陣列)。
_loadConfig() 根據副檔名自動選擇解析器(Zend_Config_Ini/Xml/Json/Yaml)。
$app = new Zend_Application('production', array(
'bootstrap' => array('path' => 'Bootstrap.php', 'class' => 'Bootstrap'),
'resources' => array('db' => array(...), 'view' => array(...)),
));
$app = new Zend_Application('production', array(
'config' => array(
'/etc/myapp/base.ini',
'/etc/myapp/local.ini',
APPLICATION_PATH . '/configs/application.ini'
)
));
按順序遞歸合併,後者覆蓋前者,極適合「基礎配置 + 環境覆蓋 + 本地機密」分層管理。
| 選項 | 類型 | 說明 |
|---|---|---|
| phpSettings | array | 映射 ini_set(),鍵值對應 php.ini 指令(如 display_errors=1) |
| includePaths | array | 追加到 include_path,自動處理 PATH_SEPARATOR |
| autoloaderNamespaces | array | 註冊到 Zend_Loader_Autoloader 的額外命名空間前綴 |
| bootstrap | string|array | Bootstrap 類路徑或 array('path'=>..., 'class'=>...) |
| appnamespace | string | 應用命名空間,驅動 Zend_Application_Module_Autoloader |
| config | string|array | 配置文件路徑或路徑陣列(多文件合併) |
| autoloaderZfPath | string | ZF 庫路徑,配合 autoloaderZfVersion 啟用 ZF 版本管理 |
注意:選項名稱不區分大小寫,內部統一轉為小寫處理。
setOptions(array $options):核心配置分發入口,按鍵名匹配 set{Key}() 方法(如 phpSettings → setPhpSettings())getOptions() / getOption($key) / hasOption($key):配置查詢setPhpSettings() / setIncludePaths() / setAutoloaderNamespaces() / setBootstrap():具體設置器getBootstrap():懶加載實例化 Bootstrap(默認 Zend_Application_Bootstrap_Bootstrap)bootstrap($resource) / run():委派給 Bootstrap[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
resources.view[] =
resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/../application/views/helpers"
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.username = "app"
resources.db.params.password = "secret"
resources.db.params.dbname = "myapp"
resources.db.isDefaultTableAdapter = true
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
mergeOptions()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;
}
深度遞歸合併,保留數值類型,陣列鍵不覆蓋整個子陣列。
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;
}
支援巢狀陣列自動展開為點號分隔指令(如 phpSettings.date.timezone = "Asia/Shanghai")。
: production)減少重複local.ini(.gitignore)或環境變量注入,不入版本庫config 陣列合併setOptions() 中異常直接拋出,避免帶病運行