Skip to content

Commit d5c1b4b

Browse files
committed
wip
Signed-off-by: Dmitry Dygalo <[email protected]>
1 parent 46814f6 commit d5c1b4b

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

bindings/php/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ name = "css_inline_php"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
ext-php-rs = "0.14"
12+
ext-php-rs = "0.14.1"
1313

1414
[dependencies.css-inline]
1515
path = "../../css-inline"

bindings/php/src/lib.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::Display;
1+
use std::{fmt::Display, num::NonZeroUsize, sync::Mutex};
22

33
use ext_php_rs::{exception::PhpException, prelude::*, zend::ce};
44

@@ -12,6 +12,22 @@ fn from_error<E: Display>(error: E) -> PhpException {
1212
PhpException::from_class::<InlineError>(error.to_string())
1313
}
1414

15+
#[php_class]
16+
#[php(name = "CssInline\\StylesheetCache")]
17+
pub struct StylesheetCache {
18+
size: NonZeroUsize,
19+
}
20+
21+
#[php_impl]
22+
impl StylesheetCache {
23+
pub fn __construct(size: usize) -> PhpResult<StylesheetCache> {
24+
let size = NonZeroUsize::new(size).ok_or_else(|| {
25+
PhpException::default("Cache size must be an integer greater than zero".to_string())
26+
})?;
27+
Ok(StylesheetCache { size })
28+
}
29+
}
30+
1531
#[php_class]
1632
#[php(name = "CssInline\\CssInliner")]
1733
pub struct CssInliner {
@@ -25,6 +41,10 @@ impl CssInliner {
2541
keep_style_tags = false,
2642
keep_link_tags = false,
2743
load_remote_stylesheets = true,
44+
base_url = None,
45+
extra_css = None,
46+
preallocate_node_capacity = 32_usize,
47+
cache = None,
2848
))]
2949
#[php(optional = inline_style_tags)]
3050
pub fn __construct(
@@ -34,20 +54,30 @@ impl CssInliner {
3454
load_remote_stylesheets: bool,
3555
base_url: Option<String>,
3656
extra_css: Option<String>,
57+
preallocate_node_capacity: usize,
58+
cache: Option<&StylesheetCache>,
3759
) -> PhpResult<CssInliner> {
3860
let base_url = if let Some(url) = base_url {
3961
Some(css_inline::Url::parse(&url).map_err(from_error)?)
4062
} else {
4163
None
4264
};
4365

66+
let cache = if let Some(cache) = cache {
67+
Some(Mutex::new(css_inline::StylesheetCache::new(cache.size)))
68+
} else {
69+
None
70+
};
71+
4472
let options = css_inline::InlineOptions {
4573
inline_style_tags,
4674
keep_style_tags,
4775
keep_link_tags,
4876
base_url,
4977
load_remote_stylesheets,
5078
extra_css: extra_css.map(Into::into),
79+
preallocate_node_capacity,
80+
cache,
5181
..Default::default()
5282
};
5383

@@ -81,6 +111,7 @@ pub fn inline_fragment(fragment: &str, css: &str) -> PhpResult<String> {
81111
pub fn get_module(module: ModuleBuilder) -> ModuleBuilder {
82112
module
83113
.class::<InlineError>()
114+
.class::<StylesheetCache>()
84115
.class::<CssInliner>()
85116
.function(wrap_function!(inline))
86117
.function(wrap_function!(inline_fragment))

bindings/php/tests/CssInlineTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public function testInlineFragment(): void
2626
public function testCSSInlinerWithExtraCss(): void
2727
{
2828
$inliner = new CSSInliner(
29-
base_url: null,
3029
extra_css: 'p { color: green; }'
3130
);
3231

0 commit comments

Comments
 (0)