Commit d101995d authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Module StepStar : suppression du XsltService abandonné.

parent d3f60e1f
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -46,8 +46,6 @@ use StepStar\Service\Xml\XmlService;
use StepStar\Service\Xml\XmlServiceFactory;
use StepStar\Service\Xsl\XslService;
use StepStar\Service\Xsl\XslServiceFactory;
use StepStar\Service\Xslt\XsltService;
use StepStar\Service\Xslt\XsltServiceFactory;
use StepStar\Service\Zip\ZipService;
use StepStar\Service\Zip\ZipServiceFactory;
use Unicaen\Console\Router\Simple;
@@ -565,7 +563,6 @@ return [

            XmlService::class => XmlServiceFactory::class,
            XslService::class => XslServiceFactory::class,
            XsltService::class => XsltServiceFactory::class,
            TefService::class => TefServiceFactory::class,
            ApiService::class => ApiServiceFactory::class,
            SoapClient::class => SoapClientFactory::class,
+0 −5
Original line number Diff line number Diff line
@@ -5,15 +5,10 @@ namespace StepStar\Service\Tef;
use Application\Command\ShellCommandRunner;
use StepStar\Command\TransformShellCommand;
use StepStar\Exception\TefServiceException;
use StepStar\Service\Xml\XmlServiceAwareTrait;
use StepStar\Service\Xslt\XsltServiceAwareTrait;
use UnicaenApp\Exception\RuntimeException;

class TefService
{
    use XmlServiceAwareTrait;
    use XsltServiceAwareTrait;

    /**
     * @var string
     */
+1 −22
Original line number Diff line number Diff line
@@ -3,33 +3,12 @@
namespace StepStar\Service\Tef;

use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use StepStar\Service\Xml\XmlService;
use StepStar\Service\Xslt\XsltService;

class TefServiceFactory implements FactoryInterface
{
    /**
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): TefService
    {
        /**
         * @var \StepStar\Service\Xslt\XsltService $xslService
         * @var \StepStar\Service\Xml\XmlService $xmlService
         */
        try {
            $xslService = $container->get(XsltService::class);
        } catch (ContainerExceptionInterface $e) {
        }
        $xmlService = $container->get(XmlService::class);

        $service = new TefService();
        $service->setXmlService($xmlService);
        $service->setXsltService($xslService);

        return $service;
        return new TefService();
    }
}
 No newline at end of file
+0 −151
Original line number Diff line number Diff line
<?php

namespace StepStar\Service\Xslt;

use Saxon\SaxonProcessor;
use Saxon\Xslt30Processor;

class XsltService
{
    protected SaxonProcessor $saxonProcessor;
    protected Xslt30Processor $transformer;
    protected string $outputDir;
    protected string $xslFilePath;

    /**
     * Spécifie le chemin du répertoire cible des fichiers XML résultats.
     *
     * @param string $outputDir
     * @return self
     */
    public function setOutputDir(string $outputDir): self
    {
        $this->outputDir = $outputDir;
        return $this;
    }

    /**
     * Spécifie le chemin du fichier XSL de transformation à utiliser.
     *
     * @param mixed $xslFilePath
     * @return self
     */
    public function setXslFilePath(string $xslFilePath): self
    {
        $this->xslFilePath = $xslFilePath;
        return $this;
    }

    /**
     * Génération des fichiers XML TEF dans le répertoire de sortie spécifié via {@see setOutputDir()}.
     *
     * @param string $thesesXmlFilepath Fichier XML source
     */
    public function transformToFiles(string $thesesXmlFilepath)
    {
        if (! class_exists(SaxonProcessor::class)) {
            throw new \RuntimeException(
                "Le module PHP SaxonC est nécessaire pour cette opération (https://www.saxonica.com/download/c.xml)."
            );
        }

        // il faut spécifier un fichier mais c'est son répertoire qui recevra les fichiers générés
        $outputFilepath = $this->outputDir . '/' . uniqid('saxonc_') . '.xml';

        // invoke the kill function during shutdown
        register_shutdown_function([self::class, 'killCurrentThread']); // NE CHANGE RIEN :-(

        $this->saxonProcessor = new SaxonProcessor();
        $this->transformer = $this->saxonProcessor->newXslt30Processor();
        $this->transformer->setInitialMatchSelectionAsFile($thesesXmlFilepath);
        $this->transformer->applyTemplatesReturningFile($this->xslFilePath, $outputFilepath);
        $this->transformer->clearParameters();
        $this->transformer->clearProperties();
        unlink($outputFilepath);
    }

    /**
     * @return bool
     */
    public function hasErrors(): bool
    {
        return $this->transformer->getExceptionCount() > 0;
    }

    /**
     * @return array [code => message]
     */
    public function getErrors(): array
    {
        $errors = [];

        $errCount = $this->transformer->getExceptionCount();
        if ($errCount > 0) {
            for ($i = 0; $i < $errCount; $i++) {
                $errCode = $this->transformer->getErrorCode($i);
                $errMessage = $this->transformer->getErrorMessage($i);
                $errors[$errCode] = $errMessage;
            }
            $this->transformer->exceptionClear();
        }

        return $errors;
    }

    /**
     * @see https://saxonica.plan.io/issues/4371
     *
     * @param int $minutes - minutes to wait until process is killed
     * @return bool
     */
    static public function killCurrentThread(int $minutes = 0): bool
    {
        if (!stristr(php_sapi_name(), 'fpm-')) {
            return false;
        }

        $signal = SIGTERM;
        $pid = posix_getpid();

        // get a list of child processes for the current running process
        $cmd = "pstree -p {$pid} | grep -o '([0-9]\+)' | grep -o '[0-9]\+'";
        exec($cmd, $processlist);

        $processlist = array_map('intval', $processlist);
        $processlist = array_filter($processlist, function ($cid) use ($pid) {
            return $cid !== $pid;
        });

        $processlist = array_filter($processlist, function ($cid) {
            return posix_kill($cid, 0);
        });

        if (!count($processlist)) {
            return false;
        }

        $firstchild = array_shift($processlist);

        $prefix = sprintf('rmproc.%s.%s.', $firstchild, uniqid(true));
        $tmpfile = tempnam(sys_get_temp_dir(), $prefix);
        chmod($tmpfile, 0755);

        $script = <<<BASH
#!/bin/sh

PID={$firstchild}
sleep 5 && kill -s {$signal} \$PID  > /dev/null 2>&1
rm \$0
BASH;

        file_put_contents($tmpfile, $script);
        $cmd = sprintf(
            '/usr/bin/at now +%d min -f %s 2>&1 | /usr/bin/logger -i &',
            $minutes,
            $tmpfile
        );

        exec($cmd);
        return true;
    }
}
 No newline at end of file
+0 −23
Original line number Diff line number Diff line
<?php

namespace StepStar\Service\Xslt;

trait XsltServiceAwareTrait
{
    /**
     * @var XsltService
     */
    protected $xsltService;

    /**
     * @param XsltService $xsltService
     * @return self
     */
    public function setXsltService(XsltService $xsltService): self
    {
        $this->xsltService = $xsltService;
        return $this;
    }


}
 No newline at end of file
Loading