root/trunk/midgard/apis/php5/php_midgard_config.c

Revision 16726, 4.9 kB (checked in by piotras, 5 months ago)

Accept GError pointer when reading or saving config file.
Throw exception if one is set

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* Copyright (C) 2007 Piotr Pokora <piotrek.pokora@gmail.com>
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published
4  * by the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15 */
16
17 #include "php_midgard.h"
18 #include "php_midgard_gobject.h"
19 #include <zend_exceptions.h>
20
21 static zend_class_entry *php_midgard_config_class;
22
23 /* Object constructor */
24 static PHP_METHOD(midgard_config, __construct)
25 {       
26         RETVAL_FALSE;
27
28         zval *object = getThis();
29
30         if(ZEND_NUM_ARGS() TSRMLS_CC > 0)
31                 WRONG_PARAM_COUNT;
32
33         MidgardConfig *config =
34                 midgard_config_new();
35
36         if(!config)
37                 RETURN_FALSE;
38
39         php_midgard_gobject *php_gobject =
40                 (php_midgard_gobject *)zend_object_store_get_object(object TSRMLS_CC);
41         php_gobject->gobject = G_OBJECT(config);
42 }
43
44 static PHP_METHOD(midgard_config, save_file)
45 {
46         RETVAL_FALSE;
47         gboolean rv;
48         zend_bool zbool = FALSE;
49         zval *zval_object = getThis();
50
51         gchar *name;
52         guint name_length;
53
54         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b",
55                                 &name, &name_length, &zbool) == FAILURE) {
56                 WRONG_PARAM_COUNT;
57         }
58
59         php_midgard_gobject *php_gobject =
60                 (php_midgard_gobject *)zend_object_store_get_object(zval_object TSRMLS_CC);
61
62         MidgardConfig *config =
63                 (MidgardConfig *) php_gobject->gobject;
64
65         GError *error = NULL;
66         rv = midgard_config_save_file(config ,name, zbool, &error);
67
68         if(!rv) {
69                
70                 if(error) {
71                
72                           zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C),
73                                           0 TSRMLS_CC, error->message);
74                           g_error_free(error);
75                           RETURN_FALSE;
76                 }
77
78         }
79
80         RETURN_TRUE;
81 }
82
83 static PHP_METHOD(midgard_config, read_file)
84 {
85         RETVAL_FALSE;
86         gboolean rv;
87         zend_bool zbool = FALSE;
88         zval *zval_object = getThis();
89
90         gchar *name;
91         guint name_length;
92
93         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b",
94                                 &name, &name_length, &zbool) == FAILURE) {
95                 WRONG_PARAM_COUNT;
96         }
97        
98         php_midgard_gobject *php_gobject =
99                 (php_midgard_gobject *)zend_object_store_get_object(zval_object TSRMLS_CC);
100        
101         MidgardConfig *config =
102                 (MidgardConfig *) php_gobject->gobject;
103
104
105         GError *error = NULL;
106         rv = midgard_config_read_file(config, name, zbool, &error);
107        
108         if(!rv) {
109                
110                 if(error) {
111                
112                         zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C),
113                                         0 TSRMLS_CC, error->message);
114                         g_error_free(error);
115                         RETURN_FALSE;
116                 }
117         }
118
119         RETURN_TRUE;
120 }
121
122 static PHP_METHOD(midgard_config, create_midgard_tables)
123 {
124         RETVAL_FALSE;
125         gboolean rv;
126         zval *zval_object = getThis();
127
128         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, ""
129                                 ) == FAILURE) {
130                 return;
131         }
132        
133         MidgardConfig *config = NULL;
134
135         if(zval_object) {
136                 php_midgard_gobject *php_gobject =
137                         (php_midgard_gobject *)zend_object_store_get_object(
138                                         zval_object TSRMLS_CC);
139                
140                 config =
141                         (MidgardConfig *) php_gobject->gobject;
142         }
143
144         rv = midgard_config_create_midgard_tables(config, mgd_handle());
145
146         RETURN_BOOL(rv);
147 }
148
149 static PHP_METHOD(midgard_config, create_class_table)
150 {
151         RETVAL_FALSE;
152         gboolean rv;
153         zval *zval_object = getThis();
154
155         gchar *name;
156         guint name_length;
157
158         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
159                                 &name, &name_length) == FAILURE) {
160                 return;
161         }
162        
163         MidgardConfig *config = NULL;
164
165         if(zval_object) {
166                
167                 php_midgard_gobject *php_gobject =
168                         (php_midgard_gobject *)zend_object_store_get_object(
169                                         zval_object TSRMLS_CC);
170                
171                 config =
172                         (MidgardConfig *) php_gobject->gobject;
173         }
174
175         MidgardObjectClass *klass =
176                 MIDGARD_OBJECT_GET_CLASS_BY_NAME(name);
177
178         if(!klass)
179                 return;
180
181         rv = midgard_config_create_class_table(config, klass, mgd_handle());
182
183         RETURN_BOOL(rv);
184 }
185
186 /* Initialize ZEND&PHP class */
187 void php_midgard_config_init(int module_numer)
188 {
189
190         static function_entry midgard_config_methods[] = {
191                 PHP_ME(midgard_config,  __construct,    NULL, ZEND_ACC_PUBLIC)
192                 PHP_ME(midgard_config,  save_file,      NULL, ZEND_ACC_PUBLIC)
193                 PHP_ME(midgard_config,  read_file,      NULL, ZEND_ACC_PUBLIC)
194                 PHP_ME(midgard_config,  create_midgard_tables, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
195                 PHP_ME(midgard_config,  create_class_table,     NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
196                 {NULL, NULL, NULL}
197         };
198
199         static zend_class_entry php_midgard_config_class_entry;
200
201         INIT_CLASS_ENTRY(
202                         php_midgard_config_class_entry,
203                         "midgard_config", midgard_config_methods);
204
205         php_midgard_config_class =
206                 zend_register_internal_class(
207                                 &php_midgard_config_class_entry TSRMLS_CC);
208        
209         /* Set function to initialize underlying data */
210         php_midgard_config_class->create_object = php_midgard_gobject_new;     
211 }
Note: See TracBrowser for help on using the browser.