forked from arnaud-lb/php-rdkafka
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata_topic.c
206 lines (163 loc) · 5.62 KB
/
metadata_topic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
+----------------------------------------------------------------------+
| php-rdkafka |
+----------------------------------------------------------------------+
| Copyright (c) 2016 Arnaud Le Blanc |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Arnaud Le Blanc <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_rdkafka.h"
#include "php_rdkafka_priv.h"
#include "librdkafka/rdkafka.h"
#include "ext/spl/spl_iterators.h"
#include "Zend/zend_interfaces.h"
#include "metadata_partition.h"
#include "metadata_collection.h"
#include "Zend/zend_exceptions.h"
#if PHP_VERSION_ID < 80000
#include "metadata_topic_legacy_arginfo.h"
#else
#include "metadata_topic_arginfo.h"
#endif
typedef struct _object_intern {
zval zmetadata;
const rd_kafka_metadata_topic_t *metadata_topic;
zend_object std;
} object_intern;
static HashTable *get_debug_info(Z_RDKAFKA_OBJ *object, int *is_temp);
static zend_class_entry * ce;
static zend_object_handlers handlers;
static void partitions_collection(zval *return_value, Z_RDKAFKA_OBJ *parent, object_intern *intern) { /* {{{ */
kafka_metadata_collection_init(return_value, parent, intern->metadata_topic->partitions, intern->metadata_topic->partition_cnt, sizeof(*intern->metadata_topic->partitions), kafka_metadata_partition_ctor);
}
/* }}} */
static void free_object(zend_object *object) /* {{{ */
{
object_intern *intern = php_kafka_from_obj(object_intern, object);
if (intern->metadata_topic) {
zval_dtor(&intern->zmetadata);
}
zend_object_std_dtor(&intern->std);
}
/* }}} */
static zend_object *create_object(zend_class_entry *class_type) /* {{{ */
{
zend_object* retval;
object_intern *intern;
intern = zend_object_alloc(sizeof(*intern), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
retval = &intern->std;
retval->handlers = &handlers;
return retval;
}
/* }}} */
static object_intern * get_object(zval *zmt)
{
object_intern *omt = Z_RDKAFKA_P(object_intern, zmt);
if (!omt->metadata_topic) {
zend_throw_exception_ex(NULL, 0, "RdKafka\\Metadata\\Topic::__construct() has not been called");
return NULL;
}
return omt;
}
static HashTable *get_debug_info(Z_RDKAFKA_OBJ *object, int *is_temp) /* {{{ */
{
zval ary;
object_intern *intern;
zval partitions;
*is_temp = 1;
array_init(&ary);
intern = rdkafka_get_debug_object(object_intern, object);
if (!intern || !intern->metadata_topic) {
return Z_ARRVAL(ary);
}
add_assoc_string(&ary, "topic", intern->metadata_topic->topic);
ZVAL_NULL(&partitions);
partitions_collection(&partitions, object, intern);
add_assoc_zval(&ary, "partitions", &partitions);
add_assoc_long(&ary, "err", intern->metadata_topic->err);
return Z_ARRVAL(ary);
}
/* }}} */
/* {{{ proto string RdKafka\MetadataTopic::getTopic()
Topic name */
PHP_METHOD(RdKafka_Metadata_Topic, getTopic)
{
object_intern *intern;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
intern = get_object(getThis());
if (!intern) {
return;
}
RETURN_STRING(intern->metadata_topic->topic);
}
/* }}} */
/* {{{ proto int RdKafka\MetadataTopic::getErr()
Error */
PHP_METHOD(RdKafka_Metadata_Topic, getErr)
{
object_intern *intern;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
intern = get_object(getThis());
if (!intern) {
return;
}
RETURN_LONG(intern->metadata_topic->err);
}
/* }}} */
/* {{{ proto RdKafka\Metadata\Collection RdKafka\Metadata\Topic::getPartitions()
Partitions */
PHP_METHOD(RdKafka_Metadata_Topic, getPartitions)
{
object_intern *intern;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
intern = get_object(getThis());
if (!intern) {
return;
}
partitions_collection(return_value, Z_RDKAFKA_PROP_OBJ(getThis()), intern);
}
/* }}} */
void kafka_metadata_topic_minit(INIT_FUNC_ARGS)
{
ce = register_class_RdKafka_Metadata_Topic();
ce->create_object = create_object;
handlers = kafka_default_object_handlers;
handlers.get_debug_info = get_debug_info;
handlers.free_obj = free_object;
handlers.offset = XtOffsetOf(object_intern, std);
}
void kafka_metadata_topic_ctor(zval *return_value, zval *zmetadata, const void *data)
{
rd_kafka_metadata_topic_t *metadata_topic = (rd_kafka_metadata_topic_t*)data;
object_intern *intern;
if (object_init_ex(return_value, ce) != SUCCESS) {
return;
}
intern = Z_RDKAFKA_P(object_intern, return_value);
if (!intern) {
return;
}
ZVAL_ZVAL(&intern->zmetadata, zmetadata, 1, 0);
intern->metadata_topic = metadata_topic;
}