Skip to content

Commit

Permalink
feat: Add config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
linc committed Dec 27, 2024
1 parent a780e90 commit c20d9e9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,36 @@ public function getConnectionAlias(string $alias): array
foreach ($this->config['connections'] as $connection) {
if ($alias === $connection['alias']) {
$result = $connection;
break;
}
}

$this->validateConnectionInfo($alias, $result);

return $result;
}

/**
* Validate config has required info.
*
* @param string $alias
* @param array $info
*/
protected function validateConnectionInfo(string $alias, array $info): void
{
// Type is required.
if (empty($info['type'])) {
trigger_error('Config error: No connection `type` for alias "' . $alias . '"', E_USER_ERROR);
}

// Database required fields.
if ($info['type'] === 'database') {
foreach (['adapter', 'host','name','user'] as $required_field) {
if (!array_key_exists($required_field, $info)) {
trigger_error('Config error: Database `' . $required_field .
'` missing for alias "' . $alias . '"', E_USER_ERROR);
}
}
}
}
}
4 changes: 0 additions & 4 deletions src/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ public function __construct(string $alias = '')
$info = Config::getInstance()->getTestConnection();
}

if (!isset($info['type'])) {
trigger_error('Config error: Connection `type` not found for "' . $alias . '"', E_USER_ERROR);
}

$this->setInfo($info);
$this->setType($info['type']);

Expand Down

0 comments on commit c20d9e9

Please sign in to comment.