Skip to content

Commit 78dfe0b

Browse files
committed
feat: PHP bindings
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent 8d66557 commit 78dfe0b

16 files changed

Lines changed: 1265 additions & 3 deletions

File tree

.github/workflows/build.yml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,94 @@ 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@v6
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: Lint PHP code
655+
run: composer lint
656+
working-directory: ./bindings/php
657+
658+
- name: Run tests
659+
run: composer test
660+
working-directory: ./bindings/php
661+
574662
test-ruby:
575663
strategy:
576664
fail-fast: false

.github/workflows/php-release.yml

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
cross: true
31+
- os: macos-15
32+
target: x86_64-apple-darwin
33+
php: "8.3"
34+
artifact: libcss_inline_php.dylib
35+
cross: true
36+
- os: macos-15
37+
target: x86_64-apple-darwin
38+
php: "8.4"
39+
artifact: libcss_inline_php.dylib
40+
cross: true
41+
- os: macos-15
42+
target: aarch64-apple-darwin
43+
php: "8.2"
44+
artifact: libcss_inline_php.dylib
45+
- os: macos-15
46+
target: aarch64-apple-darwin
47+
php: "8.3"
48+
artifact: libcss_inline_php.dylib
49+
- os: macos-15
50+
target: aarch64-apple-darwin
51+
php: "8.4"
52+
artifact: libcss_inline_php.dylib
53+
54+
runs-on: ${{ matrix.os }}
55+
56+
steps:
57+
- uses: actions/checkout@v6
58+
59+
- name: Set up PHP
60+
uses: shivammathur/setup-php@v2
61+
with:
62+
php-version: ${{ matrix.php }}
63+
64+
- name: Install Rust
65+
uses: dtolnay/rust-toolchain@stable
66+
with:
67+
targets: ${{ matrix.cross && matrix.target || '' }}
68+
69+
- uses: Swatinem/rust-cache@v2
70+
with:
71+
workspaces: bindings/php
72+
cache-all-crates: "true"
73+
74+
- name: Build
75+
run: |
76+
if [ "${{ matrix.cross }}" = "true" ]; then
77+
cargo build --release --target ${{ matrix.target }}
78+
else
79+
cargo build --release
80+
fi
81+
working-directory: bindings/php
82+
83+
- name: Rename artifact
84+
run: |
85+
mkdir -p dist
86+
if [ "${{ matrix.cross }}" = "true" ]; then
87+
cp bindings/php/target/${{ matrix.target }}/release/${{ matrix.artifact }} dist/css_inline-php${{ matrix.php }}-${{ matrix.target }}.so
88+
else
89+
cp bindings/php/target/release/${{ matrix.artifact }} dist/css_inline-php${{ matrix.php }}-${{ matrix.target }}.so
90+
fi
91+
92+
- name: Upload artifact
93+
uses: actions/upload-artifact@v6
94+
with:
95+
name: css_inline-php${{ matrix.php }}-${{ matrix.target }}
96+
path: dist/
97+
98+
release:
99+
needs: build
100+
runs-on: ubuntu-22.04
101+
permissions:
102+
contents: write
103+
104+
steps:
105+
- uses: actions/checkout@v6
106+
107+
- name: Extract Version
108+
run: echo "version=${GITHUB_REF#refs/tags/php-v}" >> $GITHUB_ENV
109+
110+
- name: Download all artifacts
111+
uses: actions/download-artifact@v7
112+
with:
113+
path: artifacts
114+
merge-multiple: true
115+
116+
- name: List artifacts
117+
run: ls -la artifacts/
118+
119+
- name: GitHub Release
120+
uses: softprops/action-gh-release@v2
121+
with:
122+
make_latest: false
123+
draft: true
124+
name: "[PHP] Release ${{ env.version }}"
125+
files: artifacts/*

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ into:
4141
- Optionally caches external stylesheets
4242
- Works on Linux, Windows, and macOS
4343
- Supports HTML5 & CSS3
44-
- Bindings for [Python](https://github.com/Stranger6667/css-inline/tree/master/bindings/python), [Ruby](https://github.com/Stranger6667/css-inline/tree/master/bindings/ruby), [JavaScript](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript), [Java](https://github.com/Stranger6667/css-inline/tree/master/bindings/java), [C](https://github.com/Stranger6667/css-inline/tree/master/bindings/c), and a [WebAssembly](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript/wasm) module to run in browsers.
44+
- Bindings for [Python](https://github.com/Stranger6667/css-inline/tree/master/bindings/python), [Ruby](https://github.com/Stranger6667/css-inline/tree/master/bindings/ruby), [JavaScript](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript), [Java](https://github.com/Stranger6667/css-inline/tree/master/bindings/java), [C](https://github.com/Stranger6667/css-inline/tree/master/bindings/c), [PHP](https://github.com/Stranger6667/css-inline/tree/master/bindings/php), and a [WebAssembly](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript/wasm) module to run in browsers.
4545
- Command Line Interface
4646

4747
## Playground

bindings/javascript/wasm/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/>
1010
<meta
1111
name="keywords"
12-
content="CSS inlining, WebAssembly, browser playground, Rust, Python, Ruby, JavaScript, Java, style attribute, HTML, CSS"
12+
content="CSS inlining, WebAssembly, browser playground, Rust, Python, Ruby, JavaScript, Java, PHP, style attribute, HTML, CSS"
1313
/>
1414
<title>CSS Inline | High-performance CSS inlining</title>
1515
<link
@@ -165,7 +165,7 @@ <h1 class="text-4xl font-semibold leading-6 text-gray-900">
165165
</p>
166166
<p class="mt-2 text-base leading-7 text-gray-900">
167167
css-inline uses components from Mozilla's Servo project and provides
168-
bindings for Rust, Python, Ruby, JavaScript, Java, and C. The
168+
bindings for Rust, Python, Ruby, JavaScript, Java, C, and PHP. The
169169
playground runs the library compiled to WebAssembly in the browser.
170170
Paste HTML with CSS into the text area and click "Inline" to process
171171
the output.

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
/composer.lock
3+
/.phpunit.cache/
4+
/.php-cs-fixer.cache
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__ . '/tests')
5+
->in(__DIR__ . '/stubs')
6+
->in(__DIR__ . '/benchmarks')
7+
->name('*.php');
8+
9+
return (new PhpCsFixer\Config())
10+
->setRiskyAllowed(true)
11+
->setRules([
12+
'@PER-CS' => true,
13+
'@PHP82Migration' => true,
14+
'array_syntax' => ['syntax' => 'short'],
15+
'declare_strict_types' => true,
16+
'no_unused_imports' => true,
17+
'ordered_imports' => ['sort_algorithm' => 'alpha'],
18+
'single_quote' => true,
19+
'trailing_comma_in_multiline' => true,
20+
])
21+
->setFinder($finder);

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 <dmitry@dygalo.dev>"]
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)