/** * Renders the given template file and returns the output. * * In case the template needs to be compiled first, triggers compilation and * writes the compiled template to a temporary file. * * Throws an exception in case an error occurred during rendering. * * @param string $sourceTemplateFile The file containing the source template to render * @param array $variables Variables which shall be available within the template during rendering * @param bool $sandboxMode If TRUE, only given variables will be available in the rendered template * @param \Ableron\Core\Template\TemplateRenderer The template renderer to use (if not set, a new template renderer will be instantiated for the job) * @throws \Ableron\Core\Exception\SystemException * @return string */ public function render($sourceTemplateFile, $variables = array(), $sandboxMode = false, $renderer = null) { // get file where to save the compiled version of the given template $compiledTemplateFile = $this->getCompiledTemplateFile($sourceTemplateFile); // compile template if necessary if (!$this->isCompiled($sourceTemplateFile, $compiledTemplateFile)) { // compile template $compiledTemplate = $this->getCompiler()->compile($sourceTemplateFile, file_get_contents($sourceTemplateFile)); // write compiled template to file if (!FileUtil::writeFile($compiledTemplateFile, $compiledTemplate)) { throw new SystemException(sprintf('Unable to write compiled template to file "%s"', $compiledTemplateFile), 0, E_USER_WARNING, __FILE__, __LINE__); } } // get renderer if ($renderer === null) { $renderer = new TemplateRenderer($this); } // render template and return result return $renderer->render($compiledTemplateFile, pathinfo($sourceTemplateFile, PATHINFO_DIRNAME), $sandboxMode ? $variables : array_merge($this->getVariables()->toArray(), $variables)); }
/** * Writes a string to a file in an atomic way. * * @param string $filePath Path to the file to write the string to * @param string $content The content to write to the file * @return bool TRUE if the content has been written to the file, FALSE otherwise */ protected function writeFile(string $filePath, string $content) { return FileUtil::writeFile($filePath, $content, 0666 & ~$this->umask); }
/** * Tests whether writeFile() returns FALSE on failure. * * @return void */ public function testWriteFileReturnsFalseOnFailure() { $this->assertFalse(FileUtil::writeFile('', 'foo')); }