Commit f405c243 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Première version de la véréfication des déclaration des macros

parent e20d7019
Loading
Loading
Loading
Loading
Loading
+72 −0
Original line number Diff line number Diff line
<?php

use Laminas\Router\Http\Literal;
use UnicaenPrivilege\Guard\PrivilegeController;
use UnicaenRenderer\Controller\VerificationController;
use UnicaenRenderer\Controller\VerificationControllerFactory;
use UnicaenRenderer\Provider\Privilege\DocumentcontenuPrivileges;
use UnicaenRenderer\Provider\Privilege\DocumentmacroPrivileges;
use UnicaenRenderer\Provider\Privilege\DocumenttemplatePrivileges;

return [
    'bjyauthorize' => [
        'guards' => [
            PrivilegeController::class => [
                [
                    'controller' => VerificationController::class,
                    'action' => [
                        'index',
                    ],
                    'privileges' => [
                        DocumentcontenuPrivileges::DOCUMENTCONTENU_INDEX,
                        DocumentmacroPrivileges::DOCUMENTMACRO_INDEX,
                        DocumenttemplatePrivileges::DOCUMENTTEMPLATE_INDEX
                    ],
                ],
            ],
        ],
    ],

    'router' => [
        'routes' => [
            'unicaen-renderer' => [
                'type' => Literal::class,
                'options' => [
                    'route' => '/unicaen-renderer',
                ],
                'may_terminate' => false,
                'child_routes' => [
                    'verification' => [
                        'type' => Literal::class,
                        'options' => [
                            'route' => '/verification',
                            'defaults' => [
                                /** @see VerificationController::indexAction() */
                                'controller' => VerificationController::class,
                                'action' => 'index',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],

    'service_manager' => [
        'factories' => [
        ],
    ],
    'form_elements' => [
        'factories' => [
        ],
    ],
    'hydrators' => [
        'factories' => [
        ],
    ],
    'controllers' => [
        'factories' => [
            VerificationController::class => VerificationControllerFactory::class,
        ]
    ],
];
+28 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenRenderer\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use UnicaenRenderer\Service\Macro\MacroServiceAwareTrait;
use UnicaenRenderer\Service\Template\TemplateServiceAwareTrait;

class VerificationController extends AbstractActionController {
    use MacroServiceAwareTrait;
    use TemplateServiceAwareTrait;

    public function indexAction() : ViewModel
    {
        $macros_ = $this->getMacroService()->getMacros();
        $macros = []; foreach ($macros_ as $macro_) { $macros[$macro_->getCode()] = $macro_;}
        $templates = $this->getTemplateService()->getTemplates();
        [$macroSujet, $macroCorps] = $this->getTemplateService()->checkMacrosFromTemplates();

        return new ViewModel([
            'macros' => $macros,
            'templates' => $templates,
            'macroSujet' => $macroSujet,
            'macroCorps' => $macroCorps,
        ]);
    }
}
 No newline at end of file
+31 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenRenderer\Controller;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use UnicaenRenderer\Service\Macro\MacroService;
use UnicaenRenderer\Service\Template\TemplateService;

class VerificationControllerFactory {

    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function __invoke(ContainerInterface $container): VerificationController
    {
        /**
         * @var MacroService $macroService
         * @var TemplateService $templateService
         */
        $macroService = $container->get(MacroService::class);
        $templateService = $container->get(TemplateService::class);

        $controller = new VerificationController();
        $controller->setMacroService($macroService);
        $controller->setTemplateService($templateService);
        return $controller;
    }
}
 No newline at end of file
+3 −1
Original line number Diff line number Diff line
@@ -167,7 +167,9 @@ class MacroService

        $result = "macros = [\n";
        foreach ($macros as $macro) {
            $result .= "    { title:'" . $macro->getCode() . "', description:'" . strip_tags(str_replace("'", "\'", $macro->getDescription())) . "', content:'VAR[" . $macro->getCode() . "]' },\n";
            $code = $macro->getCode();
            $description = $macro->getDescription()?strip_tags(str_replace("'", "\'", $macro->getDescription())):"";
            $result .= "    { title:'" . $code . "', description:'" . $description . "', content:'VAR[" . $code . "]' },\n";
        }
        $result .= "];\n";

+17 −0
Original line number Diff line number Diff line
@@ -232,6 +232,23 @@ class TemplateService {
        return $this->replaceMacros($template->getSujet(), $variables);
    }
    
    /** Verification **************************************************************************************************/

    public function checkMacrosFromTemplates(): array
    {
        $macrosSujet = [];
        $macrosCorps = [];
        $templates = $this->getTemplates();
        foreach ($templates as $template) {
            $sujet = $template->getSujet();
            preg_match_all('/VAR\[[^[\]]+#[^[\]]+\]/', $sujet, $matches);
            $macrosSujet[$template->getCode()] = $matches[0];

            $corps = $template->getCorps();
            preg_match_all('/VAR\[[^[\]]+#[^[\]]+\]/', $corps, $matches);
            $macrosCorps[$template->getCode()] = $matches[0];
        }

        return [$macrosSujet, $macrosCorps];
    }
}
 No newline at end of file
Loading