Skip to content
Snippets Groups Projects
Commit ac93d887 authored by Demian Katz's avatar Demian Katz
Browse files

Moved extractComments() from Reader to Upgrade class since it is only used in one place.

This might deserve its own class if it ever needs to be reused in the future.
parent 75eddd9e
No related merge requests found
......@@ -199,104 +199,4 @@ class Reader
$config->setReadOnly();
return $config;
}
/**
* readIniComments
*
* Read the specified file and return an associative array of this format
* containing all comments extracted from the file:
*
* array =>
* 'sections' => array
* 'section_name_1' => array
* 'before' => string ("Comments found at the beginning of this section")
* 'inline' => string ("Comments found at the end of the section's line")
* 'settings' => array
* 'setting_name_1' => array
* 'before' => string ("Comments found before this setting")
* 'inline' => string ("Comments found at the end of setting's line")
* ...
* 'setting_name_n' => array (same keys as setting_name_1)
* ...
* 'section_name_n' => array (same keys as section_name_1)
* 'after' => string ("Comments found at the very end of the file")
*
* @param string $filename Name of ini file to read.
*
* @return array Associative array as described above.
*/
public static function extractComments($filename)
{
$lines = file($filename);
// Initialize our return value:
$retVal = array('sections' => array(), 'after' => '');
// Initialize variables for tracking status during parsing:
$section = $comments = '';
foreach ($lines as $line) {
// To avoid redundant processing, create a trimmed version of the current
// line:
$trimmed = trim($line);
// Is the current line a comment? If so, add to the currentComments
// string. Note that we treat blank lines as comments.
if (substr($trimmed, 0, 1) == ';' || empty($trimmed)) {
$comments .= $line;
} else if (substr($trimmed, 0, 1) == '['
&& ($closeBracket = strpos($trimmed, ']')) > 1
) {
// Is the current line the start of a section? If so, create the
// appropriate section of the return value:
$section = substr($trimmed, 1, $closeBracket - 1);
if (!empty($section)) {
// Grab comments at the end of the line, if any:
if (($semicolon = strpos($trimmed, ';')) !== false) {
$inline = trim(substr($trimmed, $semicolon));
} else {
$inline = '';
}
$retVal['sections'][$section] = array(
'before' => $comments,
'inline' => $inline,
'settings' => array());
$comments = '';
}
} else if (($equals = strpos($trimmed, '=')) !== false) {
// Is the current line a setting? If so, add to the return value:
$set = trim(substr($trimmed, 0, $equals));
$set = trim(str_replace('[]', '', $set));
if (!empty($section) && !empty($set)) {
// Grab comments at the end of the line, if any:
if (($semicolon = strpos($trimmed, ';')) !== false) {
$inline = trim(substr($trimmed, $semicolon));
} else {
$inline = '';
}
// Currently, this data structure doesn't support arrays very
// well, since it can't distinguish which line of the array
// corresponds with which comments. For now, we just append all
// the preceding and inline comments together for arrays. Since
// we rarely use arrays in the config.ini file, this isn't a big
// concern, but we should improve it if we ever need to.
if (!isset($retVal['sections'][$section]['settings'][$set])) {
$retVal['sections'][$section]['settings'][$set]
= array('before' => $comments, 'inline' => $inline);
} else {
$retVal['sections'][$section]['settings'][$set]['before']
.= $comments;
$retVal['sections'][$section]['settings'][$set]['inline']
.= "\n" . $inline;
}
$comments = '';
}
}
}
// Store any leftover comments following the last setting:
$retVal['after'] = $comments;
return $retVal;
}
}
\ No newline at end of file
......@@ -26,7 +26,7 @@
* @link http://vufind.org Main Site
*/
namespace VuFind\Config;
use VuFind\Config\Reader as ConfigReader, VuFind\Config\Writer as ConfigWriter,
use VuFind\Config\Writer as ConfigWriter,
VuFind\Exception\FileAccess as FileAccessException;
/**
......@@ -285,7 +285,7 @@ class Upgrade
$this->newConfigs[$config]
= parse_ini_file($this->rawDir . '/' . $config, true);
$this->comments[$config]
= ConfigReader::extractComments($this->rawDir . '/' . $config);
= $this->extractComments($this->rawDir . '/' . $config);
}
}
......@@ -873,4 +873,102 @@ class Upgrade
unset($this->oldConfigs['facets.ini']['StripFacets']);
}
}
/**
* Read the specified file and return an associative array of this format
* containing all comments extracted from the file:
*
* array =>
* 'sections' => array
* 'section_name_1' => array
* 'before' => string ("Comments found at the beginning of this section")
* 'inline' => string ("Comments found at the end of the section's line")
* 'settings' => array
* 'setting_name_1' => array
* 'before' => string ("Comments found before this setting")
* 'inline' => string ("Comments found at the end of setting's line")
* ...
* 'setting_name_n' => array (same keys as setting_name_1)
* ...
* 'section_name_n' => array (same keys as section_name_1)
* 'after' => string ("Comments found at the very end of the file")
*
* @param string $filename Name of ini file to read.
*
* @return array Associative array as described above.
*/
protected function extractComments($filename)
{
$lines = file($filename);
// Initialize our return value:
$retVal = array('sections' => array(), 'after' => '');
// Initialize variables for tracking status during parsing:
$section = $comments = '';
foreach ($lines as $line) {
// To avoid redundant processing, create a trimmed version of the current
// line:
$trimmed = trim($line);
// Is the current line a comment? If so, add to the currentComments
// string. Note that we treat blank lines as comments.
if (substr($trimmed, 0, 1) == ';' || empty($trimmed)) {
$comments .= $line;
} else if (substr($trimmed, 0, 1) == '['
&& ($closeBracket = strpos($trimmed, ']')) > 1
) {
// Is the current line the start of a section? If so, create the
// appropriate section of the return value:
$section = substr($trimmed, 1, $closeBracket - 1);
if (!empty($section)) {
// Grab comments at the end of the line, if any:
if (($semicolon = strpos($trimmed, ';')) !== false) {
$inline = trim(substr($trimmed, $semicolon));
} else {
$inline = '';
}
$retVal['sections'][$section] = array(
'before' => $comments,
'inline' => $inline,
'settings' => array());
$comments = '';
}
} else if (($equals = strpos($trimmed, '=')) !== false) {
// Is the current line a setting? If so, add to the return value:
$set = trim(substr($trimmed, 0, $equals));
$set = trim(str_replace('[]', '', $set));
if (!empty($section) && !empty($set)) {
// Grab comments at the end of the line, if any:
if (($semicolon = strpos($trimmed, ';')) !== false) {
$inline = trim(substr($trimmed, $semicolon));
} else {
$inline = '';
}
// Currently, this data structure doesn't support arrays very
// well, since it can't distinguish which line of the array
// corresponds with which comments. For now, we just append all
// the preceding and inline comments together for arrays. Since
// we rarely use arrays in the config.ini file, this isn't a big
// concern, but we should improve it if we ever need to.
if (!isset($retVal['sections'][$section]['settings'][$set])) {
$retVal['sections'][$section]['settings'][$set]
= array('before' => $comments, 'inline' => $inline);
} else {
$retVal['sections'][$section]['settings'][$set]['before']
.= $comments;
$retVal['sections'][$section]['settings'][$set]['inline']
.= "\n" . $inline;
}
$comments = '';
}
}
}
// Store any leftover comments following the last setting:
$retVal['after'] = $comments;
return $retVal;
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment