Skip to content

Commit

Permalink
add more array config support
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxyw committed Mar 16, 2022
1 parent be4583f commit 4be3044
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ public function __construct()
];
}

/**
* Create a config instance from array.
*
* @param array $config
* @return static
*/
public static function fromArray(array $config): self
{
$instance = new self();
if (count($config['servers'])) {
foreach ($config['servers'] as $name => $server_config) {
$instance->addServer($name, ServerConfig::fromArray($server_config));
}
}
if (count($config['default_parsers'])) {
foreach ($config['default_parsers'] as $name => $parser_class) {
$instance->setDefaultParser($name, $parser_class);
}
}
return $instance;
}

/**
* Add server config.
*
Expand Down
23 changes: 22 additions & 1 deletion src/ServerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,35 @@ class ServerConfig
* @param array $config
* @return static
*/
#[Pure]
public static function fromArray(array $config): self
{
$server_config = new self();
$server_config->driver = $config['driver'];
$server_config->host = $config['host'];
$server_config->port = $config['port'];
$server_config->password = $config['password'];
if (isset($config['commands'])) {
if (is_a($config['commands'], CommandsInterface::class, true)) {
$server_config->commands = new $config['commands']();
} else {
$internal = [
'bukkit' => Commands\BukkitCommands::class,
// 'bungeecord' => Commands\BungeeCommands::class,
];
$server_config->commands = new $internal[$config['driver']]();
}
}
if (isset($config['permission_commands'])) {
if (is_a($config['permission_commands'], PermissionCommandsInterface::class, true)) {
$server_config->permissionCommands = new $config['permission_commands']();
} else {
$internal = [
'groupmanager' => Commands\PermissionCommands\GroupManagerPermissionCommands::class,
'upc' => Commands\PermissionCommands\UltraPermissionsPermissionCommands::class,
];
$server_config->permissionCommands = new $internal[$config['driver']]();
}
}
return $server_config;
}

Expand Down

0 comments on commit 4be3044

Please sign in to comment.