8000 [CssSelector] Added cache on top of CssSelectorConverter · symfony/symfony@4031876 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4031876

Browse files
committed
[CssSelector] Added cache on top of CssSelectorConverter
1 parent 07818f2 commit 4031876

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/Symfony/Component/CssSelector/CssSelectorConverter.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@
2727
class CssSelectorConverter
2828
{
2929
private $translator;
30+
private $html;
31+
32+
private static $xmlCache = [];
33+
private static $htmlCache = [];
3034

3135
/**
3236
* @param bool $html Whether HTML support should be enabled. Disable it for XML documents
3337
*/
3438
public function __construct(bool $html = true)
3539
{
3640
$this->translator = new Translator();
41+
$this->html = $html;
3742

38-
if ($html) {
43+
if ($this->html) {
3944
$this->translator->registerExtension(new HtmlExtension($this->translator));
4045
}
4146

@@ -57,6 +62,18 @@ public function __construct(bool $html = true)
5762
*/
5863
public function toXPath(string $cssExpr, string $prefix = 'descendant-or-self::')
5964
{
60-
return $this->translator->cssToXPath($cssExpr, $prefix);
65+
if ($this->html) {
66+
if (self::$htmlCache[$prefix][$cssExpr] ?? false) {
67+
return self::$htmlCache[$prefix][$cssExpr];
68+
}
69+
70+
return self::$htmlCache[$prefix][$cssExpr] = $this->translator->cssToXPath($cssExpr, $prefix);
71+
}
72 65F0 +
73+
if (self::$xmlCache[$prefix][$cssExpr] ?? false) {
74+
return self::$xmlCache[$prefix][$cssExpr];
75+
}
76+
77+
return self::$xmlCache[$prefix][$cssExpr] = $this->translator->cssToXPath($cssExpr, $prefix);
6178
}
6279
}

0 commit comments

Comments
 (0)
0