200 lines
3.7 KiB
PHP
200 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Application\Controller;
|
|
|
|
use Application\Breadcrumbs\Breadcrumbs;
|
|
use Application\Helper\ArrayPath;
|
|
use Exception;
|
|
use Twig\Environment;
|
|
use Twig\Error\LoaderError;
|
|
use Twig\Error\RuntimeError;
|
|
use Twig\Error\SyntaxError;
|
|
use Twig\Loader\FilesystemLoader;
|
|
|
|
abstract class MainController
|
|
{
|
|
|
|
/**
|
|
* Path to twig template folder.
|
|
*/
|
|
public const DIR_TEMPLATES = __DIR__ . '/../../../templates';
|
|
|
|
/**
|
|
* Path to cache folder.
|
|
*/
|
|
public const DIR_CACHE = __DIR__ . '/../../../cache';
|
|
|
|
/**
|
|
* Array key for global stash scope.
|
|
*/
|
|
private const STASH_GLOBAL = 'global';
|
|
|
|
/**
|
|
* Array key for local stash scope.
|
|
*/
|
|
private const STASH_LOCAL = 'local';
|
|
|
|
/**
|
|
* Stash. This array will be sent to twig for rendering.
|
|
*
|
|
* @var array
|
|
*/
|
|
private array $stash = array();
|
|
|
|
/**
|
|
* URL parameters obtained by routing.
|
|
*
|
|
* @var array
|
|
*/
|
|
private array $route_parameters = array();
|
|
|
|
/**
|
|
* Breadcrumbs.
|
|
*
|
|
* @var Breadcrumbs
|
|
*/
|
|
private Breadcrumbs $breadcrumbs;
|
|
|
|
/**
|
|
* @throws RuntimeError
|
|
* @throws SyntaxError
|
|
* @throws LoaderError
|
|
* @throws Exception
|
|
*/
|
|
public function __invoke(array $parameters = array()): void
|
|
{
|
|
$this->route_parameters = $parameters;
|
|
$this->preAction();
|
|
$this->action();
|
|
$this->postAction();
|
|
$this->render();
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
protected function getRouteParams(string|array $params)
|
|
{
|
|
return ArrayPath::GetDataFromArray($this->route_parameters, $params);
|
|
}
|
|
|
|
protected abstract function action(): void;
|
|
|
|
protected abstract function getTemplate(): string;
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
private function preAction(): void
|
|
{
|
|
$this->headerData();
|
|
$this->menuData();
|
|
$this->breadcrumbs = new Breadcrumbs();
|
|
$this->pushToStash('foo', 'bar', self::STASH_GLOBAL);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws Exception
|
|
*/
|
|
private function postAction(): void
|
|
{
|
|
$this->BreadcrumbsToStash();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
* @throws Exception
|
|
*/
|
|
private function menuData(): void
|
|
{
|
|
$this->pushToStash(
|
|
'menu',
|
|
[
|
|
[
|
|
'name' => 'Home',
|
|
'url' => '/',
|
|
],
|
|
[
|
|
'name' => 'News',
|
|
'url' => '/news',
|
|
],
|
|
[
|
|
'name' => 'Artikel',
|
|
'url' => '/article',
|
|
],
|
|
],
|
|
self::STASH_GLOBAL
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create header and meta-data.
|
|
*
|
|
* @return void
|
|
* @throws Exception
|
|
*/
|
|
private function headerData(): void
|
|
{
|
|
$this->pushToStash(
|
|
'meta',
|
|
[
|
|
'title' => 'News Page | Your #1 News Source',
|
|
],
|
|
self::STASH_GLOBAL
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
protected function pushToStash(string $name, mixed $data, string $context = self::STASH_LOCAL): void
|
|
{
|
|
if (array_key_exists($context, $this->stash) and array_key_exists($name, $this->stash[$context])) {
|
|
throw new Exception("The stash variable '$name' is already set!");
|
|
}
|
|
|
|
$this->stash[$context][$name] = $data;
|
|
}
|
|
|
|
protected function getBreadcrumbs()
|
|
{
|
|
return $this->breadcrumbs;
|
|
}
|
|
|
|
/**
|
|
* @throws SyntaxError
|
|
* @throws RuntimeError
|
|
* @throws LoaderError
|
|
*/
|
|
private function render(): void
|
|
{
|
|
$twig = new Environment(
|
|
new FilesystemLoader(self::DIR_TEMPLATES),
|
|
array(
|
|
'cache' => self::DIR_CACHE . '/twig',
|
|
'auto_reload' => true,
|
|
)
|
|
);
|
|
|
|
echo $twig->render(
|
|
$this->getTemplate(),
|
|
$this->getStash()
|
|
);
|
|
}
|
|
|
|
private function getStash(): array
|
|
{
|
|
return $this->stash;
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
private function BreadcrumbsToStash(): void
|
|
{
|
|
$this->pushToStash('breadcrumbs', $this->getBreadcrumbs()->getTemplateData(), self::STASH_GLOBAL);
|
|
}
|
|
|
|
}
|