109 if ( isset( $this->renderers[$templateKey] ) &&
110 is_callable( $this->renderers[$templateKey] )
112 return $this->renderers[$templateKey];
117 $secretKey = MediaWikiServices::hasInstance()
118 ? MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::SecretKey )
123 $key = $this->cache->makeKey(
124 'lightncandy-compiled',
130 $compiledTemplate = $this->cache->get( $key );
134 if ( $compiledTemplate ) {
135 $filesHash = FileContentsHasher::getFileContentsHash( $compiledTemplate[
'files'] );
137 if ( $filesHash !== $compiledTemplate[
'filesHash'] ) {
138 $compiledTemplate =
null;
144 if ( $compiledTemplate ) {
145 $integrityHash = hash_hmac(
'sha256', $compiledTemplate[
'phpCode'], $secretKey );
147 if ( $integrityHash !== $compiledTemplate[
'integrityHash'] ) {
148 $compiledTemplate =
null;
154 if ( !$compiledTemplate ) {
155 $compiledTemplate = $this->
compile( $templateName );
157 $compiledTemplate[
'integrityHash'] = hash_hmac(
159 $compiledTemplate[
'phpCode'],
163 $this->cache->set( $key, $compiledTemplate, self::CACHE_TTL );
168 $compiledTemplate = $this->
compile( $templateName );
171 $renderer = eval( $compiledTemplate[
'phpCode'] );
172 if ( !is_callable( $renderer ) ) {
173 throw new RuntimeException(
"Compiled template `{$templateName}` is not callable" );
175 $this->renderers[$templateKey] = $renderer;
213 if ( !file_exists( $filename ) ) {
214 throw new RuntimeException(
"Could not find template `{$templateName}` at {$filename}" );
217 $files = [ $filename ];
218 $contents = file_get_contents( $filename );
219 $compiled = LightnCandy::compile(
222 'flags' => $this->compileFlags,
223 'basedir' => $this->templateDir,
224 'fileext' =>
'.mustache',
225 'partialresolver' =>
function ( $cx, $partialName ) use ( $templateName, &$files ) {
226 $filename =
"{$this->templateDir}/{$partialName}.mustache";
227 if ( !file_exists( $filename ) ) {
228 throw new RuntimeException( sprintf(
229 'Could not compile template `%s`: Could not find partial `%s` at %s',
236 $fileContents = file_get_contents( $filename );
238 if ( $fileContents ===
false ) {
239 throw new RuntimeException( sprintf(
240 'Could not compile template `%s`: Could not find partial `%s` at %s',
247 $files[] = $filename;
249 return $fileContents;
257 throw new RuntimeException(
"Could not compile template `{$filename}`" );
260 $files = array_values( array_unique( $files ) );
263 'phpCode' => $compiled,
265 'filesHash' => FileContentsHasher::getFileContentsHash( $files ),