<?php
namespace Tools\Controller;
use Application\Controller\AbstractActionController;
use DavaHome\Framework\Form\Factory\GenericFormFactory;
use Laminas\View\Model\ViewModel;
use Tools\Form\ZeusForm;
use Tools\Service\ZeusFileLocatorService;
use Tools\Service\ZeusService;
class ZeusController extends AbstractActionController
{
private const SOURCE_SERVICE = 'ZeusService';
private const SOURCE_CONTROLLER = 'ZeusController';
private const SOURCE_FORM = 'ZeusForm';
private const SOURCE_VIEW = 'ZeusView';
private ZeusService $zeusService;
public function __construct(ZeusService $zeusService)
{
$this->zeusService = $zeusService;
}
private function getSources(): array
{
return [
self::SOURCE_SERVICE,
self::SOURCE_CONTROLLER,
self::SOURCE_FORM,
self::SOURCE_VIEW,
];
}
public function init()
{
$this->layout('layout/headless');
}
public function codeAction()
{
$source = $this->params()->fromRoute('source');
$codeFile = match ($source) {
self::SOURCE_SERVICE => ZeusFileLocatorService::getZeusServiceFilePath(),
self::SOURCE_CONTROLLER => ZeusFileLocatorService::getZeusControllerFilePath(),
self::SOURCE_FORM => ZeusFileLocatorService::getZeusFormFilePath(),
self::SOURCE_VIEW => ZeusFileLocatorService::getZeusViewFilePath(),
default => null
};
if ($codeFile === null) {
return $this->redirect()->toRoute('tools/zeus');
}
return new ViewModel([
'source' => $source,
'code' => highlight_file($codeFile, true),
'sources' => $this->getSources(),
]);
}
public function indexAction()
{
$zeusData = null;
$zeusForm = GenericFormFactory::createForm(ZeusForm::class);
if ($this->getRequest()->isPost()) {
$zeusForm->setData($this->params()->fromPost());
if ($zeusForm->isValid()) {
$zeusData = $this->zeusService->readZeusData($zeusForm->getData()['content']);
}
}
return new ViewModel([
'zeusForm' => $zeusForm,
'zeusData' => $zeusData,
'sources' => $this->getSources(),
]);
}
}