Skip to content

Commit 809eb82

Browse files
matyhtfNathanFreemanCopilot
authored
Add stdext module for swoole-cli and enhance PHP standard library (#5842)
* module stdext * update * code format * code format * typed array * code format * update * init values * fix * update * add marco (#5832) * add marco * merge upstream * fix * add tests --filter=[unit] * refactor typed array --filter=[unit] * Update typed array --filter=[unit] * Update typed array --filter=[unit] * Update typed array --filter=[unit] * optimize code --filter=[unit] * Fix --filter=[unit] * Update --filter=[unit] * Update --filter=[unit] * fix --filter=[unit] * fix --filter=[unit] * fix --filter=[unit] * [6.1] optimize feature (#5840) * optimize feature * fix error * add mbstring * Fix --filter=[unit] * Fix --filter=[unit] * Update --filter=[unit] * Fix arginfo --filter=[unit] * optimize feature (#5841) * optimize feature * Update swoole_stdext.cc --------- Co-authored-by: Tianfeng.Han <[email protected]> * Optimize --filter=[unit] * Update ext-src/swoole_stdext.cc Co-authored-by: Copilot <[email protected]> * Optimize code --filter=[unit] * Update thirdparty/php/zend/zend_execute.c Co-authored-by: Copilot <[email protected]> * Update ext-src/swoole_stdext.cc Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: MARiA so cute <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 1bfe608 commit 809eb82

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3430
-39
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ if (DEFINED verbose)
137137
add_definitions(-DSW_VERBOSE)
138138
endif()
139139

140+
set(php_dir "" CACHE STRING "Set the root directory of PHP")
141+
140142
if (DEFINED php_dir)
141143
set(PHP_CONFIG "${php_dir}/bin/php-config")
142144
else ()

config.m4

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ PHP_ARG_ENABLE([swoole-thread],
116116
[AS_HELP_STRING([--enable-swoole-thread],
117117
[Enable swoole thread support])], [no], [no])
118118

119+
PHP_ARG_ENABLE([swoole-stdext],
120+
[whether to enable swoole stdext support],
121+
[AS_HELP_STRING([--enable-swoole-stdext],
122+
[Enable swoole stdext support([Experimental] This module is only used for swoole-cli. If you are unsure which feature you need, keep it disabled)])], [no], [no])
123+
119124
PHP_ARG_ENABLE([swoole-coro-time],
120125
[whether to enable coroutine execution time ],
121126
[AS_HELP_STRING([--enable-swoole-coro-time],
@@ -965,6 +970,10 @@ EOF
965970
AC_DEFINE(SW_THREAD, 1, [enable swoole thread support])
966971
fi
967972

973+
if test "$PHP_SWOOLE_STDEXT" != "no"; then
974+
AC_DEFINE(SW_STDEXT, 1, [enable swoole stdext support])
975+
fi
976+
968977
if test "$PHP_SOCKETS" = "yes"; then
969978
AC_MSG_CHECKING([for php_sockets.h])
970979

examples/stdext/array.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
$array = [1, 2, 3, 4, 5];
3+
4+
$arr = $array->slice(1, 2);
5+
var_dump($arr);
6+
7+
$array1 = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
8+
$array2 = [6, 7, 8, 9, 10, 11, 12];
9+
10+
var_dump($array2->count());
11+
12+
// var_dump($array2->all(function ($value) {
13+
// return $value > 10;
14+
// }));
15+
16+
$input_array = array("FirSt" => 1, "SecOnd" => 4);
17+
print_r($input_array->changeKeyCase(CASE_UPPER));
18+
19+
$array4 = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
20+
$key = $array4->search('green');
21+
var_dump($key);
22+
23+
echo "==================================[contains]===================================\n";
24+
$os = array("Mac", "Windows", "Linux");
25+
var_dump($os->contains("Windows"));
26+
var_dump($os->contains("Unix"));
27+
28+
echo "==================================[isList]===================================\n";
29+
var_dump($array1->isList());
30+
var_dump($array2->isList());
31+
32+
var_dump($array1->keys());
33+
var_dump($array2->values());
34+
35+
echo "==================================[join]===================================\n";
36+
var_dump(['a', 'b', 'c']->join(','));
37+
38+
echo "==================================[method not exists]===================================\n";
39+
try {
40+
$array->notExists();
41+
} catch (throwable $e) {
42+
echo "Caught exception: ", $e->getMessage(), "\n";
43+
}
44+
45+
function odd($var)
46+
{
47+
// returns whether the input integer is odd
48+
return $var & 1;
49+
}
50+
51+
function even($var)
52+
{
53+
// returns whether the input integer is even
54+
return !($var & 1);
55+
}
56+
57+
echo "Odd :\n";
58+
print_r($array1->filter("odd"));
59+
60+
echo "Even:\n";
61+
print_r($array2->filter("even"));
62+
63+
64+
echo "==================================[array_map]===================================\n";
65+
66+
$a = [1, 2, 3, 4, 5];
67+
$b = $a->map(function ($n) {
68+
return ($n * $n * $n);
69+
});
70+
var_dump($b);
71+
72+
echo "==================================[array_key_exists]===================================\n";
73+
$searchArray = ['first' => null, 'second' => 4];
74+
75+
var_dump(isset($searchArray['first']));
76+
var_dump($searchArray->keyExists('first'));

examples/stdext/foreach.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
$arr = typed_array('<int>');
3+
4+
$arr[] = 1;
5+
// $arr[0] += 10;
6+
// assert($arr[0] == 11);
7+
$arr[0] .= "hello world";

examples/stdext/ref.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
$fruits = array("lemon", "orange", "banana", "apple");
4+
echo "Before sorting:\n";
5+
foreach ($fruits as $key => $val) {
6+
echo "fruits[" . $key . "] = " . $val . "\n";
7+
}
8+
9+
$b = &$fruits;
10+
$b->sort(SORT_NATURAL | SORT_FLAG_CASE);
11+
12+
echo "After sorting:\n";
13+
foreach ($fruits as $key => $val) {
14+
echo "fruits[" . $key . "] = " . $val . "\n";
15+
}
16+
17+
$stack = array("orange", "banana", "apple", "raspberry");
18+
19+
$ref = &$stack;
20+
$fruit = $ref->shift();
21+
var_dump($stack);
22+
23+
$ref->unshift("kiwi");
24+
var_dump($stack);

examples/stdext/string.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* This file is part of Swoole.
4+
*
5+
* @link https://www.swoole.com
6+
* @contact [email protected]
7+
* @license https://github.com/swoole/library/blob/master/LICENSE
8+
*/
9+
10+
declare(strict_types=1);
11+
$str = 'hello world';
12+
var_dump($str->toUpper());
13+
14+
var_dump($str->split(' ')->search('world'));
15+
var_dump($str->length());
16+
var_dump('test'->length());
17+
18+
var_dump($str->indexOf('world'));
19+
var_dump($str->substr(1, 4));
20+
21+
var_dump($str->startsWith('hello'));
22+
var_dump($str->endsWith('world'));
23+
var_dump($str->endsWith('.php'));
24+
25+
var_dump($str->md5(), $str->sha1(), $str->crc32());
26+
var_dump($str->hash('sha256'));
27+
echo "==============================hash=====================\n";
28+
var_dump($str->md5() === $str->hash('md5'));
29+
30+
$str = 'first=value&arr[]=foo+bar&arr[]=baz';
31+
$output = $str->parseStr();
32+
echo $output['first']; // value
33+
echo $output['arr'][0]; // foo bar
34+
echo $output['arr'][1]; // baz
35+
36+
var_dump($str->urlEncode());

examples/stdext/typed_array.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
$list = typed_array('<int>');
3+
4+
$list[] = 123;
5+
$list[] = 345;
6+
$list[] = 'hello'; // 异常
7+

examples/stdext/typed_array_map.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
$list = typed_array('<string, int>');
3+
4+
$list["hello"] = 123;
5+
$list[] = 345;
6+
$list["hello"] = 'hello';

ext-src/php_swoole.cc

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ BEGIN_EXTERN_C()
2828

2929
#include "stubs/php_swoole_arginfo.h"
3030
#include "stubs/php_swoole_ex_arginfo.h"
31+
#ifdef SW_STDEXT
32+
#include "stubs/php_swoole_stdext_arginfo.h"
33+
#endif
3134
END_EXTERN_C()
3235

3336
#include "swoole_mime_type.h"
@@ -102,6 +105,10 @@ static PHP_FUNCTION(swoole_internal_call_user_shutdown_begin);
102105
static PHP_FUNCTION(swoole_implicit_fn);
103106
SW_EXTERN_C_END
104107

108+
#ifdef SW_STDEXT
109+
#include "php_swoole_stdext.h"
110+
#endif
111+
105112
// clang-format off
106113
const zend_function_entry swoole_functions[] = {
107114
PHP_FE(swoole_version, arginfo_swoole_version)
@@ -148,6 +155,32 @@ const zend_function_entry swoole_functions[] = {
148155
ZEND_FE(swoole_name_resolver_lookup, arginfo_swoole_name_resolver_lookup)
149156
ZEND_FE(swoole_name_resolver_add, arginfo_swoole_name_resolver_add)
150157
ZEND_FE(swoole_name_resolver_remove, arginfo_swoole_name_resolver_remove)
158+
// for stdext
159+
#ifdef SW_STDEXT
160+
ZEND_FE(swoole_call_array_method, arginfo_swoole_call_array_method)
161+
ZEND_FE(swoole_call_string_method, arginfo_swoole_call_string_method)
162+
ZEND_FE(swoole_call_stream_method, arginfo_swoole_call_stream_method)
163+
ZEND_FE(swoole_array_search, arginfo_swoole_array_search)
164+
ZEND_FE(swoole_array_contains, arginfo_swoole_array_contains)
165+
ZEND_FE(swoole_array_join, arginfo_swoole_array_join)
166+
ZEND_FE(swoole_array_key_exists, arginfo_swoole_array_key_exists)
167+
ZEND_FE(swoole_array_map, arginfo_swoole_array_map)
168+
ZEND_FE(swoole_str_split, arginfo_swoole_str_split)
169+
ZEND_FE(swoole_parse_str, arginfo_swoole_parse_str)
170+
ZEND_FE(swoole_hash, arginfo_swoole_hash)
171+
ZEND_FE(swoole_typed_array, arginfo_swoole_typed_array)
172+
ZEND_FE(swoole_array_is_typed, arginfo_swoole_array_is_typed)
173+
ZEND_FE(swoole_str_is_empty, arginfo_swoole_str_is_empty)
174+
ZEND_FE(swoole_array_is_empty, arginfo_swoole_array_is_empty)
175+
ZEND_FE(swoole_str_match, arginfo_swoole_str_match)
176+
ZEND_FE(swoole_str_match_all, arginfo_swoole_str_match_all)
177+
ZEND_FE(swoole_str_json_decode, arginfo_swoole_str_json_decode)
178+
ZEND_FE(swoole_str_json_decode_to_object, arginfo_swoole_str_json_decode_to_object)
179+
ZEND_FE(swoole_str_replace, arginfo_swoole_str_replace)
180+
ZEND_FE(swoole_str_ireplace, arginfo_swoole_str_ireplace)
181+
ZEND_FE(swoole_array_replace_str, arginfo_swoole_array_replace_str)
182+
ZEND_FE(swoole_array_ireplace_str, arginfo_swoole_array_ireplace_str)
183+
#endif
151184
PHP_FE_END /* Must be the last line in swoole_functions[] */
152185
};
153186

@@ -430,7 +463,7 @@ SW_API bool php_swoole_unserialize(zend_string *data, zval *zv) {
430463
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
431464
if (!unserialized) {
432465
swoole_warning("unserialize() failed, Error at offset " ZEND_LONG_FMT " of %zd bytes",
433-
(zend_long)((char *) p - ZSTR_VAL(data)),
466+
(zend_long) ((char *) p - ZSTR_VAL(data)),
434467
l);
435468
}
436469
return unserialized;
@@ -807,6 +840,10 @@ PHP_MINIT_FUNCTION(swoole) {
807840
CG(function_table), "swoole_coroutine_create", CG(function_table), "go", arginfo_swoole_coroutine_create);
808841
SW_FUNCTION_ALIAS(
809842
CG(function_table), "swoole_coroutine_defer", CG(function_table), "defer", arginfo_swoole_coroutine_defer);
843+
#ifdef SW_STDEXT
844+
SW_FUNCTION_ALIAS(
845+
CG(function_table), "swoole_typed_array", CG(function_table), "typed_array", arginfo_swoole_typed_array);
846+
#endif
810847
}
811848

812849
swoole_init();
@@ -875,6 +912,9 @@ PHP_MINIT_FUNCTION(swoole) {
875912
php_swoole_thread_map_minit(module_number);
876913
php_swoole_thread_arraylist_minit(module_number);
877914
#endif
915+
#ifdef SW_STDEXT
916+
php_swoole_stdext_minit(module_number);
917+
#endif
878918

879919
SwooleG.fatal_error = fatal_error;
880920
Socket::default_buffer_size = SWOOLE_G(socket_buffer_size);
@@ -1589,7 +1629,7 @@ static PHP_FUNCTION(swoole_substr_unserialize) {
15891629
if ((zend_long) buf_len <= offset) {
15901630
RETURN_FALSE;
15911631
}
1592-
if (length <= 0 || length > (zend_long)(buf_len - offset)) {
1632+
if (length <= 0 || length > (zend_long) (buf_len - offset)) {
15931633
length = buf_len - offset;
15941634
}
15951635
zend::unserialize(return_value, buf + offset, length, options ? Z_ARRVAL_P(options) : NULL);
@@ -1629,7 +1669,7 @@ static PHP_FUNCTION(swoole_substr_json_decode) {
16291669
php_error_docref(nullptr, E_WARNING, "Offset must be less than the length of the string");
16301670
RETURN_NULL();
16311671
}
1632-
if (length <= 0 || length > (zend_long)(str_len - offset)) {
1672+
if (length <= 0 || length > (zend_long) (str_len - offset)) {
16331673
length = str_len - offset;
16341674
}
16351675
/* For BC reasons, the bool $assoc overrides the long $options bit for PHP_JSON_OBJECT_AS_ARRAY */

0 commit comments

Comments
 (0)