wiryono.lau / php-config-db
配置数据库
dev-master
2018-04-08 00:46 UTC
Requires
- php: ^5.6
- zendframework/zend-cache: ^2.7
- zendframework/zend-db: ^2.9
- zendframework/zend-serializer: ^2.8
- zendframework/zend-servicemanager: ^3.3
- zfcampus/zf-console: ^1.4
This package is auto-updated.
Last update: 2024-09-16 07:59:19 UTC
README
在数据库中管理应用程序配置,每个配置由用户空间分开。
配置可以保存到文件/数据库,如MySQL。
需要ZF3
安装
composer require wiryonolau/php-config-db
使用Docker进行测试
#Start php-cli docker container make start #Build composer dependency docker exec -it php-config-db composer-update docker exec -it php-config-db gosu 1000 bin/configdb #If you want to use mysql as backend run this make start-mysql make connect docker exec -it php-config-db gosu 1000 bin/configdb initdb
用法
使用键 "configdb" 在您的应用程序中指定配置
配置参数
- config_adapter
- ConfigDB\Adapter\FileConfigAdapter : 配置保存于文件夹中
- ConfigDB\Adapter\DatabaseConfigAdapter : 配置保存于指定的数据库中
- 实现 ConfigDB\Adapter\ConfigAdapterInterface 的任何类
- database_adapter
- 数据库配置数组 - 检查 Zend\Db\Adapter\Adapter 格式
- 实现 Zend\Db\Adapter\AdapterInterface 的任何类
- database_table : 保存配置数据库的表名
- cache_config : (bool) 是否缓存配置
- cache_adapter : 缓存适配器
- 缓存配置数组
- 实现 Zend\Cache\Storage\StorageInterface 的任何类
- default_userspace : 默认用户空间,默认为 "global"
示例
MyModule\config\configdb.config.php
<?php return [ "configdb" => [ "config_adapter" => \ConfigDB\Adapter\DatabaseConfigAdapter::class, "database_adapter" => [ 'driver' => 'Pdo_Mysql', /*Docker mysql*/ 'hostname' => 'php-config-db-mysql', 'port' => 3306, 'database' => 'configdb', 'username' => 'configdb', 'password' => '888888', 'driver_options' => [ \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'', \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_EMULATE_PREPARES => true, \PDO::MYSQL_ATTR_LOCAL_INFILE => true ] ], "database_table" => "configdb", "cache_config" => false, "cache_adapter" => [ 'adapter' => array( 'name' => 'filesystem', 'options' => array( 'dir_level' => 1, 'cache_dir' => 'data/cache', 'namespace' => 'configdb', 'ttl' => 300 ) ), 'plugins' => array( 'serializer', 'exception_handler' => array( 'throw_exceptions' => true ) ) ], "default_userspace" => "global" ], ];
MyModule\src\Controller\Factory\MyClassFactory.php
namespace MyModule\Controller\Factory; use Interop\Container\ContainerInterface; use Zend\ServiceManager\Factory\FactoryInterface; class MyClassFactory implements FactoryInterface { public function __invoke($container, $requestedName, $options) { $configDbService = $container->get(\ConfigDB\Service\ConfigDbService::class); return new MyClass($configDbService); } }
MyModule\src\Controller\MyClass.php
namespace MyModule\Controller; class MyClass { public function __construct($configDbService) { #to get config $configDbService->getConfig($shcemadir, $key, $userspace); #to set config $configDbService->setConfig($schemadir, $key, $value, $value_type, $userspace) } }
返回值
所有值都返回为 ConfigDB\Model\EntryModel 或 ConfigDB\Model\EntriesModel