🎉 initial commit
This commit is contained in:
210
classes/Application/Controller/MainController.php
Normal file
210
classes/Application/Controller/MainController.php
Normal file
@@ -0,0 +1,210 @@
|
||||
<?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;
|
||||
use Faker\Factory;
|
||||
use Faker\Generator;
|
||||
|
||||
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();
|
||||
|
||||
/**
|
||||
* Fake data generator.
|
||||
*
|
||||
* @var Generator
|
||||
*/
|
||||
protected Generator $faker;
|
||||
|
||||
/**
|
||||
* 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->faker = Factory::create();
|
||||
$this->faker->seed(1234567);
|
||||
$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' => $this->faker->domainName(),
|
||||
],
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user