-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca220db
commit 9fcd6fe
Showing
77 changed files
with
1,080 additions
and
493 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace DanBettles\Marigold\OutputHelper; | ||
|
||
// @todo Remove this. | ||
interface OutputHelperInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanBettles\Marigold; | ||
|
||
use DanBettles\Marigold\Exception\FileNotFoundException; | ||
|
||
use function extract; | ||
use function is_file; | ||
use function ob_end_clean; | ||
use function ob_get_contents; | ||
use function ob_start; | ||
|
||
use const null; | ||
|
||
class Php | ||
{ | ||
/** | ||
* @param array<string, mixed> $context | ||
* @return mixed | ||
* @throws FileNotFoundException If the PHP file does not exist. | ||
*/ | ||
public function executeFile( | ||
string $pathname, | ||
array $context = [], | ||
?string &$output = null | ||
) { | ||
if (!is_file($pathname)) { | ||
throw new FileNotFoundException($pathname); | ||
} | ||
|
||
return (static function ( | ||
string $__FILE__, | ||
array $context, | ||
&$__OUTPUT__ | ||
) { | ||
ob_start(); | ||
|
||
try { | ||
extract($context); | ||
unset($context); | ||
|
||
$__RESPONSE__ = require $__FILE__; | ||
|
||
$__OUTPUT__ = ob_get_contents(); | ||
|
||
return $__RESPONSE__; | ||
} finally { | ||
ob_end_clean(); | ||
} | ||
})($pathname, $context, $output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DanBettles\Marigold\TemplateEngine; | ||
|
||
use DanBettles\Marigold\Exception\FileNotFoundException; | ||
use DanBettles\Marigold\Php; | ||
use SplFileInfo; | ||
|
||
use function array_replace; | ||
|
||
use const null; | ||
|
||
class Engine | ||
{ | ||
private Php $php; | ||
|
||
private TemplateFileLoader $templateFileLoader; | ||
|
||
public function __construct( | ||
Php $php, | ||
TemplateFileLoader $templateFileLoader | ||
) { | ||
$this | ||
->setPhp($php) | ||
->setTemplateFileLoader($templateFileLoader) | ||
; | ||
} | ||
|
||
/** | ||
* Inside the template being rendered, variables can be found in the `$input` collection. The value of a variable | ||
* called "foo" could be accessed with `$input['foo']`, for example. | ||
* | ||
* The `$output` object provides: | ||
* - `include()`, which allows to include other files; | ||
* - and `insertInto()`/`wrapWith()`, which causes the output of the template to be inserted into another template. | ||
* | ||
* @param string|SplFileInfo $pathnameOrFileInfo | ||
* @param array<string, mixed> $variables | ||
* @throws FileNotFoundException If the template file could not be found. | ||
*/ | ||
public function render( | ||
$pathnameOrFileInfo, | ||
array $variables = [] | ||
): string { | ||
$templateFile = $this->getTemplateFileLoader()->findTemplate($pathnameOrFileInfo); | ||
|
||
if (null === $templateFile) { | ||
throw new FileNotFoundException((string) $pathnameOrFileInfo); | ||
} | ||
|
||
$output = new OutputFacade($this); | ||
|
||
$renderedOutput = null; | ||
|
||
$this->getPhp()->executeFile( | ||
$templateFile->getPathname(), | ||
[ | ||
'input' => $variables, | ||
'output' => $output, | ||
], | ||
$renderedOutput | ||
); | ||
|
||
if ($output->getWrapperArgs()) { | ||
// @phpstan-ignore-next-line | ||
list( | ||
$wrapperPathnameOrTemplateFile, | ||
$wrapperTargetVarName, | ||
$wrapperVariables | ||
) = $output->getWrapperArgs(); | ||
|
||
/** @var array<string, mixed> */ | ||
$wrapperVariables = array_replace($wrapperVariables, [ | ||
($wrapperTargetVarName) => $renderedOutput, | ||
]); | ||
|
||
return (new self($this->getPhp(), $this->getTemplateFileLoader())) | ||
->render($wrapperPathnameOrTemplateFile, $wrapperVariables) | ||
; | ||
} | ||
|
||
return $renderedOutput; | ||
} | ||
|
||
private function setPhp(Php $php): self | ||
{ | ||
$this->php = $php; | ||
return $this; | ||
} | ||
|
||
public function getPhp(): Php | ||
{ | ||
return $this->php; | ||
} | ||
|
||
private function setTemplateFileLoader(TemplateFileLoader $loader): self | ||
{ | ||
$this->templateFileLoader = $loader; | ||
return $this; | ||
} | ||
|
||
public function getTemplateFileLoader(): TemplateFileLoader | ||
{ | ||
return $this->templateFileLoader; | ||
} | ||
|
||
/** | ||
* Factory method, for convenience. | ||
*/ | ||
public static function create(TemplateFileLoader $loader): self | ||
{ | ||
return new self(new Php(), $loader); | ||
} | ||
} |
Oops, something went wrong.