30 protected $templateDir;
40 protected $forceRecompile =
false;
45 protected $compileFlags;
51 public function __construct( $templateDir =
null, $forceRecompile =
false ) {
52 $this->templateDir = $templateDir ?: __DIR__ .
'/templates';
53 $this->forceRecompile = $forceRecompile;
57 $this->compileFlags = LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_MUSTACHELOOKUP;
64 public function enableRecursivePartials( $enable ) {
66 $this->compileFlags |= LightnCandy::FLAG_RUNTIMEPARTIAL;
68 $this->compileFlags &= ~LightnCandy::FLAG_RUNTIMEPARTIAL;
78 protected function getTemplateFilename( $templateName ) {
83 strcspn( $templateName,
":/\\\000&<>'\"%" ) !== strlen( $templateName )
85 throw new UnexpectedValueException(
"Malformed \$templateName: $templateName" );
88 return "{$this->templateDir}/{$templateName}.mustache";
97 protected function getTemplate( $templateName ) {
98 $templateKey = $templateName .
'|' . $this->compileFlags;
101 if ( isset( $this->renderers[$templateKey] ) &&
102 is_callable( $this->renderers[$templateKey] )
104 return $this->renderers[$templateKey];
107 $filename = $this->getTemplateFilename( $templateName );
109 if ( !file_exists( $filename ) ) {
110 throw new RuntimeException(
"Could not locate template: {$filename}" );
114 $fileContents = file_get_contents( $filename );
117 $fastHash = md5( $this->compileFlags .
'|' . $fileContents );
120 $config = MediaWikiServices::getInstance()->getMainConfig();
121 $secretKey = $config->get(
'SecretKey' );
126 $key =
$cache->makeKey(
'template', $templateName, $fastHash );
127 $code = $this->forceRecompile ? null :
$cache->get( $key );
131 $keyedHash = substr( $code, 0, 64 );
132 $code = substr( $code, 64 );
133 if ( $keyedHash !== hash_hmac(
'sha256', $code, $secretKey ) ) {
140 $code = $this->compileForEval( $fileContents, $filename );
143 $cache->set( $key, hash_hmac(
'sha256', $code, $secretKey ) . $code );
147 $code = $this->compileForEval( $fileContents, $filename );
150 $renderer = eval( $code );
151 if ( !is_callable( $renderer ) ) {
152 throw new RuntimeException(
"Requested template, {$templateName}, is not callable" );
154 $this->renderers[$templateKey] = $renderer;
166 protected function compileForEval( $fileContents, $filename ) {
168 $code = $this->compile( $fileContents );
171 throw new RuntimeException(
"Could not compile template: {$filename}" );
175 if ( substr( $code, 0, 5 ) ===
'<?php' ) {
176 $code = substr( $code, 5 );
188 protected function compile( $code ) {
189 if ( !class_exists(
'LightnCandy' ) ) {
190 throw new RuntimeException(
'LightnCandy class not defined' );
192 return LightnCandy::compile(
195 'flags' => $this->compileFlags,
196 'basedir' => $this->templateDir,
197 'fileext' =>
'.mustache',
222 public function processTemplate( $templateName,
$args, array $scopes = [] ) {
223 $template = $this->getTemplate( $templateName );
224 return $template(
$args, $scopes );
static getLocalServerInstance( $fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)