Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Untitled #1

Merged
3 commits merged into from

2 participants

@mfonda

Hi Patrick,

I'm new to git/github so hopefully I am doing this right! I've made several updates to Config_Lite that I would like to propose for inclusion.

  1. Add option for to ignore sections (i.e. pass false as 2nd parameter to parse_ini_file). Some ini files (for example, php.ini) have sections in them, but aren't actually used for anything, so it's nice to have the option to ignore them completely.
  2. I removed adding the ;<?php return; ?> in write() if extension is .php. File extensions are arbitrary and there can be php files that aren't .php so this just seems too arbitrary.
  3. Make output via write() and __toString() match
  4. Add accessor for filename.
  5. Implement Countable interface to make it feel even more like array.

I am working on a fix for PEAR bug #18352 - would love to hear if you have any ideas about the best approach to this.

--Matthew

@pce
Owner

Hi Matthew, thanks! :)

@pce
Owner

Hi Matthew,

#18352 this is a limitation and the simple way Config_Lite handles arrays.
One Solution could be to store "metainfos", ie. a array key like "__global",
(like XML_Serializer), but i hoped to handle sectionless arrays with no such overhead.

greetings,
Patrick

@mfonda

I thought of that as well and don't like it either. A way to handle sectionless arrays with no such overhead is definitely best :)

Not sure how this will be possible yet.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 9, 2011
  1. @mfonda

    * add option to choose if sections should be processed

    mfonda authored
    * implement Countable interface
    * add tests for count and get (without sections)
  2. @mfonda

    add getFilename accessor

    mfonda authored
  3. @mfonda
This page is out of date. Refresh to see the latest.
Showing with 109 additions and 32 deletions.
  1. +71 −31 Config/Lite.php
  2. +38 −1 tests/Config_LiteTest.php
View
102 Config/Lite.php
@@ -38,7 +38,7 @@
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @link https://github.com/pce/config_lite
*/
-class Config_Lite implements ArrayAccess, IteratorAggregate
+class Config_Lite implements ArrayAccess, IteratorAggregate, Countable
{
/**
* sections, holds the config sections
@@ -68,6 +68,13 @@ class Config_Lite implements ArrayAccess, IteratorAggregate
* @var string
*/
protected $linebreak = "\n";
+
+ /**
+ * parseSections - if true, sections will be processed
+ *
+ * @var bool
+ */
+ protected $processSections = true;
/**
* the read method parses the optional given filename
@@ -98,7 +105,7 @@ public function read($filename = null)
. $filename
);
}
- $this->sections = parse_ini_file($filename, true);
+ $this->sections = parse_ini_file($filename, $this->processSections);
if (false === $this->sections) {
throw new Config_Lite_Exception_Runtime(
'failure, can not parse the file: ' . $filename
@@ -192,10 +199,27 @@ protected function normalizeValue($value)
*/
public function write($filename, $sectionsarray)
{
- $content = '';
- if ('.php' === substr($filename, -4)) {
- $content .= ';<?php return; ?>' . $this->linebreak;
+ $content = $this->buildOutputString($sectionsarray);
+ if (false === file_put_contents($filename, $content, LOCK_EX)) {
+ throw new Config_Lite_Exception_Runtime(
+ sprintf(
+ 'failed to write file `%s\' for writing.', $filename
+ )
+ );
}
+ return true;
+ }
+
+ /**
+ * Generated the output of the ini file, suitable for echo'ing or
+ * writing back to the ini file.
+ *
+ * @param string $sectionsarray array of ini data
+ * @return string
+ */
+ protected function buildOutputString($sectionsarray)
+ {
+ $content = '';
$sections = '';
$globals = '';
if (!empty($sectionsarray)) {
@@ -227,17 +251,9 @@ public function write($filename, $sectionsarray)
}
$content .= $sections;
}
-
- if (false === file_put_contents($filename, $content, LOCK_EX)) {
- throw new Config_Lite_Exception_Runtime(
- sprintf(
- 'failed to write file `%s\' for writing.', $filename
- )
- );
- }
- return true;
+ return $content;
}
-
+
/**
* converts type (format) to string or representable Config Format
*
@@ -452,7 +468,7 @@ public function getSection($sec, $default = null)
*/
public function hasSection($sec)
{
- if (isset($this->sections[$sec])) {
+ if (isset($this->sections[$sec]) && is_array($this->sections[$sec])) {
return true;
}
return false;
@@ -612,7 +628,17 @@ public function setFilename($filename)
$this->filename = $filename;
return $this;
}
-
+
+ /**
+ * returns the current filename
+ *
+ * @return string
+ */
+ public function getFilename()
+ {
+ return $this->filename;
+ }
+
/**
* set the line break (newline) chars
*
@@ -629,6 +655,22 @@ public function setLinebreak($linebreakchars)
$this->linebreak = $linebreakchars;
return $this;
}
+
+ /**
+ * Sets whether or not sections should be processed
+ *
+ * If true, values for each section will be placed into
+ * a sub-array for the section. If false, all values will
+ * be placed in the global scope.
+ *
+ * @param bool $processSections
+ * @return $this
+ */
+ public function setProcessSections($processSections)
+ {
+ $this->processSections = $processSections;
+ return $this;
+ }
/**
* text presentation of the config object
@@ -641,20 +683,7 @@ public function setLinebreak($linebreakchars)
*/
public function __toString()
{
- $s = "";
- if ($this->sections != null) {
- foreach ($this->sections as $section => $name) {
- if (is_array($name)) {
- $s .= sprintf("[%s]\n", $section);
- foreach ($name as $key => $val) {
- $s .= sprintf("\t%s = %s\n", $key, $val);
- }
- } else {
- $s .= sprintf("%s=%s\n", $section, $name);
- }
- }
- }
- return $s;
+ return $this->buildOutputString($this->sections);
}
/**
@@ -718,6 +747,17 @@ public function getIterator()
{
return new ArrayIterator($this->sections);
}
+
+ /**
+ * implemented for interface Countable
+ *
+ * @see http://php.net.countable
+ * @return int
+ */
+ public function count()
+ {
+ return count($this->sections);
+ }
/**
* takes an optional filename, if the file exists, also reads it.
View
39 tests/Config_LiteTest.php
@@ -335,5 +335,42 @@ public function testNormalizeValue()
$s = $m->invokeArgs($obj, array('String'));
$this->assertEquals('"String"', $s);
- }
+ }
+
+ public function testCountSections()
+ {
+ $count = count($this->config);
+ $this->assertEquals($count, 6);
+ }
+
+ public function testCountGlobal()
+ {
+ $this->config->setProcessSections(false)->read(dirname(__FILE__).'/test.cfg');
+ $count = count($this->config);
+ $this->assertEquals($count, 9);
+ }
+
+ public function testDoNotProcessSectionsGet()
+ {
+ $this->config->setProcessSections(false)->read(dirname(__FILE__).'/test.cfg');
+ $counter = $this->config->get(null, 'count');
+ $this->assertEquals(2, $counter);
+ // fallback to default given value 3
+ $counter = $this->config->get(null, 'nonexisting_counter_option', 3);
+ $this->assertEquals(3, $counter);
+ }
+
+ public function testGetFilename()
+ {
+ $filename = dirname(__FILE__).'/test.cfg';
+ $this->config->setFilename($filename);
+ $this->assertEquals($filename, $this->config->getFilename());
+ }
+
+ public function testToStringMatchesWrite()
+ {
+ $this->config->setFilename($this->filename);
+ $this->config->save();
+ $this->assertEquals($this->config->__toString(), file_get_contents($this->filename));
+ }
}
Something went wrong with that request. Please try again.