root/trunk/lib/SGL/ParamHandler.php

Revision 1897, 5.7 kB (checked in by demian, 1 year ago)

fixed probs where failing to select langs during install results in fatal error

  • Property svn:eol-style set to native
Line 
1 <?php
2 /* Reminder: always indent with 4 spaces (no tabs). */
3 // +---------------------------------------------------------------------------+
4 // | Copyright (c) 2006, Demian Turner                                         |
5 // | All rights reserved.                                                      |
6 // |                                                                           |
7 // | Redistribution and use in source and binary forms, with or without        |
8 // | modification, are permitted provided that the following conditions        |
9 // | are met:                                                                  |
10 // |                                                                           |
11 // | o Redistributions of source code must retain the above copyright          |
12 // |   notice, this list of conditions and the following disclaimer.           |
13 // | o Redistributions in binary form must reproduce the above copyright       |
14 // |   notice, this list of conditions and the following disclaimer in the     |
15 // |   documentation and/or other materials provided with the distribution.    |
16 // | o The names of the authors may not be used to endorse or promote          |
17 // |   products derived from this software without specific prior written      |
18 // |   permission.                                                             |
19 // |                                                                           |
20 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       |
21 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         |
22 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR     |
23 // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      |
24 // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     |
25 // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT          |
26 // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     |
27 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     |
28 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       |
29 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE     |
30 // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.      |
31 // |                                                                           |
32 // +---------------------------------------------------------------------------+
33 // | Seagull 0.6                                                               |
34 // +---------------------------------------------------------------------------+
35 // | ParamHandler.php                                                          |
36 // +---------------------------------------------------------------------------+
37 // | Author:   Demian Turner <demian@phpkitchen.com>                           |
38 // +---------------------------------------------------------------------------+
39 // $Id: Permissions.php,v 1.5 2005/02/03 11:29:01 demian Exp $
40
41 /**
42  * Handles reading and writing of config files.
43  *
44  * @package SGL
45  * @author  Demian Turner <demian@phpkitchen.com>
46  * @version $Revision: 1.49 $
47  */
48 class SGL_ParamHandler
49 {
50     var $source;
51
52     function SGL_ParamHandler($source)
53     {
54         $this->source = $source;
55     }
56
57     function &singleton($source)
58     {
59         static $instances;
60         if (!isset($instances)) {
61             $instances = array();
62         }
63
64         $signature = md5($source);
65         if (!isset($instances[$signature])) {
66
67             $ext = substr($source, -3);
68             switch ($ext) {
69
70             case 'xml':
71                 $ret = new SGL_ParamHandler_Xml($source);
72                 break;
73
74             case 'php':
75                 $ret = new SGL_ParamHandler_Array($source);
76                 break;
77             //  at the moment .php file are ini files
78             //  but they should be arrays
79             case 'ini':
80                 $ret =  new SGL_ParamHandler_Ini($source);
81                 break;
82             }
83             $instances[$signature] = $ret;
84         }
85         return $instances[$signature];
86     }
87
88     function read() {}
89     function write() {}
90
91 //    function getAll()
92 //    {
93 //        if (empty($this->aParams)) {
94 //            $this->read();
95 //        }
96 //        return $this->aParams;
97 //    }
98 }
99
100 class SGL_ParamHandler_Ini extends SGL_ParamHandler
101 {
102     function read()
103     {
104         $ret = @parse_ini_file($this->source, true);
105         return (count($ret)) ? $ret : false;
106     }
107
108     function write($data)
109     {
110         //  load PEAR::Config
111         require_once 'Config.php';
112         $c = new Config();
113         $c->parseConfig($data, 'phparray');
114         $ok = $c->writeConfig($this->source, 'inifile');
115         return $ok;
116     }
117 }
118
119 class SGL_ParamHandler_Array extends SGL_ParamHandler
120 {
121     function read()
122     {
123         if (is_file($this->source)) {
124             $ok = require $this->source;
125             if ($ok) {
126                 $ret = $conf;
127             } else {
128                 $ret = $ok;
129             }
130         } else {
131             $ret = false;
132         }
133         return $ret;
134     }
135
136     function write($data)
137     {
138         //  load PEAR::Config
139         require_once 'Config.php';
140         $c = new Config();
141         $c->parseConfig($data, 'phparray');
142         $ok = $c->writeConfig($this->source, 'phparray');
143         return $ok;
144     }
145 }
146
147 class SGL_ParamHandler_Xml extends SGL_ParamHandler
148 {
149     function read()
150     {
151         return simplexml_load_file($this->source);
152     }
153
154     function write($data)
155     {
156         //  load PEAR::Config
157         require_once 'Config.php';
158         $c = new Config();
159         $c->parseConfig($data, 'phparray');
160         $ok = $c->writeConfig($this->source, 'xml');
161         return $ok;
162     }
163 }
164 ?>
Note: See TracBrowser for help on using the browser.