我尝试解析一个ini文件来从中加载PHP类,但是每当parse_ini_file解析该文件时,保存内容的变量总是只有“1”。
这是我的代码:
private $plugins = array();
public function __construct() {
Logger::log("Loading plugins");
Logger::debug("Loading " . APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini");
if (file_exists(APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini")) {
$data = parse_ini_file(APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini", true);
foreach ($data as $k => $d) {
if (class_exists($k)) {
$plugin = $this->createPlugin($k);
if ($plugin instanceof Plugin) {
$this->init($plugin, $d);
} else {
Logger::error("Plugin doesn't implements interface Plugin");
}
} else {
Logger::error("Can't load plugin $k");
}
}
} else {
Logger::error(APP_ROOT . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "plugins.ini doesn't exists");
}
}
public function createPlugin($name) {
$plugin = unserialize(
sprintf(
'O:%d:"%s":0:{}', strlen($name), $name
)
);
return $plugin;
}
public function init(Plugin $plugin, $data) {
try {
Logger::log("Adding plugin " . get_class($plugin));
Logger::debug("Calling init");
$plugin->init($data);
$this->plugins[get_class($plugin)] = $plugin;
} catch (PluginException $ex) {
Logger::error("Error while init plugin");
}
}
ini文件:
[\eBot\Plugins\Official\MissionChecker]
url=http://someurl
我试图在
之后添加一条日志消息foreach ($data as $k => $d) {
但这条线从未被调用过,因此$ data必须是空的,这是怎么回事? var_dump和print_r($ data)显示“1”。
答案 0 :(得分:1)
我通过将inl文件中的url放在引号中来解决问题。
[\eBot\Plugins\Official\MissionChecker]
url="http://someurl"