Write compiled templates atomically
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 8 Sep 2014 16:06:16 +0000 (18:06 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 8 Sep 2014 16:27:31 +0000 (18:27 +0200)
wcfsetup/install/files/lib/system/template/TemplateCompiler.class.php

index 7c0cdfd208e900b0b096357d85464a2dd3c205bf..7bb759c6e26e5a10314713990d637736ca9d6d42 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 namespace wcf\system\template;
-use wcf\system\io\File;
+use wcf\system\io\AtomicWriter;
 
 /**
  * Compiles template source into valid PHP code.
@@ -22,19 +22,19 @@ class TemplateCompiler extends TemplateScriptingCompiler {
         * @param       array           $metaData
         */
        public function compile($templateName, $sourceContent, $compiledFilename, $metaData) {
+               $writer = new AtomicWriter($compiledFilename);
                // build fileheader for template
-               $compiledHeader = "<?php\n/**\n * WoltLab Community Framework\n * Template: ".$templateName."\n * Compiled at: ".gmdate('r')."\n * \n * DO NOT EDIT THIS FILE\n */\n\$this->v['tpl']['template'] = '".addcslashes($templateName, "'\\")."';\n?>\n";
+               $writer->write("<?php\n/**\n * WoltLab Community Framework\n * Template: ".$templateName."\n * Compiled at: ".gmdate('r')."\n * \n * DO NOT EDIT THIS FILE\n */\n\$this->v['tpl']['template'] = '".addcslashes($templateName, "'\\")."';\n?>\n");
                
                // include plug-ins
                $compiledContent = $this->compileString($templateName, $sourceContent, $metaData);
-               
-               // write compiled template to file
-               $file = new File($compiledFilename);
-               $file->write($compiledHeader.$compiledContent['template']);
-               $file->close();
+               $writer->write($compiledContent['template']);
                
                // write meta data to file
                $this->saveMetaData($templateName, $metaData['filename'], $compiledContent['meta']);
+               
+               $writer->flush();
+               $writer->close();
        }
        
        /**
@@ -45,10 +45,11 @@ class TemplateCompiler extends TemplateScriptingCompiler {
         * @param       string          $content
         */
        public function saveMetaData($templateName, $filename, $content) {
-               $file = new File($filename);
-               $file->write("<?php exit; /* meta data for template: ".$templateName." (generated at ".gmdate('r').") DO NOT EDIT THIS FILE */ ?>\n");
-               $file->write(serialize($content));
-               $file->close();
+               $writer = new AtomicWriter($filename);
+               $writer->write("<?php exit; /* meta data for template: ".$templateName." (generated at ".gmdate('r').") DO NOT EDIT THIS FILE */ ?>\n");
+               $writer->write(serialize($content));
+               $writer->flush();
+               $writer->close();
        }
        
        /**