|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
namespace TYPO3Fluid\Fluid\Core\Parser; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file belongs to the package "TYPO3 Fluid". |
|
7
|
|
|
* See LICENSE.txt that was shipped with this package. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
use TYPO3Fluid\Fluid\Component\ComponentInterface; |
|
11
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Template parser building up an object syntax tree |
|
15
|
|
|
*/ |
|
16
|
|
|
class TemplateParser |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Configuration |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $configuration; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var RenderingContextInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $renderingContext; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(RenderingContextInterface $renderingContext) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->renderingContext = $renderingContext; |
|
31
|
|
|
$this->configuration = $renderingContext->getParserConfiguration(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function parseFile(string $templatePathAndFilename, ?Configuration $configuration = null): ComponentInterface |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
return $this->getOrParseAndStoreTemplate( |
|
37
|
|
|
$this->createIdentifierForFile($templatePathAndFilename, ''), |
|
38
|
|
|
function () use ($templatePathAndFilename): string { |
|
39
|
|
|
return file_get_contents($templatePathAndFilename); |
|
40
|
|
|
} |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Parses a given template string and returns a parsed template object. |
|
46
|
|
|
* |
|
47
|
|
|
* The resulting ParsedTemplate can then be rendered by calling evaluate() on it. |
|
48
|
|
|
* |
|
49
|
|
|
* Normally, you should use a subclass of AbstractTemplateView instead of calling the |
|
50
|
|
|
* TemplateParser directly. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $templateString The template to parse as a string |
|
53
|
|
|
* @param Configuration|null Template parsing configuration to use |
|
54
|
|
|
* @return ComponentInterface Parsed template |
|
55
|
|
|
* @throws Exception |
|
56
|
|
|
*/ |
|
57
|
|
|
public function parse(string $templateString, ?Configuration $configuration = null): ComponentInterface |
|
58
|
|
|
{ |
|
59
|
|
|
$source = new Source($templateString); |
|
60
|
|
|
$contexts = new Contexts(); |
|
61
|
|
|
$sequencer = new Sequencer( |
|
62
|
|
|
$this->renderingContext, |
|
63
|
|
|
$contexts, |
|
64
|
|
|
$source, |
|
65
|
|
|
$configuration ?? $this->configuration |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
return $sequencer->sequence(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $templateIdentifier |
|
73
|
|
|
* @param \Closure $templateSourceClosure Closure which returns the template source if needed |
|
74
|
|
|
* @return ComponentInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getOrParseAndStoreTemplate(string $templateIdentifier, \Closure $templateSourceClosure): ComponentInterface |
|
77
|
|
|
{ |
|
78
|
|
|
if (!$this->configuration->isFeatureEnabled(Configuration::FEATURE_RUNTIME_CACHE)) { |
|
79
|
|
|
return $this->parseTemplateSource($templateIdentifier, $templateSourceClosure); |
|
80
|
|
|
} |
|
81
|
|
|
static $cache = []; |
|
82
|
|
|
if (!isset($cache[$templateIdentifier])) { |
|
83
|
|
|
$cache[$templateIdentifier] = $this->parseTemplateSource($templateIdentifier, $templateSourceClosure); |
|
84
|
|
|
} |
|
85
|
|
|
return $cache[$templateIdentifier]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $templateIdentifier |
|
90
|
|
|
* @param \Closure $templateSourceClosure |
|
91
|
|
|
* @return ComponentInterface |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function parseTemplateSource(string $templateIdentifier, \Closure $templateSourceClosure): ComponentInterface |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$parsedTemplate = $this->parse( |
|
96
|
|
|
$templateSourceClosure($this->renderingContext), |
|
97
|
|
|
$this->renderingContext->getParserConfiguration() |
|
98
|
|
|
); |
|
99
|
|
|
//$parsedTemplate->setIdentifier($templateIdentifier); |
|
100
|
|
|
return $parsedTemplate; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns a unique identifier for the given file in the format |
|
105
|
|
|
* <PackageKey>_<SubPackageKey>_<ControllerName>_<prefix>_<SHA1> |
|
106
|
|
|
* The SH1 hash is a checksum that is based on the file path and last modification date |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $pathAndFilename |
|
109
|
|
|
* @param string $prefix |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function createIdentifierForFile(string $pathAndFilename, string $prefix): string |
|
113
|
|
|
{ |
|
114
|
|
|
return sprintf('%s_%s', $prefix, sha1($pathAndFilename)); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.