rastusik / zend-yaml-config
此包已被废弃且不再维护。没有建议的替代包。
允许在ZF2中使用YAML配置文件
dev-master
2014-12-21 14:59 UTC
Requires
- php: >=5.3.3
- symfony/yaml: 2.*@dev
Requires (Dev)
- phpmd/phpmd: @stable
- squizlabs/php_codesniffer: 2.*
- symfony/var-dumper: dev-master
- zendframework/zendframework: dev-master
This package is auto-updated.
Last update: 2023-02-27 00:27:25 UTC
README
模块,提供在ZF2中使用YAML配置文件的能力。
许多开发者不喜欢ZF2,原因很多,其中之一是必须使用PHP数组进行配置。使用此模块后,您可以摆脱所有的PHP数组,并使用更简洁的格式,换句话说,您可以像这样更改文件
<?php return [ 'modules' => [ 'ZendDeveloperTools', 'ZendYamlConfig', 'Application', ], 'module_listener_options' => [ 'config_glob_paths' => [ 'config/autoload/{,*.}{global,local}.php', ], 'module_paths' => [ './module', './vendor', ], ], ];
变成这样
modules: - ZendDeveloperTools - ZendYamlConfig - Application module_listener_options: config_glob_paths: - config/autoload/{,*.}{global,local}.yaml module_paths: - ./module - ./vendor
注意行数的差异。YAML还禁止您在配置文件中写入可执行代码,因此配置文件只会做它应该做的事情(没有任何匿名回调)。
要求
- ZendFramework 2.
- 任何类似于ZendSkeletonApplication的应用程序。
安装
- 将
"rastusik/zend-yaml-config": "dev-master"
添加到您的composer.json
- 运行
php composer.phar install
- 在您的
config/application.config.php
中启用该模块,通过将ZendYamlConfig
添加到modules
中(注意:模块必须在所有使用YAML配置文件的模块之前加载)
使用
如果要在模块级别(您模块的配置文件)读取配置文件,则您的模块应如下所示
<?php class Module implements DependencyIndicatorInterface, InitProviderInterface { /** * @var ZendYamlConfig\Service\YamlFileParser */ protected $yamlParser; /** * Expected to return an array of modules on which the current one depends on * - this module dependes on ZendYamlConfig * * @return array */ public function getModuleDependencies() { return ['ZendYamlConfig']; } /** * Initialization of the module - retrieval of the YAML file parser */ public function init(ModuleManagerInterface $manager) { if (!$manager instanceof ModuleManager) { return; } $event = $manager->getEvent(); /* @var $serviceManager ServiceManager */ $serviceManager = $event->getParam('ServiceManager'); $this->yamlParser = $serviceManager->get('yamlParser'); } /** * Config array retrieval from the YAML file */ public function getConfig() { $config = $this->yamlParser->parseFile(__DIR__ . '/config/module.config.yaml'); return $config; } }
也可以使用YAML文件作为项目的主要配置文件。主要配置文件应类似于以下示例
modules: - ZendDeveloperTools - ZendYamlConfig - Application module_listener_options: config_glob_paths: - config/autoload/{,*.}{global,local}.yaml module_paths: - ./module - ./vendor
然后,应用必须按照以下方式初始化
<?php $yamlParser = ZendYamlConfig\Service\YamlFileParserFactory::getYamlFileParser(); $config = $yamlParser->parseFile(__DIR__ . '/../config/application.config.yaml'); Zend\Mvc\Application::init($config)->run();
功能
文件解析器将YAML文件中出现的每个__DIR__
常量替换为YAML文件父目录的路径(基本上与通常PHP文件中的__DIR__
常量功能相同)。
待办事项
YAML文件的解析速度不如使用纯PHP文件,因此需要实现缓存层。即将推出。