Skip to content

Commit e389939

Browse files
committed
feat: PHP bindings
Signed-off-by: Dmitry Dygalo <[email protected]>
1 parent 8d66557 commit e389939

File tree

13 files changed

+1050
-0
lines changed

13 files changed

+1050
-0
lines changed

.github/workflows/build.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,90 @@ jobs:
571571
DEFAULT_CROSS_BUILD_ENV_URL: "https://github.com/pyodide/pyodide/releases/download/0.28.0a3/xbuildenv-0.28.0a3.tar.bz2"
572572
RUSTFLAGS: "-C link-arg=-sSIDE_MODULE=2 -Z link-native-libraries=no -Z emscripten-wasm-eh"
573573

574+
test-php:
575+
strategy:
576+
fail-fast: false
577+
matrix:
578+
os: [ubuntu-22.04, macos-15]
579+
php-version: ["8.2", "8.3", "8.4"]
580+
clang: ["20"]
581+
582+
name: PHP ${{ matrix.php-version }} on ${{ matrix.os }}
583+
runs-on: ${{ matrix.os }}
584+
steps:
585+
- uses: actions/checkout@v4
586+
587+
- uses: dtolnay/rust-toolchain@stable
588+
589+
- name: Cache LLVM and Clang
590+
id: cache-llvm
591+
uses: actions/cache@v4
592+
if: matrix.os == 'ubuntu-22.04'
593+
with:
594+
path: ${{ runner.temp }}/llvm-${{ matrix.clang }}
595+
key: ${{ matrix.os }}-llvm-${{ matrix.clang }}
596+
597+
- name: Setup LLVM & Clang
598+
id: clang
599+
uses: KyleMayes/install-llvm-action@v2
600+
if: matrix.os == 'ubuntu-22.04'
601+
with:
602+
version: ${{ matrix.clang }}
603+
directory: ${{ runner.temp }}/llvm-${{ matrix.clang }}
604+
cached: ${{ steps.cache-llvm.outputs.cache-hit }}
605+
606+
- name: Configure Clang
607+
if: matrix.os == 'ubuntu-22.04'
608+
run: |
609+
echo "LIBCLANG_PATH=${{ runner.temp }}/llvm-${{ matrix.clang }}/lib" >> $GITHUB_ENV
610+
echo "LLVM_VERSION=${{ steps.clang.outputs.version }}" >> $GITHUB_ENV
611+
echo "LLVM_CONFIG_PATH=${{ runner.temp }}/llvm-${{ matrix.clang }}/bin/llvm-config" >> $GITHUB_ENV
612+
613+
- uses: shivammathur/setup-php@v2
614+
with:
615+
php-version: ${{ matrix.php-version }}
616+
extensions: mbstring
617+
coverage: none
618+
619+
- name: Build PHP extension
620+
run: |
621+
export PHP_CONFIG=$(which php-config)
622+
623+
cargo build --release
624+
625+
EXT_DIR=$(php -r "echo ini_get('extension_dir');")
626+
627+
if [[ "${{ matrix.os }}" == "macos-15" ]]; then
628+
BUILT_LIB=$(find target/release -name "libcss_inline_php.dylib" -o -name "css_inline_php.dylib" | head -1)
629+
if [[ -z "$BUILT_LIB" ]]; then
630+
BUILT_LIB=$(find target/release -name "*.dylib" | head -1)
631+
fi
632+
sudo cp "$BUILT_LIB" "$EXT_DIR/css_inline.so"
633+
else
634+
BUILT_LIB=$(find target/release -name "*.so" | head -1)
635+
sudo cp "$BUILT_LIB" "$EXT_DIR/css_inline.so"
636+
fi
637+
working-directory: ./bindings/php
638+
shell: bash
639+
640+
- name: Enable and verify extension
641+
run: |
642+
if [[ "${{ matrix.os }}" == "macos-15" ]]; then
643+
PHP_INI_DIR=$(php -i | grep "Scan this dir for additional .ini files" | cut -d' ' -f9 | tr -d ' ')
644+
echo "extension=css_inline" | sudo tee "$PHP_INI_DIR/99-css_inline.ini"
645+
else
646+
echo "extension=css_inline" | sudo tee /etc/php/${{ matrix.php-version }}/cli/conf.d/99-css_inline.ini
647+
fi
648+
shell: bash
649+
650+
- name: Install dependencies
651+
run: composer install --no-interaction --prefer-dist
652+
working-directory: ./bindings/php
653+
654+
- name: Run tests
655+
run: composer test
656+
working-directory: ./bindings/php
657+
574658
test-ruby:
575659
strategy:
576660
fail-fast: false

.github/workflows/php-release.yml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
name: "[PHP] Release"
2+
3+
on:
4+
push:
5+
tags:
6+
- php-v*
7+
8+
jobs:
9+
build:
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
include:
14+
- os: ubuntu-22.04
15+
target: x86_64-unknown-linux-gnu
16+
php: "8.2"
17+
artifact: libcss_inline_php.so
18+
- os: ubuntu-22.04
19+
target: x86_64-unknown-linux-gnu
20+
php: "8.3"
21+
artifact: libcss_inline_php.so
22+
- os: ubuntu-22.04
23+
target: x86_64-unknown-linux-gnu
24+
php: "8.4"
25+
artifact: libcss_inline_php.so
26+
- os: macos-15
27+
target: x86_64-apple-darwin
28+
php: "8.2"
29+
artifact: libcss_inline_php.dylib
30+
- os: macos-15
31+
target: x86_64-apple-darwin
32+
php: "8.3"
33+
artifact: libcss_inline_php.dylib
34+
- os: macos-15
35+
target: x86_64-apple-darwin
36+
php: "8.4"
37+
artifact: libcss_inline_php.dylib
38+
- os: macos-15
39+
target: aarch64-apple-darwin
40+
php: "8.2"
41+
artifact: libcss_inline_php.dylib
42+
- os: macos-15
43+
target: aarch64-apple-darwin
44+
php: "8.3"
45+
artifact: libcss_inline_php.dylib
46+
- os: macos-15
47+
target: aarch64-apple-darwin
48+
php: "8.4"
49+
artifact: libcss_inline_php.dylib
50+
51+
runs-on: ${{ matrix.os }}
52+
53+
steps:
54+
- uses: actions/checkout@v6
55+
56+
- name: Set up PHP
57+
uses: shivammathur/setup-php@v2
58+
with:
59+
php-version: ${{ matrix.php }}
60+
61+
- name: Install Rust
62+
uses: dtolnay/rust-toolchain@stable
63+
64+
- uses: Swatinem/rust-cache@v2
65+
with:
66+
workspaces: bindings/php
67+
cache-all-crates: "true"
68+
69+
- name: Build
70+
run: cargo build --release
71+
working-directory: bindings/php
72+
73+
- name: Rename artifact
74+
run: |
75+
mkdir -p dist
76+
cp bindings/php/target/release/${{ matrix.artifact }} dist/css_inline-php${{ matrix.php }}-${{ matrix.target }}.so
77+
78+
- name: Upload artifact
79+
uses: actions/upload-artifact@v6
80+
with:
81+
name: css_inline-php${{ matrix.php }}-${{ matrix.target }}
82+
path: dist/
83+
84+
release:
85+
needs: build
86+
runs-on: ubuntu-22.04
87+
permissions:
88+
contents: write
89+
90+
steps:
91+
- uses: actions/checkout@v6
92+
93+
- name: Extract Version
94+
run: echo "version=${GITHUB_REF#refs/tags/php-v}" >> $GITHUB_ENV
95+
96+
- name: Download all artifacts
97+
uses: actions/download-artifact@v7
98+
with:
99+
path: artifacts
100+
merge-multiple: true
101+
102+
- name: List artifacts
103+
run: ls -la artifacts/
104+
105+
- name: GitHub Release
106+
uses: softprops/action-gh-release@v2
107+
with:
108+
make_latest: false
109+
draft: true
110+
name: "[PHP] Release ${{ env.version }}"
111+
files: artifacts/*

bindings/php/.cargo/config.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[target.x86_64-unknown-linux-gnu]
2+
rustflags = ["-C", "link-arg=-Wl,-undefined,dynamic_lookup"]
3+
4+
[target.x86_64-apple-darwin]
5+
rustflags = ["-C", "link-arg=-Wl,-undefined,dynamic_lookup"]
6+
7+
[target.aarch64-apple-darwin]
8+
rustflags = ["-C", "link-arg=-Wl,-undefined,dynamic_lookup"]
9+
10+
[target.x86_64-pc-windows-msvc]
11+
linker = "rust-lld"
12+
rustflags = ["-C", "link-arg=/FORCE"]

bindings/php/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/composer.lock
3+
/.phpunit.cache/

bindings/php/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "css_inline"
3+
version = "0.18.0"
4+
edition = "2021"
5+
authors = ["Dmitry Dygalo <[email protected]>"]
6+
7+
[lib]
8+
name = "css_inline_php"
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
ext-php-rs = "0.15.2"
13+
rayon = "1"
14+
15+
[dependencies.css-inline]
16+
path = "../../css-inline"
17+
version = "*"
18+
default-features = false
19+
features = ["http", "file", "stylesheet-cache"]

0 commit comments

Comments
 (0)