Skip to content

Commit

Permalink
Provide ->renderAndWriteFile method to the Renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Feb 8, 2018
1 parent 33c423a commit 5f1f505
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/Phug/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ function ($path) {
);
}

/**
* Render a pug file and dump it into a file.
* Return true if the render and the writing succeeded.
*
* @param string $inputFile input file (Pug file)
* @param string $outputFile output file (typically the HTML/XML file)
* @param array $parameters local variables
*
* @return bool
*/
public function renderAndWriteFile($inputFile, $outputFile, $parameters)
{
$outputDirectory = dirname($outputFile);

if (!file_exists($outputDirectory) && !@mkdir($outputDirectory, 0777, true)) {
return false;
}

return is_int($this->getNewSandBox(function () use ($outputFile, $inputFile, $parameters) {
return file_put_contents($outputFile, $this->renderFile($inputFile, $parameters));
})->getResult());
}

/**
* Render all pug template files in an input directory and output in an other or the same directory.
* Return an array with success count and error count.
Expand Down Expand Up @@ -174,19 +197,11 @@ public function renderDirectory($path, $destination = null, $extension = '.html'
$relativeDirectory = trim(mb_substr(dirname($file), $length), '//\\');
$filename = pathinfo($file, PATHINFO_FILENAME);
$outputDirectory = $destination.DIRECTORY_SEPARATOR.$relativeDirectory;
$counter = null;
if (!file_exists($outputDirectory)) {
if (!@mkdir($outputDirectory, 0777, true)) {
$counter = 'errors';
}
}
if (!$counter) {
$outputFile = $outputDirectory.DIRECTORY_SEPARATOR.$filename.$extension;
$sandBox = $this->getNewSandBox(function () use ($outputFile, $file, $parameters) {
return file_put_contents($outputFile, $this->renderFile($file, $parameters));
});
$counter = $sandBox->getResult() ? 'success' : 'errors';
}
$counter = $this->renderAndWriteFile(
$file,
$outputDirectory.DIRECTORY_SEPARATOR.$filename.$extension,
$parameters
) ? 'success' : 'errors';
$$counter++;
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Phug/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function testRenderFile()
/**
* @covers \Phug\Renderer\Partial\FileSystemTrait::fileMatchExtensions
* @covers \Phug\Renderer\Partial\FileSystemTrait::scanDirectory
* @covers ::renderAndWriteFile
* @covers ::renderDirectory
*/
public function testRenderDirectory()
Expand Down

0 comments on commit 5f1f505

Please sign in to comment.