1
- use std:: fmt:: Display ;
1
+ use std:: { fmt:: Display , num :: NonZeroUsize , sync :: Mutex } ;
2
2
3
3
use ext_php_rs:: { exception:: PhpException , prelude:: * , zend:: ce} ;
4
4
@@ -12,6 +12,22 @@ fn from_error<E: Display>(error: E) -> PhpException {
12
12
PhpException :: from_class :: < InlineError > ( error. to_string ( ) )
13
13
}
14
14
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
+
15
31
#[ php_class]
16
32
#[ php( name = "CssInline\\ CssInliner" ) ]
17
33
pub struct CssInliner {
@@ -25,6 +41,10 @@ impl CssInliner {
25
41
keep_style_tags = false ,
26
42
keep_link_tags = false ,
27
43
load_remote_stylesheets = true ,
44
+ base_url = None ,
45
+ extra_css = None ,
46
+ preallocate_node_capacity = 32_usize ,
47
+ cache = None ,
28
48
) ) ]
29
49
#[ php( optional = inline_style_tags) ]
30
50
pub fn __construct (
@@ -34,20 +54,30 @@ impl CssInliner {
34
54
load_remote_stylesheets : bool ,
35
55
base_url : Option < String > ,
36
56
extra_css : Option < String > ,
57
+ preallocate_node_capacity : usize ,
58
+ cache : Option < & StylesheetCache > ,
37
59
) -> PhpResult < CssInliner > {
38
60
let base_url = if let Some ( url) = base_url {
39
61
Some ( css_inline:: Url :: parse ( & url) . map_err ( from_error) ?)
40
62
} else {
41
63
None
42
64
} ;
43
65
66
+ let cache = if let Some ( cache) = cache {
67
+ Some ( Mutex :: new ( css_inline:: StylesheetCache :: new ( cache. size ) ) )
68
+ } else {
69
+ None
70
+ } ;
71
+
44
72
let options = css_inline:: InlineOptions {
45
73
inline_style_tags,
46
74
keep_style_tags,
47
75
keep_link_tags,
48
76
base_url,
49
77
load_remote_stylesheets,
50
78
extra_css : extra_css. map ( Into :: into) ,
79
+ preallocate_node_capacity,
80
+ cache,
51
81
..Default :: default ( )
52
82
} ;
53
83
@@ -81,6 +111,7 @@ pub fn inline_fragment(fragment: &str, css: &str) -> PhpResult<String> {
81
111
pub fn get_module ( module : ModuleBuilder ) -> ModuleBuilder {
82
112
module
83
113
. class :: < InlineError > ( )
114
+ . class :: < StylesheetCache > ( )
84
115
. class :: < CssInliner > ( )
85
116
. function ( wrap_function ! ( inline) )
86
117
. function ( wrap_function ! ( inline_fragment) )
0 commit comments