src/Web/Core/Engine/Block/ExtraInterface.php line 57

Open in your IDE?
  1. <?php
  2. namespace Web\Core\Engine\Block;
  3. use IcebirdCMS\App\KernelLoader;
  4. use Web\Core\Engine\TwigTemplate;
  5. use Web\Core\Engine\Url;
  6. use Symfony\Component\HttpKernel\KernelInterface;
  7. use Web\Core\Engine\Base\Block as WebBaseBlock;
  8. use Web\Core\Engine\Base\Config;
  9. use Web\Core\Engine\Exception as WebException;
  10. use Web\Core\Language\Language as FL;
  11. class ExtraInterface extends KernelLoader implements ModuleExtraInterface
  12. {
  13.     private $action;
  14.     private $config;
  15.     private $data;
  16.     private $module;
  17.     private $object;
  18.     private $output;
  19.     protected $overwrite false;
  20.     protected $templatePath '';
  21.     protected $template;
  22.     protected $url;
  23.     public function __construct(KernelInterface $kernelstring $modulestring $action null$data null)
  24.     {
  25.         parent::__construct($kernel);
  26.         $this->setModule($module);
  27.         $this->setAction($action);
  28.         $this->setData($data);
  29.         $this->template $this->getContainer()->get('templating');
  30.         $this->url $this->getContainer()->get('url');
  31.         $this->loadConfig();
  32.     }
  33.     public function execute(): void
  34.     {
  35.         $actionClass 'Web\\Modules\\' $this->getModule() . '\\Controller\\' $this->getAction();
  36.         if ($this->getModule() === 'Core') {
  37.             $actionClass 'Web\\Core\\Actions\\' $this->getAction();
  38.         }
  39.         if (!class_exists($actionClass)) {
  40.             throw new WebException('The action class couldn\'t be found: ' $actionClass '.');
  41.         }
  42.         $this->object = new $actionClass($this->getKernel(), $this->getModule(), $this->getAction(), $this->getData());
  43.         if (!is_callable([$this->object'execute'])) {
  44.             throw new WebException('The action file should contain a callable method "execute".');
  45.         }
  46.         $this->object->execute();
  47.         $this->setOverwrite($this->object->getOverwrite());
  48.         if ($this->object->getTemplatePath() !== null) {
  49.             $this->setTemplatePath($this->object->getTemplatePath());
  50.         }
  51.     }
  52.     public function getAction(): ?string
  53.     {
  54.         if ($this->action !== null) {
  55.             if (!\in_array($this->action$this->config->getPossibleActions(), true)) {
  56.                 $this->setAction($this->config->getDefaultAction());
  57.             }
  58.             return $this->action;
  59.         }
  60.         $actionParameter $this->url->getParameter(0);
  61.         if ($actionParameter === null) {
  62.             $this->setAction($this->config->getDefaultAction());
  63.             return $this->action;
  64.         }
  65.         $actionParameter = \SpoonFilter::toCamelCase($actionParameter);
  66.         foreach ($this->config->getPossibleActions() as $actionName) {
  67.             $actionUrl = \SpoonFilter::toCamelCase(
  68.                 rawurlencode(FL::act(\SpoonFilter::toCamelCase($actionName)))
  69.             );
  70.             if ($actionUrl === $actionParameter) {
  71.                 $this->setAction($actionName);
  72.                 break;
  73.             }
  74.         }
  75.         if (!\in_array($this->action$this->config->getPossibleActions(), true)) {
  76.             $this->setAction($this->config->getDefaultAction());
  77.         }
  78.         return $this->action;
  79.     }
  80.     public function getContent(): string
  81.     {
  82.         if ($this->output === null) {
  83.             return trim($this->object->getContent());
  84.         }
  85.         return trim($this->output);
  86.     }
  87.     public function getData()
  88.     {
  89.         return $this->data;
  90.     }
  91.     public function getModule(): string
  92.     {
  93.         return $this->module;
  94.     }
  95.     public function getOverwrite(): bool
  96.     {
  97.         return $this->overwrite;
  98.     }
  99.     public function getTemplate(): TwigTemplate
  100.     {
  101.         return $this->object->getTemplate();
  102.     }
  103.     public function getTemplatePath(): string
  104.     {
  105.         return $this->templatePath;
  106.     }
  107.     public function getVariables(): array
  108.     {
  109.         return (array) $this->template->getAssignedVariables();
  110.     }
  111.     public function loadConfig(): void
  112.     {
  113.         $configClass 'Web\\Modules\\' $this->getModule() . '\\Config';
  114.         if ($this->getModule() === 'Core') {
  115.             $configClass 'Web\\Core\\Config';
  116.         }
  117.         if (!class_exists($configClass)) {
  118.             throw new WebException('The config file ' $configClass ' could not be found.');
  119.         }
  120.         $this->config = new $configClass($this->getKernel(), $this->getModule());
  121.     }
  122.     private function setAction(string $action null): void
  123.     {
  124.         $this->action $action;
  125.     }
  126.     private function setData($data): void
  127.     {
  128.         $this->data $data;
  129.     }
  130.     private function setModule(string $module): void
  131.     {
  132.         $this->module $module;
  133.     }
  134.     private function setOverwrite(bool $overwrite): void
  135.     {
  136.         $this->overwrite $overwrite;
  137.     }
  138.     private function setTemplatePath(string $path): void
  139.     {
  140.         $this->templatePath $path;
  141.     }
  142. }