mode changes

This commit is contained in:
Dennis Eichhorn 2021-02-20 10:59:06 +01:00
parent 122d48d493
commit 333741d1ef
5 changed files with 67 additions and 8 deletions

View File

@ -30,9 +30,11 @@ interface RenderableInterface
/** /**
* Get the evaluated contents of the object. * Get the evaluated contents of the object.
* *
* @param mixed ...$data Data to pass to renderer
*
* @return string Returns rendered output * @return string Returns rendered output
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function render() : string; public function render(...$data) : string;
} }

View File

@ -106,7 +106,7 @@ final class HttpResponse extends ResponseAbstract implements RenderableInterface
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function render(bool $optimize = false) : string public function render(...$data) : string
{ {
$types = $this->header->get('Content-Type'); $types = $this->header->get('Content-Type');
@ -116,7 +116,7 @@ final class HttpResponse extends ResponseAbstract implements RenderableInterface
} }
} }
return $this->getRaw($optimize); return $this->getRaw($data[0]);
} }
/** /**
@ -196,4 +196,14 @@ final class HttpResponse extends ResponseAbstract implements RenderableInterface
return $result; return $result;
} }
public function endAllOutputBuffering() : void
{
$this->header->push();
$levels = \ob_get_level();
for ($i = 0; $i < $levels; ++$i) {
\ob_end_clean();
}
}
} }

View File

@ -142,11 +142,13 @@ final class Head implements RenderableInterface
/** /**
* Get the evaluated contents of the object. * Get the evaluated contents of the object.
* *
* @param mixed ...$data Data to pass to renderer
*
* @return string * @return string
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function render() : string public function render(...$data) : string
{ {
$head = ''; $head = '';
$head .= $this->meta->render(); $head .= $this->meta->render();

View File

@ -227,7 +227,7 @@ final class Meta implements RenderableInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function render() : string public function render(...$data) : string
{ {
return (\count($this->keywords) > 0 ? '<meta name="keywords" content="' . ViewAbstract::html(\implode(',', $this->keywords)) . '">' : '') return (\count($this->keywords) > 0 ? '<meta name="keywords" content="' . ViewAbstract::html(\implode(',', $this->keywords)) . '">' : '')
. (!empty($this->author) ? '<meta name="author" content="' . ViewAbstract::html($this->author) . '">' : '') . (!empty($this->author) ? '<meta name="author" content="' . ViewAbstract::html($this->author) . '">' : '')

View File

@ -35,6 +35,14 @@ abstract class ViewAbstract implements RenderableInterface
*/ */
private const BASE_PATH = __DIR__ . '/../..'; private const BASE_PATH = __DIR__ . '/../..';
/**
* Output is buffered
*
* @var bool
* @since 1.0.0
*/
public bool $isBuffered = true;
/** /**
* Template. * Template.
* *
@ -230,7 +238,9 @@ abstract class ViewAbstract implements RenderableInterface
$ob = ''; $ob = '';
try { try {
\ob_start(); if ($this->isBuffered) {
\ob_start();
}
$path = $this->template; $path = $this->template;
if (!\is_file($path)) { if (!\is_file($path)) {
@ -239,13 +249,48 @@ abstract class ViewAbstract implements RenderableInterface
/** @noinspection PhpIncludeInspection */ /** @noinspection PhpIncludeInspection */
$includeData = include $path; $includeData = include $path;
$ob = (string) \ob_get_clean();
if ($this->isBuffered) {
$ob = (string) \ob_get_clean();
}
if (\is_array($includeData)) { if (\is_array($includeData)) {
$ob = (string) \json_encode($includeData); $ob = (string) \json_encode($includeData);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
\ob_end_clean(); if ($this->isBuffered) {
\ob_end_clean();
}
$ob = '';
} finally {
return $ob;
}
}
/**
* Very similar to render, except that it executes the template file and returns its response as is.
* This allows to build the template as any datatype (e.g. pdf).
*
* @param mixed ...$data Data to pass to build
*
* @return mixed
*
* @since 1.0.0
*/
public function build(...$data) : mixed
{
$ob = '';
try {
$path = $this->template;
if (!\is_file($path)) {
throw new PathException($path);
}
/** @noinspection PhpIncludeInspection */
$ob = include $path;
} catch (\Throwable $e) {
$ob = ''; $ob = '';
} finally { } finally {
return $ob; return $ob;