在特定命名空间下定义配置值?
Defining configuration values under specific namespaces?
我正在编写一个框架,并且刚刚添加了命名空间。我在转换所有内容时没有遇到任何问题,除了配置值。
我可以很容易地定义常量,所以单个值不是问题...但是值数组呢?
以这个数组为例,就是给Capsule添加连接的配置数组...
[
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'DB_NAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => ''
]
我一直在使用类似这样的文件进行数据库配置...
<?php
namespace BareBones;
use Illuminate\Database\Capsule\Manager as Capsule;
$BareBonesCapsule = new Capsule;
$BareBonesCapsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'DB_NAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => ''
]);
$BareBonesCapsule->setAsGlobal();
$BareBonesCapsule->bootEloquent();
use Illuminate\Database\Schema\Blueprint as Blueprint;
use Illuminate\Database\Eloquent\Model as Eloquent;
我要从我的 App
class 开始启动 Eloquent。这将阻止全局变量执行此操作。虽然不太可能使用 $BareBonesCapsule
,但我仍然希望保持我的框架干净并将所有内容保留在它的命名空间中。
我可以在配置文件中声明一堆常量...
<?php
namesapce BareBones;
define("driver", "mysql");
define("host", "localhost");
define("database", "DB_NAME");
/* etc... */
这看起来不太干净,我假设有更好的方法来做到这一点。其他框架如何处理这个问题,在保持与全局命名空间分离的同时我有哪些替代配置方法?
您可以使用 .ini 文件并将它们分成几个部分,如下所示:
; This is a sample configuration file
; Comments start with ';', as in php.ini
[first_section]
one = 1
five = 5
animal = BIRD
[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"
[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"
urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"
发件人:http://php.net/manual/en/function.parse-ini-file.php
Symfony 支持 XML、PHP 和 YML,并允许您编写自己的配置解码器。
我正在编写一个框架,并且刚刚添加了命名空间。我在转换所有内容时没有遇到任何问题,除了配置值。
我可以很容易地定义常量,所以单个值不是问题...但是值数组呢?
以这个数组为例,就是给Capsule添加连接的配置数组...
[
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'DB_NAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => ''
]
我一直在使用类似这样的文件进行数据库配置...
<?php
namespace BareBones;
use Illuminate\Database\Capsule\Manager as Capsule;
$BareBonesCapsule = new Capsule;
$BareBonesCapsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'DB_NAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => ''
]);
$BareBonesCapsule->setAsGlobal();
$BareBonesCapsule->bootEloquent();
use Illuminate\Database\Schema\Blueprint as Blueprint;
use Illuminate\Database\Eloquent\Model as Eloquent;
我要从我的 App
class 开始启动 Eloquent。这将阻止全局变量执行此操作。虽然不太可能使用 $BareBonesCapsule
,但我仍然希望保持我的框架干净并将所有内容保留在它的命名空间中。
我可以在配置文件中声明一堆常量...
<?php
namesapce BareBones;
define("driver", "mysql");
define("host", "localhost");
define("database", "DB_NAME");
/* etc... */
这看起来不太干净,我假设有更好的方法来做到这一点。其他框架如何处理这个问题,在保持与全局命名空间分离的同时我有哪些替代配置方法?
您可以使用 .ini 文件并将它们分成几个部分,如下所示:
; This is a sample configuration file
; Comments start with ';', as in php.ini
[first_section]
one = 1
five = 5
animal = BIRD
[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"
[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"
urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"
发件人:http://php.net/manual/en/function.parse-ini-file.php
Symfony 支持 XML、PHP 和 YML,并允许您编写自己的配置解码器。