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

Revision 15009, 6.5 kB (checked in by piotras, 10 months ago)

Added midgard_object_class::connect_default to register defaulc class' callback

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* Copyright (C) 2006 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
20 static zend_class_entry *php_midgard_object_class_class;
21
22 static ZEND_METHOD(midgard_object_class, is_multilang)
23 {
24         CHECK_MGD;
25         RETVAL_FALSE;
26        
27         zval *zvalue;
28         gboolean is_ml = FALSE;
29         MidgardObjectClass *klass = NULL;
30
31         if (zend_parse_parameters(
32                                 ZEND_NUM_ARGS() TSRMLS_CC, "z", &zvalue) == FAILURE) {
33                 WRONG_PARAM_COUNT;
34                 RETURN_FALSE;
35         }
36        
37         if(Z_TYPE_P(zvalue) != IS_STRING) {
38                 if(Z_TYPE_P(zvalue) != IS_OBJECT) {
39                         php_error(E_WARNING,
40                                         "%s() accepts object or string as first argument",
41                                         get_active_function_name(TSRMLS_C));
42                         RETURN_FALSE;
43                 }
44         }
45
46         if(Z_TYPE_P(zvalue) == IS_STRING) {
47                 klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(
48                                 (const gchar *)Z_STRVAL_P(zvalue));
49         } else if(Z_TYPE_P(zvalue) == IS_OBJECT) {
50                 klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(
51                                 (const gchar *)Z_OBJCE_P(zvalue)->name);
52         }
53
54         if(!klass) {
55                 php_error(E_WARNING,
56                                 "MidgardObjectClass not found");
57                 RETURN_FALSE;
58         }
59
60         is_ml = midgard_object_class_is_multilang(klass);
61
62         RETURN_BOOL(is_ml);
63 }
64
65 static ZEND_METHOD(midgard_object_class, factory)
66 {
67         RETVAL_FALSE;
68         CHECK_MGD;
69
70         gchar *class_name;
71         guint class_name_length;
72         zval *zvalue;
73
74         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
75                                 "s|z", &class_name, &class_name_length, &zvalue) == FAILURE)
76                 return;
77
78         GValue *val = php_midgard_zval2gvalue(zvalue);
79         MgdObject *object = midgard_object_new(mgd_handle(),
80                         (const gchar *)class_name, val);
81
82         /* Exception should be thrown */
83         if(!object)
84                 RETURN_FALSE;
85
86         php_midgard_gobject_init(return_value, class_name, G_OBJECT(object), TRUE);
87 }
88
89 static ZEND_METHOD(midgard_object_class, get_object_by_guid)
90 {
91         RETVAL_FALSE;
92         CHECK_MGD;
93
94         gchar *guid, *class_name;
95         guint guid_length;
96         const gchar *type_name;
97        
98         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
99                                 "s", &guid, &guid_length) == FAILURE)
100                 return;
101
102         MgdObject *object =
103                 midgard_object_class_get_object_by_guid(mgd_handle(), guid);
104
105         if(!object)
106                 RETURN_NULL();
107
108         type_name = G_OBJECT_TYPE_NAME(G_OBJECT(object));
109         class_name = g_ascii_strdown(type_name, strlen(type_name));
110
111         php_midgard_gobject_init(return_value, (const gchar *)class_name,
112                         G_OBJECT(object), FALSE);       
113
114         g_free(class_name);
115 }
116
117 static ZEND_METHOD(midgard_object_class, get_property_up)
118 {
119         CHECK_MGD;
120         RETVAL_FALSE;
121         zval *zvalue;
122         MidgardObjectClass *klass = NULL;
123
124         if (zend_parse_parameters(
125                                 ZEND_NUM_ARGS() TSRMLS_CC, "z", &zvalue) == FAILURE) {
126                 WRONG_PARAM_COUNT;
127                 RETURN_FALSE;
128         }
129        
130         if(Z_TYPE_P(zvalue) != IS_STRING) {
131                 if(Z_TYPE_P(zvalue) != IS_OBJECT) {
132                         php_error(E_WARNING,
133                                         "%s() accepts object or string as first argument",
134                                         get_active_function_name(TSRMLS_C));
135                         RETURN_FALSE;
136                 }
137         }
138
139         if(Z_TYPE_P(zvalue) == IS_STRING) {
140                 klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(
141                                 (const gchar *)Z_STRVAL_P(zvalue));
142         } else if(Z_TYPE_P(zvalue) == IS_OBJECT) {
143                 klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(
144                                 (const gchar *)Z_OBJCE_P(zvalue)->name);
145         }
146
147         if(!klass) {
148                 php_error(E_WARNING,
149                                 "MidgardObjectClass not found");
150                 RETURN_FALSE;
151         }
152        
153         /* TODO, add to core
154         const gchar *property_up =
155                 midgard_object_class_get_up_property(klass);
156
157         if(!property_up)
158                 RETURN_NULL();
159
160         RETURN_STRING((gchar *)property_up, 1); */
161 }
162
163 static ZEND_METHOD(midgard_object_class, get_property_parent)
164 {
165         CHECK_MGD;
166         RETVAL_FALSE;
167         zval *zvalue;
168         MidgardObjectClass *klass = NULL;
169
170         if (zend_parse_parameters(
171                                 ZEND_NUM_ARGS() TSRMLS_CC, "z", &zvalue) == FAILURE) {
172                 WRONG_PARAM_COUNT;
173                 RETURN_FALSE;
174         }
175        
176         if(Z_TYPE_P(zvalue) != IS_STRING) {
177                 if(Z_TYPE_P(zvalue) != IS_OBJECT) {
178                         php_error(E_WARNING,
179                                         "%s() accepts object or string as first argument",
180                                         get_active_function_name(TSRMLS_C));
181                         RETURN_FALSE;
182                 }
183         }
184
185         if(Z_TYPE_P(zvalue) == IS_STRING) {
186                 klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(
187                                 (const gchar *)Z_STRVAL_P(zvalue));
188         } else if(Z_TYPE_P(zvalue) == IS_OBJECT) {
189                 klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(
190                                 (const gchar *)Z_OBJCE_P(zvalue)->name);
191         }
192
193         if(!klass) {
194                 php_error(E_WARNING,
195                                 "MidgardObjectClass not found");
196                 RETURN_FALSE;
197         }
198
199         /* TODO, add to core
200         const gchar *property_parent =
201                 midgard_object_class_get_parent_property(klass);
202         
203         if(!property_parent)
204                 RETURN_NULL();
205
206         RETURN_STRING((gchar *)property_parent, 1); */
207 }
208
209 static ZEND_METHOD(midgard_object_class, undelete)
210 {
211         RETVAL_FALSE;
212         CHECK_MGD;
213        
214         gchar *guid;
215         guint guid_length;
216         gboolean rv;
217
218         if (zend_parse_parameters(1 TSRMLS_CC,
219                                 "s", &guid, &guid_length)  == FAILURE) {
220                 return;
221         }
222
223         rv = midgard_object_class_undelete(mgd_handle(), (const gchar *)guid);
224         RETURN_BOOL(rv);
225 }
226
227 static ZEND_METHOD(midgard_object_class, connect_default)
228 {
229         CHECK_MGD;     
230         php_midgard_object_class_connect_default(INTERNAL_FUNCTION_PARAM_PASSTHRU);
231 }
232
233 void php_midgard_object_class_init(int module_number)
234 {
235         static function_entry object_class_methods[] = {
236                 ZEND_ME(midgard_object_class,   factory,                NULL,
237                                 ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
238                 ZEND_ME(midgard_object_class,   undelete,               NULL,
239                                 ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
240                 ZEND_ME(midgard_object_class,   is_multilang,           NULL,
241                                 ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
242                 ZEND_ME(midgard_object_class,   get_object_by_guid,     NULL,
243                                 ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
244                 ZEND_ME(midgard_object_class,   get_property_up,        NULL,
245                                 ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
246                 ZEND_ME(midgard_object_class,   get_property_parent,    NULL,
247                                 ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
248                 ZEND_ME(midgard_object_class,   connect_default,        NULL,
249                                 ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
250         {NULL, NULL, NULL}
251         };
252
253         static zend_class_entry php_midgard_object_class_class_entry;
254        
255         INIT_CLASS_ENTRY(
256                         php_midgard_object_class_class_entry,
257                         "midgard_object_class", object_class_methods);
258        
259         php_midgard_object_class_class =
260                 zend_register_internal_class(&php_midgard_object_class_class_entry TSRMLS_CC);
261 }
Note: See TracBrowser for help on using the browser.