2
2
3
3
namespace CSSFromHTMLExtractor ;
4
4
5
+ use Doctrine \Common \Cache \ArrayCache ;
6
+ use Doctrine \Common \Cache \Cache ;
5
7
use DOMNodeList ;
6
8
use Exception ;
7
9
use Symfony \Component \CssSelector \CssSelectorConverter ;
@@ -28,10 +30,14 @@ class CssFromHTMLExtractor
28
30
/** @var HtmlStore */
29
31
private $ htmlStore ;
30
32
33
+ /** @var Cache */
34
+ private $ resultCache ;
35
+
31
36
/**
32
37
* CssFromHTMLExtractor constructor.
38
+ * @param Cache|null $resultCache
33
39
*/
34
- public function __construct ()
40
+ public function __construct (Cache $ resultCache = null )
35
41
{
36
42
if (class_exists ('Symfony\Component\CssSelector\CssSelectorConverter ' )) {
37
43
$ this ->cssConverter = new CssSelectorConverter ();
@@ -41,6 +47,8 @@ public function __construct()
41
47
$ this ->htmlStore = new HtmlStore ();
42
48
$ this ->processor = new Processor ();
43
49
$ this ->cssConverter = new CssSelectorConverter ();
50
+
51
+ $ this ->resultCache = is_null ($ resultCache ) ? new ArrayCache () : $ resultCache ;
44
52
}
45
53
46
54
public function getCssStore ()
@@ -58,8 +66,21 @@ public function getHtmlStore()
58
66
*/
59
67
public function addBaseRules ($ sourceCss )
60
68
{
61
- $ this ->rules = $ this ->processor ->getRules ($ sourceCss , $ this ->rules );
62
- $ this ->getCssStore ()->setCharset ($ this ->processor ->getCharset ($ sourceCss ));
69
+ $ identifier = md5 ($ sourceCss );
70
+ if ($ this ->resultCache ->contains ($ identifier )) {
71
+ list ($ rules , $ charset ) = $ this ->resultCache ->fetch ($ identifier );
72
+ $ this ->rules = $ rules ;
73
+ $ this ->getCssStore ()->setCharset ($ charset );
74
+
75
+ return ;
76
+ }
77
+
78
+ $ results = [$ this ->processor ->getRules ($ sourceCss , $ this ->rules ), $ this ->processor ->getCharset ($ sourceCss )];
79
+
80
+ $ this ->rules = $ results [0 ];
81
+ $ this ->getCssStore ()->setCharset ($ results [1 ]);
82
+
83
+ $ this ->resultCache ->save ($ identifier , $ results );
63
84
}
64
85
65
86
public function buildExtractedRuleSet ()
0 commit comments