Skip to content

Commit

Permalink
Checked out src/config/Config.php as of commit 62938. I must have acc…
Browse files Browse the repository at this point in the history
…identally modified that file during work on issue #49.
  • Loading branch information
mjordan committed Jul 27, 2015
1 parent 7620fd5 commit a57cb58
Showing 1 changed file with 98 additions and 1 deletion.
99 changes: 98 additions & 1 deletion src/config/Config.php
Expand Up @@ -38,4 +38,101 @@ public function __construct($configPath)
}
}

}
/**
* Wrapper function for calling other functions that validate configuration data.
*
* @param $type string
* One of 'of 'snippets', 'urls', 'paths', or 'all'.
*/
public function validate($type = 'all')
{
switch ($type) {
case 'all':
$this->checkMappingSnippets();
$this->checkPaths();
$this->checkUrls();
exit;
break;
case 'snippets':
$this->checkMappingSnippets();
exit;
break;
case 'urls':
$this->checkUrls();
exit;
break;
case 'paths':
$this->checkPaths();
exit;
break;
}
}

/**
* Tests metadata mapping snippets for well-formedness.
*/
public function checkMappingSnippets()
{
ini_set('display_errors', false);
$path = $this->settings['METADATA_PARSER']['mapping_csv_path'];
// First test that the mappings file exists.
if (!file_exists($path)) {
exit("Error: Can't find mappings file at $path\n");
}

$reader = Reader::createFromPath($path);
foreach ($reader as $index => $row) {
if (count($row) > 1) {
if (strlen($row[1])) {
$doc = new \DOMDocument();
if (!@$doc->loadXML($row[1])) {
exit("Error: Mapping snippet $row[1] appears to be not well formed\n");
}
}
}
}
print "Mapping snippets are OK\n";
}

/**
* Tests URLs (whose setting names end in _url) in configuration files.
*/
public function checkUrls()
{
$client = new Client();
$sections = array_values($this->settings);
foreach ($sections as $section) {
foreach ($section as $key => $value) {
if (preg_match('/_url$/', $key) && strlen($value)) {
try {
$response = $client->get($value);
$code = $response->getStatusCode();
}
catch (RequestException $e) {
exit("Error: The URL $value (defined in configuration setting $key) appears to be a bad URL (response code $code).\n");
}
}
}
}
print "URLs are OK\n";
}

/**
* Tests filesystem paths (whose setting names end in _path or _directory) in configuration files.
*/
public function checkPaths()
{
$sections = array_values($this->settings);
foreach ($sections as $section) {
foreach ($section as $key => $value) {
if (preg_match('/(_path|_directory)$/', $key) && strlen($value)) {
if (!file_exists($value)) {
exit("Error: The path $value (defined in configuration setting $key) does not exist.\n");
}
}
}
}
print "Paths are OK\n";
}

}

0 comments on commit a57cb58

Please sign in to comment.