1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2010 - 2012 Shuhei Tanuma
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ */
24
+
25
+ #include "php_git2.h"
26
+ #include <spl/spl_array.h>
27
+ #include <zend_interfaces.h>
28
+
29
+ PHPAPI zend_class_entry * git2_config_class_entry ;
30
+
31
+ static void php_git2_config_free_storage (php_git2_config * object TSRMLS_DC )
32
+ {
33
+ if (object -> config != NULL ) {
34
+ free (object -> config );
35
+ object -> config = NULL ;
36
+ }
37
+ zend_object_std_dtor (& object -> zo TSRMLS_CC );
38
+ efree (object );
39
+ }
40
+
41
+ zend_object_value php_git2_config_new (zend_class_entry * ce TSRMLS_DC )
42
+ {
43
+ zend_object_value retval ;
44
+
45
+ PHP_GIT2_STD_CREATE_OBJECT (php_git2_config );
46
+ return retval ;
47
+ }
48
+
49
+
50
+ ZEND_BEGIN_ARG_INFO_EX (arginfo_git2_config___construct , 0 ,0 ,1 )
51
+ ZEND_ARG_INFO (0 , entry )
52
+ ZEND_END_ARG_INFO ()
53
+
54
+ ZEND_BEGIN_ARG_INFO_EX (arginfo_git2_config_get , 0 ,0 ,1 )
55
+ ZEND_ARG_INFO (0 , get )
56
+ ZEND_END_ARG_INFO ()
57
+
58
+ typedef struct {
59
+ zval * result ;
60
+ git_config * config ;
61
+ } php_git2_config_foreach_t ;
62
+
63
+ static int php_git2_config_foreach (const char * var_name , const char * value , void * payload )
64
+ {
65
+ HashTable * hash ;
66
+ zval * entry , * * target_offset ;
67
+ const char * config_value ;
68
+ char * current_key , * tmp_value , * savedptr , * k ;
69
+ php_git2_config_foreach_t * opaque = (php_git2_config_foreach_t * )payload ;
70
+ int error = 0 ;
71
+
72
+ hash = Z_ARRVAL_P (opaque -> result );
73
+
74
+ error = git_config_get_string (opaque -> config , var_name , & config_value );
75
+
76
+ tmp_value = estrdup (var_name );
77
+ current_key = php_strtok_r (tmp_value , "." , & savedptr );
78
+ while (current_key != NULL ) {
79
+ k = current_key ;
80
+ current_key = php_strtok_r (NULL , "." , & savedptr );
81
+
82
+ if (current_key != NULL ) {
83
+ if (zend_hash_exists (hash , k , strlen (k )+ 1 )) {
84
+ if (zend_hash_find (hash ,k , strlen (k )+ 1 , (void * * )& target_offset ) == SUCCESS ) {
85
+ hash = Z_ARRVAL_P (* target_offset );
86
+ }
87
+ } else {
88
+ MAKE_STD_ZVAL (entry );
89
+ array_init (entry );
90
+ zend_hash_add (hash , k , strlen (k )+ 1 , (void * * )& entry , sizeof (entry ), NULL );
91
+ hash = Z_ARRVAL_P (entry );
92
+ }
93
+ }
94
+ }
95
+
96
+ if (k != NULL ) {
97
+ MAKE_STD_ZVAL (entry );
98
+ ZVAL_STRING (entry , config_value , 1 );
99
+ zend_hash_add (hash , k , strlen (k )+ 1 , (void * * )& entry , sizeof (entry ), NULL );
100
+ Z_ADDREF_P (entry );
101
+ zval_ptr_dtor (& entry );
102
+ }
103
+ efree (tmp_value );
104
+
105
+ return GIT_SUCCESS ;
106
+ }
107
+
108
+ /*
109
+ {{{ proto: Git2\Config::__construct(string $path)
110
+ */
111
+ PHP_METHOD (git2_config , __construct )
112
+ {
113
+ char * path ;
114
+ git_config * config ;
115
+ int error , path_len = 0 ;
116
+ php_git2_config * m_config ;
117
+ zval * config_array ;
118
+ php_git2_config_foreach_t payload ;
119
+
120
+ /* @todo: supports array for reading multiple configs */
121
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
122
+ "s" , & path , & path_len ) == FAILURE ) {
123
+ return ;
124
+ }
125
+
126
+ error = git_config_open_ondisk (& config , path );
127
+ /* @todo: automatic convert types */
128
+ m_config = PHP_GIT2_GET_OBJECT (php_git2_config , getThis ());
129
+ m_config -> config = config ;
130
+
131
+ /* @todo: think solution */
132
+ MAKE_STD_ZVAL (config_array );
133
+ array_init (config_array );
134
+
135
+ payload .config = config ;
136
+ payload .result = config_array ;
137
+ error = git_config_foreach (config ,& php_git2_config_foreach ,& payload );
138
+ add_property_zval (getThis (), "configs" , config_array );
139
+ zval_ptr_dtor (& config_array );
140
+ }
141
+ /* }}} */
142
+
143
+ /*
144
+ {{{ proto: Git2\Config::get(string $key)
145
+ */
146
+ PHP_METHOD (git2_config , get )
147
+ {
148
+ char * key ;
149
+ git_config * config ;
150
+ int error , key_len = 0 ;
151
+ const char * value ;
152
+ php_git2_config * m_config ;
153
+
154
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
155
+ "s" , & key , & key_len ) == FAILURE ) {
156
+ return ;
157
+ }
158
+
159
+ m_config = PHP_GIT2_GET_OBJECT (php_git2_config , getThis ());
160
+ error = git_config_get_string (m_config -> config , key , & value );
161
+ RETVAL_STRING (value , 1 );
162
+ }
163
+ /* }}} */
164
+
165
+
166
+ static zend_function_entry php_git2_config_methods [] = {
167
+ PHP_ME (git2_config , __construct , arginfo_git2_config___construct , ZEND_ACC_PUBLIC )
168
+ PHP_ME (git2_config , get , arginfo_git2_config_get , ZEND_ACC_PUBLIC )
169
+ {NULL ,NULL ,NULL }
170
+ };
171
+
172
+ void php_git2_config_init (TSRMLS_D )
173
+ {
174
+ zend_class_entry ce ;
175
+
176
+ INIT_NS_CLASS_ENTRY (ce , PHP_GIT2_NS , "Config" , php_git2_config_methods );
177
+ git2_config_class_entry = zend_register_internal_class (& ce TSRMLS_CC );
178
+ git2_config_class_entry -> create_object = php_git2_config_new ;
179
+ }
0 commit comments