-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsplclassloader.c
More file actions
429 lines (348 loc) · 12.6 KB
/
splclassloader.c
File metadata and controls
429 lines (348 loc) · 12.6 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 The PHP Group |
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: metagoto <runpac314 gmail com> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "zend_interfaces.h"
#include "php_splclassloader.h"
#ifdef COMPILE_DL_SPLCLASSLOADER
ZEND_GET_MODULE(splclassloader)
#endif
/* zend_API.h: #define ZEND_NS_NAME(ns, name) ns"\\"name */
#define SPLCLASSLD_NS_SEPARATOR '\\'
static zend_class_entry* splclassloader_ce;
zend_object_handlers splclassloader_object_handlers;
typedef struct _splclassloader_object {
zend_object std;
char* ns;
int ns_len;
char* inc_path;
int inc_path_len;
char* file_ext;
int file_ext_len;
} splclassloader_object;
static void splclassloader_object_dtor(void* object, zend_object_handle handle TSRMLS_DC)
{
splclassloader_object* obj = (splclassloader_object*)object;
if (obj->ns) {
efree(obj->ns);
obj->ns = NULL;
}
if (obj->inc_path) {
efree(obj->inc_path);
obj->inc_path = NULL;
}
if (obj->file_ext) {
efree(obj->file_ext);
obj->file_ext = NULL;
}
zend_object_std_dtor(&obj->std TSRMLS_CC);
efree(obj);
}
zend_object_value splclassloader_create_object(zend_class_entry* class_type TSRMLS_DC)
{
zend_object_value retval;
splclassloader_object* obj = (splclassloader_object*)emalloc(sizeof(splclassloader_object));
memset(obj, 0, sizeof(splclassloader_object));
zend_object_std_init(&obj->std, class_type TSRMLS_CC);
zend_hash_copy(obj->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void*)0, sizeof(zval*));
obj->file_ext = estrndup(".php", sizeof(".php")-1);
obj->file_ext_len = sizeof(".php")-1;
retval.handle = zend_objects_store_put(obj, (zend_objects_store_dtor_t)zend_objects_destroy_object, (void*)splclassloader_object_dtor, NULL TSRMLS_CC);
retval.handlers = &splclassloader_object_handlers;
return retval;
}
/* {{{ proto void SplClassLoader::__construct([string $namespace [, string $include_path]])
Constructor */
PHP_METHOD(SplClassLoader, __construct)
{
char* ns = NULL;
int ns_len = 0;
char* inc_path = NULL;
int inc_path_len = 0;
splclassloader_object* obj;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &ns, &ns_len, &inc_path, &inc_path_len)) {
return; /* should throw ? */
}
obj = (splclassloader_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
if (ns_len) {
obj->ns = estrndup(ns, ns_len);
obj->ns_len = ns_len;
}
if (inc_path_len) {
obj->inc_path = estrndup(inc_path, inc_path_len);
obj->inc_path_len = inc_path_len;
}
} /* }}} */
/* {{{ proto bool SplClassLoader::register()
Installs this class loader on the SPL autoload stack */
PHP_METHOD(SplClassLoader, register)
{
zval* arr;
zval* retval = NULL;
zval* pthis = getThis();
int res = 0;
MAKE_STD_ZVAL(arr);
array_init(arr);
Z_ADDREF_P(pthis);
add_next_index_zval(arr, pthis);
add_next_index_string(arr, estrndup("loadClass", sizeof("loadClass")-1), 0);
zend_call_method_with_1_params(NULL, NULL, NULL, "spl_autoload_register", &retval, arr);
zval_ptr_dtor(&arr);
if (retval) {
res = i_zend_is_true(retval);
zval_ptr_dtor(&retval);
}
RETURN_BOOL(res);
} /* }}} */
/* {{{ proto bool SplClassLoader::unregister()
Uninstalls this class loader from the SPL autoloader stack */
PHP_METHOD(SplClassLoader, unregister)
{
zval* arr;
zval* retval = NULL;
zval* pthis = getThis();
int res = 0;
MAKE_STD_ZVAL(arr);
array_init(arr);
Z_ADDREF_P(pthis);
add_next_index_zval(arr, pthis);
add_next_index_string(arr, estrndup("loadClass", sizeof("loadClass")-1), 0);
zend_call_method_with_1_params(NULL, NULL, NULL, "spl_autoload_unregister", &retval, arr);
zval_ptr_dtor(&arr);
if (retval) {
res = i_zend_is_true(retval);
zval_ptr_dtor(&retval);
}
RETURN_BOOL(res);
} /* }}} */
/* compute filename if (partial) ns and class match. returns filename len including '\0' or 0 */
static int get_filename(splclassloader_object* obj, char* cl, int cl_len, char* filename TSRMLS_DC)
{
char* ccl = cl;
char* cns = obj->ns;
char* cur = &cl[cl_len-1];
int len = 0;
if (cl_len < obj->ns_len) {
return 0;
}
/* we have a ns to compare to the qualified class name */
if (obj->ns) {
int i = 0; /* compare and transform ns separator in place */
for ( ; i < obj->ns_len; ++i) {
if (*cns != *ccl) {
return 0;
}
if (*ccl == SPLCLASSLD_NS_SEPARATOR) {
*ccl = PHP_DIR_SEPARATOR;
}
++ccl;
++cns;
}
if (*ccl != SPLCLASSLD_NS_SEPARATOR) {
return 0;
}
*ccl = PHP_DIR_SEPARATOR;
}
/* transform '_' in the class name from right to left */
for ( ; cur != ccl; --cur) {
if (*cur == SPLCLASSLD_NS_SEPARATOR) {
break;
}
if (*cur == '_') {
*cur = PHP_DIR_SEPARATOR;
}
}
/* tranform the remaining ns separator from right to left */
for ( ; cur != ccl; --cur) {
if (*cur == SPLCLASSLD_NS_SEPARATOR) {
*cur = PHP_DIR_SEPARATOR;
}
}
if (obj->inc_path) {
len = obj->inc_path_len + 1 + cl_len + obj->file_ext_len + 1;
if (len > MAXPATHLEN) {
return 0;
}
/* sprintf: we know the len */
sprintf(filename, "%s%c%s%s", obj->inc_path, PHP_DIR_SEPARATOR, cl, obj->file_ext);
return len;
}
len = cl_len + obj->file_ext_len + 1;
if (len > MAXPATHLEN) {
return 0;
}
/* sprintf: we know the len */
sprintf(filename, "%s%s", cl, obj->file_ext);
return len;
}
/* {{{ proto bool SplClassLoader::loadClass(string $class_name)
Loads the given class or interface */
PHP_METHOD(SplClassLoader, loadClass)
{
char* cl;
int cl_len = 0;
char filename[MAXPATHLEN];
int len = 0;
splclassloader_object* obj;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s/", &cl, &cl_len) /* zval separation is required */
|| !cl_len) {
RETURN_FALSE;
}
obj = (splclassloader_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
len = get_filename(obj, cl, cl_len, filename TSRMLS_CC);
if (len) {
zend_file_handle fh;
fh.filename = filename;
fh.opened_path = NULL;
fh.free_filename = 0;
fh.type = ZEND_HANDLE_FILENAME;
zend_execute_scripts(ZEND_INCLUDE TSRMLS_CC, NULL, 1, &fh);
RETURN_TRUE;
}
RETURN_FALSE;
} /* }}} */
/* {{{ proto bool SplClassLoader::setIncludePath(string $include_path)
Sets the base include path for all class files in the namespace of this class loader */
PHP_METHOD(SplClassLoader, setIncludePath)
{
char* inc_path;
int inc_path_len;
splclassloader_object* obj;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &inc_path, &inc_path_len)) {
RETURN_FALSE;
}
obj = (splclassloader_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->inc_path) {
efree(obj->inc_path);
obj->inc_path = NULL;
}
if (inc_path_len) {
obj->inc_path = estrndup(inc_path, inc_path_len);
}
obj->inc_path_len = inc_path_len;
RETURN_TRUE;
} /* }}} */
/* {{{ proto string SplClassLoader::getIncludePath()
Gets the base include path for all class files in the namespace of this class loader */
PHP_METHOD(SplClassLoader, getIncludePath)
{
splclassloader_object* obj = (splclassloader_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->inc_path) {
RETURN_STRINGL(obj->inc_path, obj->inc_path_len, 1);
}
RETURN_EMPTY_STRING();
} /* }}} */
/* {{{ proto bool SplClassLoader::setFileExtension(string $file_extension)
Sets the file extension of class files in the namespace of this class loader */
PHP_METHOD(SplClassLoader, setFileExtension)
{
char* ext;
int ext_len;
splclassloader_object* obj;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ext, &ext_len)) {
RETURN_FALSE;
}
obj = (splclassloader_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->file_ext) {
efree(obj->file_ext);
obj->file_ext = NULL;
}
if (ext_len) {
obj->file_ext = estrndup(ext, ext_len);
}
obj->file_ext_len = ext_len;
RETURN_TRUE;
} /* }}} */
/* {{{ proto string SplClassLoader::getFileExtension()
Gets the file extension of class files in the namespace of this class loader */
PHP_METHOD(SplClassLoader, getFileExtension)
{
splclassloader_object* obj = (splclassloader_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
if (obj->file_ext) {
RETURN_STRINGL(obj->file_ext, obj->file_ext_len, 1);
}
RETURN_EMPTY_STRING();
} /* }}} */
/* {{{ proto mixed SplClassLoader::getPath(string $class_name)
Gets the path for the given class. Does not load anything. This is just a handy method */
PHP_METHOD(SplClassLoader, getPath)
{
char* cl;
int cl_len = 0;
char filename[MAXPATHLEN];
int len = 0;
splclassloader_object* obj;
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &cl, &cl_len)
|| !cl_len) {
RETURN_FALSE;
}
obj = (splclassloader_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
len = get_filename(obj, cl, cl_len, filename TSRMLS_CC);
if (len) {
RETURN_STRINGL(filename, --len, 1); /* cl_len > 0 and len includes '\0' */
}
RETURN_FALSE;
} /* }}} */
zend_function_entry splclassloader_methods[] = {
PHP_ME(SplClassLoader, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(SplClassLoader, register, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SplClassLoader, unregister, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SplClassLoader, loadClass, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SplClassLoader, setIncludePath, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SplClassLoader, getIncludePath, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SplClassLoader, setFileExtension, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SplClassLoader, getFileExtension, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SplClassLoader, getPath, NULL, ZEND_ACC_PUBLIC)
{ NULL, NULL, NULL }
};
/*const zend_function_entry splclassloader_functions[] = {
{NULL, NULL, NULL}
};*/
PHP_MINIT_FUNCTION(splclassloader)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "SplClassLoader", splclassloader_methods);
splclassloader_ce = zend_register_internal_class(&ce TSRMLS_CC);
splclassloader_ce->create_object = splclassloader_create_object;
memcpy(&splclassloader_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
splclassloader_object_handlers.clone_obj = NULL; /* no cloning! */
return SUCCESS;
}
PHP_MINFO_FUNCTION(splclassloader)
{
php_info_print_table_start ();
php_info_print_table_header(2, "SplClassLoader support", "enabled");
php_info_print_table_row (2, "Conformance", "PSR-0");
php_info_print_table_end ();
}
zend_module_entry splclassloader_module_entry = {
STANDARD_MODULE_HEADER,
"SplClassLoader",
NULL, /*splclassloader_functions, */
PHP_MINIT(splclassloader),
NULL,
NULL,
NULL,
PHP_MINFO(splclassloader),
"0.1",
STANDARD_MODULE_PROPERTIES
};