Source for file mvblog_inifilereader.php

Documentation is available at mvblog_inifilereader.php

  1. <?php
  2. /**
  3.  * MvBlog -- An open source no-nosense blogtool
  4.  *
  5.  * Copyright (C) 2005-2007, Michiel van Baak
  6.  * Michiel van Baak <mvanbaak@users.sourceforge.net>
  7.  *
  8.  * See http://www.mvblog.org for more information on MvBlog.
  9.  * That page also provides Bugtrackers, Filereleases etc.
  10.  *
  11.  * This program is free software, distributed under the terms of
  12.  * the GNU General Public License Version 2. See the LICENSE file
  13.  * at the top of the source tree.
  14.  *
  15.  * @package MvBlog
  16.  * @author Ferry Boender
  17.  * @version %%VERSION%%
  18.  * @copyright 2005-2007 Ferry Boender
  19.  */
  20.  
  21. /**
  22.  * Class to read an ini file
  23.  */
  24.     /**
  25.      * @var string $filename The ini file to read
  26.      */
  27.     protected $filename;
  28.  
  29.     public function __construct($availSettings$filename{
  30.         parent::__construct($availSettings);
  31.         $this->filename = $filename;
  32.         $contents $this->read($this->filename);
  33.         $settings $this->parse($contents);
  34.         $this->setSettings($settings);
  35.     }
  36.  
  37.     protected function read($filename{
  38.         if (($contents @file_get_contents($filename)) === False)
  39.             throw new mvblog_IniFileReaderException(1$filename);
  40.  
  41.         return($contents);
  42.     }
  43.  
  44.     // Custom parser so we get file &line numbers
  45.     protected function parse($contents{
  46.         $settings array();
  47.  
  48.         // Find and replace dos line endings
  49.         $contents str_replace("\r\n""\n"$contents);
  50.         // Find and replace mac line endings
  51.         $contents str_replace("\r""\n"$contents);
  52.  
  53.         $contents explode("\n"$contents);
  54.         $section "";
  55.         for ($i 0$i count($contents)$i++{
  56.             // Look for a section
  57.             if (preg_match('/^\[(.*?)\]*$/'$contents[$i]$match)) {
  58.                 $section $match[1];
  59.             }
  60.  
  61.             if (!preg_match("/^\s*#.*$/"$contents[$i]&& // Comment
  62.                 preg_match('/^\s*(.*?)\s*=\s*(.*?)\s*$/'$contents[$i]$match)) {
  63.                 $key $match[1];
  64.                 $value $match[2];
  65.                 $settings[$section][array("key" => $key"value" => $value"line" => $i);
  66.             }
  67.         }
  68.  
  69.         return($settings);
  70.     }
  71.  
  72.     protected function setSettings($settings{
  73.         foreach ($settings as $section => $data{
  74.             foreach($data as $setting{
  75.                 $key $setting["key"];
  76.                 $value $setting["value"];
  77.                 $file $this->filename;
  78.                 $line $setting["line"];
  79.  
  80.                 try {
  81.                     $this->setSetting($section$key$value);
  82.                 catch (mvblog_IniParserException $e{
  83.                     switch($e->getCode()) {
  84.                         case 1:
  85.                             throw new mvblog_IniFileReaderException(2$key$section$file$line);
  86.                             break;
  87.                         case 2:
  88.                             $type mvblog_IniParser::$typeNames[$this->availSettings[$section][$key]["type"]];
  89.                             throw new mvblog_IniFileReaderException(3$value$key$type$section$file$line);
  90.                             break;
  91.                         case 3:
  92.                             throw new mvblog_IniFileReaderException(4$key$value$section$file$line);
  93.                             break;
  94.                         default:
  95.                             throw $e;
  96.                             break;
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102. }
  103. ?>

Documentation generated on Fri, 28 Dec 2007 13:17:40 +0100 by phpDocumentor 1.4.1