From 7e15ed8f390174f6870509b43686c0c2cdd6fa42 Mon Sep 17 00:00:00 2001 From: CismonX Date: Wed, 21 Mar 2018 16:21:28 +0800 Subject: [PATCH] update --- .gitignore | 35 ++++++++++++ collections.c | 59 +++++++++++++++++++++ collections_fe.c | 63 ++++++++++++++++++++++ config.m4 | 15 ++++++ config.w32 | 5 ++ php_collections.h | 37 +++++++++++++ php_collections_fe.h | 123 +++++++++++++++++++++++++++++++++++++++++++ stubs/Collection.php | 22 ++++++-- 8 files changed, 355 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 collections.c create mode 100644 collections_fe.c create mode 100644 config.m4 create mode 100644 config.w32 create mode 100644 php_collections.h create mode 100644 php_collections_fe.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2bdbb49 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +.deps +*.lo +*.la +.libs +acinclude.m4 +aclocal.m4 +autom4te.cache +build +config.guess +config.h +config.h.in +config.log +config.nice +config.status +config.sub +configure +configure.in +include +install-sh +libtool +ltmain.sh +Makefile +Makefile.fragments +Makefile.global +Makefile.objects +missing +mkinstalldirs +modules +run-tests.php +tests/*/*.diff +tests/*/*.out +tests/*/*.php +tests/*/*.exp +tests/*/*.log +tests/*/*.sh diff --git a/collections.c b/collections.c new file mode 100644 index 0000000..d037f7e --- /dev/null +++ b/collections.c @@ -0,0 +1,59 @@ +// +// ext-collections/collections.c +// +// @Author CismonX +// + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "php_collections.h" + +PHP_MINIT_FUNCTION(collections) +{ + INIT_CLASS_ENTRY_EX(collections_collection_ce, "Collection", strlen("Collection"), collection_methods); + zend_register_internal_class(&collections_collection_ce); + INIT_CLASS_ENTRY_EX(collections_pair_ce, "Pair", strlen("Pair"), pair_methods); + zend_register_internal_class(&collections_pair_ce); + return SUCCESS; +} + +PHP_RINIT_FUNCTION(collections) +{ +#if defined(COMPILE_DL_COLLECTIONS) && defined(ZTS) + ZEND_TSRMLS_CACHE_UPDATE(); +#endif + return SUCCESS; +} + +PHP_MINFO_FUNCTION(collections) +{ + php_info_print_table_start(); + php_info_print_table_header(2, "collections support", "enabled"); + php_info_print_table_end(); +} + +zend_module_entry collections_module_entry = { + STANDARD_MODULE_HEADER, + "collections", + NULL, + PHP_MINIT(collections), + NULL, + PHP_RINIT(collections), + NULL, + PHP_MINFO(collections), + PHP_COLLECTIONS_VERSION, + STANDARD_MODULE_PROPERTIES +}; + +#ifdef COMPILE_DL_COLLECTIONS +#ifdef ZTS +ZEND_TSRMLS_CACHE_DEFINE() +#endif +ZEND_GET_MODULE(collections) +#endif diff --git a/collections_fe.c b/collections_fe.c new file mode 100644 index 0000000..86b3a20 --- /dev/null +++ b/collections_fe.c @@ -0,0 +1,63 @@ +// +// ext-collections/collections_fe.c +// +// @Author CismonX +// +#include + +#include "php_collections.h" +#include "php_collections_fe.h" + +ZEND_BEGIN_ARG_INFO(action_arginfo, 0) + ZEND_ARG_CALLABLE_INFO(0, action, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(element_arginfo, 0) + ZEND_ARG_INFO(0, element) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(elements_arginfo, 0) + ZEND_ARG_INFO(0, elements) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(predicate_arginfo, 0) + ZEND_ARG_CALLABLE_INFO(0, predicate, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(selector_arginfo, 0) + ZEND_ARG_CALLABLE_INFO(0, selector, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(other_arginfo, 0) + ZEND_ARG_INFO(0, other) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(transform_arginfo, 0) + ZEND_ARG_CALLABLE_INFO(0, transform, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(associate_by_arginfo, 0) + ZEND_ARG_CALLABLE_INFO(0, key_selector, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(associate_by_to_arginfo, 0) + ZEND_ARG_OBJ_INFO(0, destination, Collection, 0) + ZEND_ARG_CALLABLE_INFO(0, key_selector, 0) +ZEND_END_ARG_INFO() + +const zend_function_entry collections_collection_methods[] = { + PHP_ME(Collection, __construct, NULL, ZEND_ACC_PRIVATE | ZEND_ACC_CTOR) + PHP_ME(Collection, addAll, elements_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(Collection, all, predicate_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(Collection, any, predicate_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(Collection, associate, transform_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(Collection, associateBy, associate_by_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(Collection, associateByTo, associate_by_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(Collection, average, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Collection, containsAll, other_arginfo, ZEND_ACC_PUBLIC) + PHP_FE_END +}; + +const zend_function_entry collections_pair_methods[] = { + PHP_FE_END +}; \ No newline at end of file diff --git a/config.m4 b/config.m4 new file mode 100644 index 0000000..9002b5d --- /dev/null +++ b/config.m4 @@ -0,0 +1,15 @@ +PHP_ARG_ENABLE(collections, for collections support, +[ --enable-collections Enable collections support]) + +PHP_ARG_ENABLE(collections, for debug support, +[ --enable-collections-debug Compile with debug symbols]) + +if test "$PHP_COLLECTIONS" != "no"; then + if test "$PHP_COLLECTIONS_DEBUG" != "no"; then + EXTRA_CFLAGS="-O2" + else + EXTRA_CFLAGS="-g -O0" + fi + COLLECTIONS_SRC="collections.c collections_fe.c collections_methods.c" + PHP_NEW_EXTENSION(collections, $COLLECTIONS_SRC, $ext_shared,, $EXTRA_CFLAGS) +fi diff --git a/config.w32 b/config.w32 new file mode 100644 index 0000000..9018a0e --- /dev/null +++ b/config.w32 @@ -0,0 +1,5 @@ +ARG_ENABLE("collections", "enable collections support", "no"); + +if (PHP_COLLECTIONS != "no") { + EXTENSION("collections", "collections.c collections_fe.c collections_methods.c", true); +} diff --git a/php_collections.h b/php_collections.h new file mode 100644 index 0000000..c9273b1 --- /dev/null +++ b/php_collections.h @@ -0,0 +1,37 @@ +// +// ext-collections/php_collections.h +// +// @Author CismonX +// + +#ifndef PHP_COLLECTIONS_H +#define PHP_COLLECTIONS_H + +extern zend_module_entry collections_module_entry; +#define phpext_collections_ptr &collections_module_entry + +extern zend_class_entry collections_collection_ce; +extern zend_class_entry collections_pair_ce; + +const zend_function_entry collection_methods[]; +const zend_function_entry pair_methods[]; + +#define PHP_COLLECTIONS_VERSION "0.1.0" + +#ifdef PHP_WIN32 +#define PHP_COLLECTIONS_API __declspec(dllexport) +#elif defined(__GNUC__) && __GNUC__ >= 4 +#define PHP_COLLECTIONS_API __attribute__ ((visibility("default"))) +#else +#define PHP_COLLECTIONS_API +#endif + +#ifdef ZTS +#include "TSRM.h" +#endif + +#if defined(ZTS) && defined(COMPILE_DL_COLLECTIONS) +ZEND_TSRMLS_CACHE_EXTERN() +#endif + +#endif // !PHP_COLLECTIONS_FE_H \ No newline at end of file diff --git a/php_collections_fe.h b/php_collections_fe.h new file mode 100644 index 0000000..3d010cd --- /dev/null +++ b/php_collections_fe.h @@ -0,0 +1,123 @@ +// +// ext-collections/php_collections_fe.h +// +// @Author CismonX +// + +#ifndef PHP_COLLECTIONS_FE_H +#define PHP_COLLECTIONS_FE_H + +#include + +#include "php_collections.h" + +PHP_METHOD(Collection, __construct); +PHP_METHOD(Collection, addAll); +PHP_METHOD(Collection, all); +PHP_METHOD(Collection, any); +PHP_METHOD(Collection, associate); +PHP_METHOD(Collection, associateTo); +PHP_METHOD(Collection, associateBy); +PHP_METHOD(Collection, associateByTo); +PHP_METHOD(Collection, average); +PHP_METHOD(Collection, containsAll); +PHP_METHOD(Collection, containsKey); +PHP_METHOD(Collection, containsValue); +PHP_METHOD(Collection, copyOf); +PHP_METHOD(Collection, copyOfRange); +PHP_METHOD(Collection, count); +PHP_METHOD(Collection, distinct); +PHP_METHOD(Collection, distinctBy); +PHP_METHOD(Collection, drop); +PHP_METHOD(Collection, dropLast); +PHP_METHOD(Collection, dropLastWhile); +PHP_METHOD(Collection, dropWhile); +PHP_METHOD(Collection, fill); +PHP_METHOD(Collection, filter); +PHP_METHOD(Collection, filterNot); +PHP_METHOD(Collection, filterNotTo); +PHP_METHOD(Collection, filterTo); +PHP_METHOD(Collection, find); +PHP_METHOD(Collection, findLast); +PHP_METHOD(Collection, first); +PHP_METHOD(Collection, flatMap); +PHP_METHOD(Collection, flatMapTo); +PHP_METHOD(Collection, flatten); +PHP_METHOD(Collection, fold); +PHP_METHOD(Collection, foldRight); +PHP_METHOD(Collection, forEach); +PHP_METHOD(Collection, get); +PHP_METHOD(Collection, groupBy); +PHP_METHOD(Collection, groupByTo); +PHP_METHOD(Collection, indexOf); +PHP_METHOD(Collection, indexOfFirst); +PHP_METHOD(Collection, indexOfLast); +PHP_METHOD(Collection, init); +PHP_METHOD(Collection, intersect); +PHP_METHOD(Collection, isEmpty); +PHP_METHOD(Collection, isNotEmpty); +PHP_METHOD(Collection, keys); +PHP_METHOD(Collection, last); +PHP_METHOD(Collection, lastIndexOf); +PHP_METHOD(Collection, map); +PHP_METHOD(Collection, mapKeys); +PHP_METHOD(Collection, mapKeysTo); +PHP_METHOD(Collection, mapTo); +PHP_METHOD(Collection, mapValues); +PHP_METHOD(Collection, mapValuesTo); +PHP_METHOD(Collection, max); +PHP_METHOD(Collection, maxBy); +PHP_METHOD(Collection, maxWith); +PHP_METHOD(Collection, min); +PHP_METHOD(Collection, minBy); +PHP_METHOD(Collection, minWith); +PHP_METHOD(Collection, minus); +PHP_METHOD(Collection, minusAssign); +PHP_METHOD(Collection, minusKeys); +PHP_METHOD(Collection, minusKeysAssign); +PHP_METHOD(Collection, minusValues); +PHP_METHOD(Collection, minusValuesAssign); +PHP_METHOD(Collection, none); +PHP_METHOD(Collection, offsetUnset); +PHP_METHOD(Collection, offsetSet); +PHP_METHOD(Collection, offsetGet); +PHP_METHOD(Collection, offsetExists); +PHP_METHOD(Collection, onEach); +PHP_METHOD(Collection, orEmpty); +PHP_METHOD(Collection, partition); +PHP_METHOD(Collection, plus); +PHP_METHOD(Collection, plusAssign); +PHP_METHOD(Collection, plusValues); +PHP_METHOD(Collection, plusValuesAssign); +PHP_METHOD(Collection, putAll); +PHP_METHOD(Collection, reduce); +PHP_METHOD(Collection, reduceRight); +PHP_METHOD(Collection, remove); +PHP_METHOD(Collection, removeAll); +PHP_METHOD(Collection, retainAll); +PHP_METHOD(Collection, reverse); +PHP_METHOD(Collection, reversed); +PHP_METHOD(Collection, shuffle); +PHP_METHOD(Collection, single); +PHP_METHOD(Collection, slice); +PHP_METHOD(Collection, sort); +PHP_METHOD(Collection, sortBy); +PHP_METHOD(Collection, sortByDescending); +PHP_METHOD(Collection, sortDescending); +PHP_METHOD(Collection, sortWith); +PHP_METHOD(Collection, sorted); +PHP_METHOD(Collection, sortedBy); +PHP_METHOD(Collection, sortedByDescending); +PHP_METHOD(Collection, sortedDescending); +PHP_METHOD(Collection, sortedWith); +PHP_METHOD(Collection, take); +PHP_METHOD(Collection, takeLast); +PHP_METHOD(Collection, takeLastWhile); +PHP_METHOD(Collection, takeWhile); +PHP_METHOD(Collection, toArray); +PHP_METHOD(Collection, toCollection); +PHP_METHOD(Collection, toPairs); +PHP_METHOD(Collection, union); +PHP_METHOD(Collection, values); + +#endif // !PHP_COLLECTIONS_FE_H diff --git a/stubs/Collection.php b/stubs/Collection.php index e82bac6..653a37f 100644 --- a/stubs/Collection.php +++ b/stubs/Collection.php @@ -84,18 +84,18 @@ class Collection implements ArrayAccess, Countable /** * Checks if all elements in the specified collection are contained in this collection. * - * @param Collection $collection + * @param array|Collection $other * @return bool */ - function containsAll($collection) {} + function containsAll($other) {} /** * Checks if the array contains the given key. * - * @param int|string $element + * @param int|string $key * @return bool */ - function containsKey($element) {} + function containsKey($key) {} /** * Check if the array maps one or more keys to the specified value. @@ -391,6 +391,13 @@ class Collection implements ArrayAccess, Countable */ function isNotEmpty() {} + /** + * Return a collection of all keys in the collection. + * + * @return Collection + */ + function keys() {} + /** * Returns the last element matching the given predicate. * @@ -881,4 +888,11 @@ class Collection implements ArrayAccess, Countable * @param array|Collection $other */ function union($other) {} + + /** + * Return a collection of all values of the collection. + * + * @return Collection + */ + function values() {} } \ No newline at end of file