1- use std:: fmt:: Display ;
1+ use std:: { fmt:: Display , num :: NonZeroUsize , sync :: Mutex } ;
22
33use 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" ) ]
1733pub 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> {
81111pub 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) )
0 commit comments