Skip to content

Commit 42fac86

Browse files
committed
initial commit
1 parent 8e06aeb commit 42fac86

Some content is hidden

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

76 files changed

+6495
-0
lines changed

.editorconfig

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
insert_final_newline = true
5+
6+
[*.{c,h}]
7+
indent_style = space
8+
indent_size = 4
9+
10+
[*.md]
11+
trim_trailing_whitespace = false
12+
13+
[*.phpt]
14+
trim_trailing_whitespace = true
15+
indent_style = space
16+
indent_size = 4
17+
18+
[package.xml]
19+
indent_style = space
20+
indent_size = 1

.github/ISSUE_TEMPLATE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!--
2+
Thank you for creating a ticket!
3+
4+
If you have questions, you can also join the Gitter channel.
5+
https://gitter.im/php-kafka/php-kafka
6+
7+
Please always include the following in an issue, so we can help faster:
8+
-->
9+
10+
* PHP version:
11+
* librdkafka version:
12+
* php-kafka version:
13+
* kafka version:
14+
15+
If possible, add debug logs from librdkafka, which you can obtain by adjusting your configuration:
16+
```php
17+
<?php
18+
$conf = new Kafka\Configuration();
19+
// Add the two settings below for a more verbose output
20+
$conf->set('log_level', (string) LOG_DEBUG);
21+
$conf->set('debug', 'all');
22+
```

.github/workflows/test.yml

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: 'Tests'
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
tests:
11+
name: 'Tests'
12+
strategy:
13+
matrix:
14+
include:
15+
- php: '8.0.0'
16+
librdkafka: 'v1.5.3'
17+
memcheck: '1'
18+
- php: '8.0.0'
19+
librdkafka: 'v1.5.3'
20+
- php: '7.4.0'
21+
librdkafka: 'v1.5.3'
22+
- php: '7.3.0'
23+
librdkafka: 'v1.5.3'
24+
- php: '8.0.0'
25+
librdkafka: 'v1.4.4'
26+
- php: '7.4.0'
27+
librdkafka: 'v1.4.4'
28+
- php: '7.3.0'
29+
librdkafka: 'v1.4.4'
30+
- php: '8.0.0'
31+
librdkafka: 'master'
32+
experimental: true
33+
- php: '7.4.0'
34+
librdkafka: 'master'
35+
experimental: true
36+
- php: '7.3.0'
37+
librdkafka: 'master'
38+
experimental: true
39+
40+
runs-on: 'ubuntu-18.04'
41+
continue-on-error: ${{ !!matrix.experimental }}
42+
env:
43+
PHP_VERSION: ${{ matrix.php }}
44+
LIBRDKAFKA_VERSION: ${{ matrix.librdkafka }}
45+
MEMORY_CHECK: ${{ matrix.memcheck }}
46+
TEST_KAFKA_BROKERS: kafka:9092
47+
TEST_KAFKA_BROKER_VERSION: 2.6
48+
steps:
49+
- name: 'Check out repository'
50+
uses: 'actions/checkout@v2'
51+
with:
52+
path: 'php-kafka'
53+
54+
- uses: actions/cache@v2
55+
with:
56+
path: ~/build-cache/php
57+
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.memcheck }}
58+
59+
- uses: actions/cache@v2
60+
with:
61+
path: ~/build-cache/librdkafka
62+
key: ${{ runner.os }}-${{ matrix.librdkafka }}
63+
64+
- name: 'Build librdkafka'
65+
run: './php-kafka/.github/workflows/test/build-librdkafka.sh'
66+
67+
- name: 'Build PHP'
68+
run: './php-kafka/.github/workflows/test/build-php.sh'
69+
70+
- name: 'Build php-kafka'
71+
run: './php-kafka/.github/workflows/test/build-php-kafka.sh'
72+
73+
- name: 'Start Kafka'
74+
run: './php-kafka/.github/workflows/test/start-kafka.sh'
75+
76+
- name: 'Run tests'
77+
run: './php-kafka/.github/workflows/test/tests.sh'
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
set -ex
4+
5+
if ! [ -f ~/build-cache/librdkafka/usr/local/include/librdkafka/rdkafka.h ] || ! [ -f ~/build-cache/librdkafka/usr/local/bin/kafkacat ]; then
6+
echo "librdkafka build is not cached"
7+
8+
git clone --depth 1 --branch "${LIBRDKAFKA_VERSION:-1.5.0}" "${LIBRDKAFKA_REPOSITORY_URL:-https://github.com/edenhill/librdkafka.git}"
9+
10+
cd librdkafka
11+
./configure
12+
make
13+
mkdir -p ~/build-cache/librdkafka
14+
sudo make install DESTDIR=$HOME/build-cache/librdkafka
15+
test -f ~/build-cache/librdkafka/usr/local/include/librdkafka/rdkafka.h || echo "librdkafka build failed"
16+
17+
sudo rsync -a ~/build-cache/librdkafka/ /
18+
sudo ldconfig
19+
cd ..
20+
21+
git clone --depth 1 --branch "1.6.0" "${LIBRDKAFKA_REPOSITORY_URL:-https://github.com/edenhill/kafkacat.git}"
22+
23+
cd kafkacat
24+
./configure
25+
make
26+
sudo make install DESTDIR=$HOME/build-cache/librdkafka
27+
28+
else
29+
echo "librdkafka build is cached"
30+
fi
31+
32+
sudo rsync -av ~/build-cache/librdkafka/ /
33+
sudo ldconfig
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
echo "Building php-kafka with PHP version:"
6+
php --version
7+
8+
if [ $MEMORY_CHECK -eq 1 ]; then
9+
PHP_KAFKA_CFLAGS="-Wall -Werror -Wno-deprecated-declarations"
10+
fi
11+
12+
cd php-kafka
13+
phpize
14+
CFLAGS="$PHP_KAFKA_CFLAGS" ./configure
15+
make
16+
17+
echo "extension=$(pwd)/modules/kafka.so"|sudo tee /usr/local/etc/php/kafka.ini >/dev/null

.github/workflows/test/build-php.sh

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
3+
set -ex
4+
5+
if [ $MEMORY_CHECK -eq 1 ]; then
6+
sudo bash -c 'apt-get update;apt-get -y install valgrind'
7+
fi
8+
9+
if ! [ -f ~/build-cache/php/usr/local/bin/php ]; then
10+
echo "PHP build is not cached"
11+
12+
wget https://secure.php.net/distributions/php-${PHP_VERSION}.tar.bz2
13+
tar xjf php-${PHP_VERSION}.tar.bz2
14+
cd php-${PHP_VERSION}
15+
16+
PHP_BUILD_FLAGS="--prefix=/usr/local --disable-all --enable-cli --enable-cgi --with-config-file-scan-dir=/usr/local/etc/php --with-zlib"
17+
18+
if [ $MEMORY_CHECK -eq 1 ]; then
19+
PHP_BUILD_FLAGS="$PHP_BUILD_FLAGS --enable-debug --with-valgrind"
20+
else
21+
case $PHP_VERSION in
22+
8.*)
23+
PHP_BUILD_FLAGS="$PHP_BUILD_FLAGS --enable-zts"
24+
;;
25+
7.*)
26+
PHP_BUILD_FLAGS="$PHP_BUILD_FLAGS --enable-maintainer-zts"
27+
;;
28+
esac
29+
fi
30+
31+
./configure $PHP_BUILD_FLAGS $PHP_BUILD_EXTRA_FLAGS
32+
make -j $(nproc)
33+
mkdir -p ~/build-cache/php
34+
sudo make install INSTALL_ROOT=$HOME/build-cache/php
35+
else
36+
echo "PHP build is cached"
37+
fi
38+
39+
sudo rsync -av ~/build-cache/php/ /
40+
sudo mkdir -p /usr/local/etc/php

.github/workflows/test/start-kafka.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
docker network create kafka_network
4+
docker pull wurstmeister/zookeeper:3.4.6
5+
docker run -d --network kafka_network --name zookeeper wurstmeister/zookeeper:3.4.6
6+
docker pull wurstmeister/kafka:2.13-2.6.0
7+
docker run -d -p 9092:9092 --network kafka_network -e "KAFKA_AUTO_CREATE_TOPICS_ENABLE=true" -e "KAFKA_CREATE_TOPICS=test-topic:1:1:compact" -e "KAFKA_ADVERTISED_HOST_NAME=kafka" -e "KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181" -e "KAFKA_ADVERTISED_PORT=9092" -e "KAFKA_BROKER_ID=1" --name kafka wurstmeister/kafka:2.13-2.6.0
8+
printf "\n127.0.0.1 kafka\n"|sudo tee /etc/hosts >/dev/null
9+
10+
echo "Waiting for Kafka to be ready"
11+
12+
for i in $(seq 1 20); do
13+
if kafkacat -b 127.0.0.1 -L; then
14+
echo "Kafka is ready"
15+
exit 0
16+
fi
17+
done
18+
19+
echo "Timedout waiting for Kafka to be ready"
20+
exit 1

.github/workflows/test/tests.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
3+
set -xve
4+
5+
cd php-kafka
6+
7+
if [ $MEMORY_CHECK -eq 1 ]; then
8+
echo "Enabling memory checking"
9+
showmem=--show-mem
10+
checkmem=-m
11+
fi
12+
13+
cp tests/test_env.php.sample tests/test_env.php
14+
15+
PHP=$(which php)
16+
REPORT_EXIT_STATUS=1 TEST_PHP_EXECUTABLE="$PHP" "$PHP" run-tests.php -q $checkmem --show-diff $showmem

CONTRIBUTING.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# How to contribute
2+
All contributions to the project are welcome.
3+
Please keep the following in mind when contributing:
4+
5+
## Branches
6+
Pull requests should be made against the main branch, which supports both PHP 7 and PHP 8.
7+
8+
## Testing
9+
Tests are in phpt file format in the tests directory.
10+
11+
### Using your own machine for building and testing.
12+
Tests can be run by following compilation and installation procedure
13+
and executing `make test`.
14+
To run integration tests, make sure you have Kafka instance running.
15+
Then, rename `test_env.php.sample` to `test_env.php` and adjust it
16+
with values proper for your kafka instance.

LICENSE

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2020, Nick Chiu
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
31+
Based on [php-rdkafka](https://github.com/arnaud-lb/php-rdkafka)
32+
33+
The MIT License (MIT)
34+
35+
Copyright (c) 2015 Arnaud Le Blanc
36+
37+
Permission is hereby granted, free of charge, to any person obtaining a copy
38+
of this software and associated documentation files (the "Software"), to deal
39+
in the Software without restriction, including without limitation the rights
40+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
41+
copies of the Software, and to permit persons to whom the Software is
42+
furnished to do so, subject to the following conditions:
43+
44+
The above copyright notice and this permission notice shall be included in all
45+
copies or substantial portions of the Software.
46+
47+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
48+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
49+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
50+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
51+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
52+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53+
SOFTWARE.

config.m4

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
dnl $Id$
2+
dnl config.m4 for extension kafka
3+
4+
PHP_ARG_WITH(kafka, for kafka support,
5+
[ --with-kafka Include kafka support])
6+
7+
dnl Check whether the extension is enabled at all
8+
if test "$PHP_KAFKA" != "no"; then
9+
10+
SEARCH_PATH="/usr/local /usr" # you might want to change this
11+
SEARCH_FOR="/include/librdkafka/rdkafka.h" # you most likely want to change this
12+
if test -r $PHP_KAFKA/$SEARCH_FOR; then # path given as parameter
13+
RDKAFKA_DIR=$PHP_KAFKA
14+
else # search default path list
15+
AC_MSG_CHECKING([for librdkafka/rdkafka.h" in default path])
16+
for i in $SEARCH_PATH ; do
17+
if test -r $i/$SEARCH_FOR; then
18+
RDKAFKA_DIR=$i
19+
AC_MSG_RESULT(found in $i)
20+
fi
21+
done
22+
fi
23+
24+
if test -z "$RDKAFKA_DIR"; then
25+
AC_MSG_RESULT([not found])
26+
AC_MSG_ERROR([Please reinstall the rdkafka distribution])
27+
fi
28+
29+
PHP_ADD_INCLUDE($RDKAFKA_DIR/include)
30+
31+
SOURCES="kafka.c producer.c metadata.c metadata_broker.c metadata_topic.c metadata_partition.c metadata_collection.c configuration.c topic.c message.c functions.c consumer.c topic_partition.c kafka_exception.c"
32+
33+
LIBNAME=rdkafka
34+
LIBSYMBOL=rd_kafka_new
35+
36+
PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
37+
[
38+
PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $RDKAFKA_DIR/$PHP_LIBDIR, KAFKA_SHARED_LIBADD)
39+
AC_DEFINE(HAVE_RDKAFKALIB,1,[ ])
40+
],[
41+
AC_MSG_ERROR([wrong rdkafka lib version or lib not found])
42+
],[
43+
-L$RDKAFKA_DIR/$PHP_LIBDIR -lm
44+
])
45+
46+
ORIG_LDFLAGS="$LDFLAGS"
47+
ORIG_CPPFLAGS="$CPPFLAGS"
48+
LDFLAGS="-L$RDKAFKA_DIR/$PHP_LIBDIR -lm"
49+
CPPFLAGS="-I$RDKAFKA_DIR/include"
50+
51+
AC_MSG_CHECKING([for librdkafka version])
52+
AC_EGREP_CPP(yes,[
53+
#include <librdkafka/rdkafka.h>
54+
#if RD_KAFKA_VERSION >= 0x000b0000
55+
yes
56+
#endif
57+
],[
58+
AC_MSG_RESULT([>= 1.4.0])
59+
],[
60+
AC_MSG_ERROR([librdkafka version 1.4.0 or greater required.])
61+
])
62+
63+
LDFLAGS="$ORIG_LDFLAGS"
64+
CPPFLAGS="$ORIG_CPPFLAGS"
65+
66+
PHP_SUBST(KAFKA_SHARED_LIBADD)
67+
68+
PHP_NEW_EXTENSION(kafka, $SOURCES, $ext_shared)
69+
fi

0 commit comments

Comments
 (0)