<?php
namespace Web\Core\Engine\Block;
use IcebirdCMS\App\KernelLoader;
use Web\Core\Engine\TwigTemplate;
use Web\Core\Engine\Url;
use Symfony\Component\HttpKernel\KernelInterface;
use Web\Core\Engine\Base\Block as WebBaseBlock;
use Web\Core\Engine\Base\Config;
use Web\Core\Engine\Exception as WebException;
use Web\Core\Language\Language as FL;
class ExtraInterface extends KernelLoader implements ModuleExtraInterface
{
private $action;
private $config;
private $data;
private $module;
private $object;
private $output;
protected $overwrite = false;
protected $templatePath = '';
protected $template;
protected $url;
public function __construct(KernelInterface $kernel, string $module, string $action = null, $data = null)
{
parent::__construct($kernel);
$this->setModule($module);
$this->setAction($action);
$this->setData($data);
$this->template = $this->getContainer()->get('templating');
$this->url = $this->getContainer()->get('url');
$this->loadConfig();
}
public function execute(): void
{
$actionClass = 'Web\\Modules\\' . $this->getModule() . '\\Controller\\' . $this->getAction();
if ($this->getModule() === 'Core') {
$actionClass = 'Web\\Core\\Actions\\' . $this->getAction();
}
if (!class_exists($actionClass)) {
throw new WebException('The action class couldn\'t be found: ' . $actionClass . '.');
}
$this->object = new $actionClass($this->getKernel(), $this->getModule(), $this->getAction(), $this->getData());
if (!is_callable([$this->object, 'execute'])) {
throw new WebException('The action file should contain a callable method "execute".');
}
$this->object->execute();
$this->setOverwrite($this->object->getOverwrite());
if ($this->object->getTemplatePath() !== null) {
$this->setTemplatePath($this->object->getTemplatePath());
}
}
public function getAction(): ?string
{
if ($this->action !== null) {
if (!\in_array($this->action, $this->config->getPossibleActions(), true)) {
$this->setAction($this->config->getDefaultAction());
}
return $this->action;
}
$actionParameter = $this->url->getParameter(0);
if ($actionParameter === null) {
$this->setAction($this->config->getDefaultAction());
return $this->action;
}
$actionParameter = \SpoonFilter::toCamelCase($actionParameter);
foreach ($this->config->getPossibleActions() as $actionName) {
$actionUrl = \SpoonFilter::toCamelCase(
rawurlencode(FL::act(\SpoonFilter::toCamelCase($actionName)))
);
if ($actionUrl === $actionParameter) {
$this->setAction($actionName);
break;
}
}
if (!\in_array($this->action, $this->config->getPossibleActions(), true)) {
$this->setAction($this->config->getDefaultAction());
}
return $this->action;
}
public function getContent(): string
{
if ($this->output === null) {
return trim($this->object->getContent());
}
return trim($this->output);
}
public function getData()
{
return $this->data;
}
public function getModule(): string
{
return $this->module;
}
public function getOverwrite(): bool
{
return $this->overwrite;
}
public function getTemplate(): TwigTemplate
{
return $this->object->getTemplate();
}
public function getTemplatePath(): string
{
return $this->templatePath;
}
public function getVariables(): array
{
return (array) $this->template->getAssignedVariables();
}
public function loadConfig(): void
{
$configClass = 'Web\\Modules\\' . $this->getModule() . '\\Config';
if ($this->getModule() === 'Core') {
$configClass = 'Web\\Core\\Config';
}
if (!class_exists($configClass)) {
throw new WebException('The config file ' . $configClass . ' could not be found.');
}
$this->config = new $configClass($this->getKernel(), $this->getModule());
}
private function setAction(string $action = null): void
{
$this->action = $action;
}
private function setData($data): void
{
$this->data = $data;
}
private function setModule(string $module): void
{
$this->module = $module;
}
private function setOverwrite(bool $overwrite): void
{
$this->overwrite = $overwrite;
}
private function setTemplatePath(string $path): void
{
$this->templatePath = $path;
}
}