Skip to content

Commit 4191ab5

Browse files
committed
Implemented cachable baserules, so they won't be reparsed every request
1 parent 1c46fdb commit 4191ab5

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"require": {
1515
"php": "^5.5 || ^7",
1616
"symfony/css-selector": "^2.7|~3.0",
17-
"twig/twig": "^1.0|^2.0"
17+
"twig/twig": "^1.0|^2.0",
18+
"doctrine/cache": "^1.7"
1819
},
1920
"autoload": {
2021
"psr-4": {

src/CssFromHTMLExtractor.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CSSFromHTMLExtractor;
44

5+
use Doctrine\Common\Cache\ArrayCache;
6+
use Doctrine\Common\Cache\Cache;
57
use DOMNodeList;
68
use Exception;
79
use Symfony\Component\CssSelector\CssSelectorConverter;
@@ -28,10 +30,14 @@ class CssFromHTMLExtractor
2830
/** @var HtmlStore */
2931
private $htmlStore;
3032

33+
/** @var Cache */
34+
private $resultCache;
35+
3136
/**
3237
* CssFromHTMLExtractor constructor.
38+
* @param Cache|null $resultCache
3339
*/
34-
public function __construct()
40+
public function __construct(Cache $resultCache = null)
3541
{
3642
if (class_exists('Symfony\Component\CssSelector\CssSelectorConverter')) {
3743
$this->cssConverter = new CssSelectorConverter();
@@ -41,6 +47,8 @@ public function __construct()
4147
$this->htmlStore = new HtmlStore();
4248
$this->processor = new Processor();
4349
$this->cssConverter = new CssSelectorConverter();
50+
51+
$this->resultCache = is_null($resultCache) ? new ArrayCache() : $resultCache;
4452
}
4553

4654
public function getCssStore()
@@ -58,8 +66,21 @@ public function getHtmlStore()
5866
*/
5967
public function addBaseRules($sourceCss)
6068
{
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);
6384
}
6485

6586
public function buildExtractedRuleSet()

src/Twig/Extension.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CSSFromHTMLExtractor\CssFromHTMLExtractor;
66
use CSSFromHTMLExtractor\Twig\TokenParsers\FoldTokenParser;
7+
use Doctrine\Common\Cache\Cache;
78
use Twig_Extension;
89
use Twig_ExtensionInterface;
910

@@ -14,11 +15,11 @@ class Extension extends Twig_Extension implements Twig_ExtensionInterface
1415
private $pageSpecificCssService;
1516

1617
/**
17-
* Extension constructor.
18+
* @param Cache|null $resultSetCache
1819
*/
19-
public function __construct()
20+
public function __construct(Cache $resultSetCache = null)
2021
{
21-
$this->pageSpecificCssService = new CssFromHTMLExtractor();
22+
$this->pageSpecificCssService = new CssFromHTMLExtractor($resultSetCache);
2223
}
2324

2425
/**

0 commit comments

Comments
 (0)