From cb6c3d035a9bdf753a609859dd1b31357748bbfa Mon Sep 17 00:00:00 2001
From: Sophy SEM <semsphy@gmail.com>
Date: Mon, 18 Nov 2024 18:24:57 +0700
Subject: [PATCH] Added h3 extension layer

---
 Readme.md             |  1 +
 layers/h3/Dockerfile  | 41 +++++++++++++++++++++++++++++++++++++++++
 layers/h3/config.json |  8 ++++++++
 layers/h3/test.php    |  9 +++++++++
 4 files changed, 59 insertions(+)
 create mode 100644 layers/h3/Dockerfile
 create mode 100644 layers/h3/config.json
 create mode 100644 layers/h3/test.php

diff --git a/Readme.md b/Readme.md
index 9b1b58ef..6b1d2348 100644
--- a/Readme.md
+++ b/Readme.md
@@ -64,6 +64,7 @@ functions:
 | gnupg            | `${bref-extra:gnupg-php-81}`           |
 | GMP              | `${bref-extra:gmp-php-81}`             |
 | gRPC             | `${bref-extra:grpc-php-81}`            |
+| h3               | `${bref-extra:grpc-php-81}`            |
 | Igbinary         | `${bref-extra:igbinary-php-81}`        |
 | Imagick          | `${bref-extra:imagick-php-81}`         |
 | IMAP             | `${bref-extra:imap-php-81}`            |
diff --git a/layers/h3/Dockerfile b/layers/h3/Dockerfile
new file mode 100644
index 00000000..26139da8
--- /dev/null
+++ b/layers/h3/Dockerfile
@@ -0,0 +1,41 @@
+ARG PHP_VERSION
+ARG BREF_VERSION
+FROM bref/build-php-$PHP_VERSION:$BREF_VERSION AS ext
+ARG PHP_VERSION
+
+# Prepare environment
+ENV H3_BUILD_DIR=${BUILD_DIR}/h3
+RUN mkdir -p ${H3_BUILD_DIR}
+
+# Compile h3
+WORKDIR ${H3_BUILD_DIR}
+RUN git clone https://github.com/uber/h3.git && \
+    cd h3 && \
+    git checkout v3.7.2 && \
+    cmake -DBUILD_SHARED_LIBS=ON . && \
+    make -j "$(nproc)" && \
+    make install
+
+# Compile the php h3 extension
+WORKDIR ${H3_BUILD_DIR}/h3-php
+RUN curl -L https://github.com/abler98/h3-php/archive/refs/tags/v1.0.0.tar.gz -o h3.tar.gz && \
+    tar xzf h3.tar.gz && \
+    cd h3-php-1.0.0 && \
+    phpize && \
+    ./configure --with-h3 && \
+    make && \
+    make install
+
+RUN cp `php-config --extension-dir`/h3.so /tmp/h3.so
+RUN strip --strip-debug /tmp/h3.so
+RUN echo 'extension=h3.so' > /tmp/ext.ini
+
+RUN php /bref/lib-copy/copy-dependencies.php /tmp/h3.so /tmp/extension-libs
+
+# Build the final image with just the files we need
+FROM scratch
+
+# Copy things we installed to the final image
+COPY --from=ext /tmp/h3.so /opt/bref/extensions/h3.so
+COPY --from=ext /tmp/ext.ini /opt/bref/etc/php/conf.d/ext-h3.ini
+COPY --from=ext /tmp/extension-libs /opt/lib
diff --git a/layers/h3/config.json b/layers/h3/config.json
new file mode 100644
index 00000000..eea4b0f4
--- /dev/null
+++ b/layers/h3/config.json
@@ -0,0 +1,8 @@
+{
+    "php": [
+        "80",
+        "81",
+        "82",
+        "83"
+    ]
+}
diff --git a/layers/h3/test.php b/layers/h3/test.php
new file mode 100644
index 00000000..b79a3202
--- /dev/null
+++ b/layers/h3/test.php
@@ -0,0 +1,9 @@
+<?php
+
+if (!class_exists($class = \H3\H3Index::class)) {
+    echo sprintf('FAIL: Class "%s" does not exist.', $class).PHP_EOL;
+    exit(1);
+}
+
+
+exit(0);