Commit 4c0bdba7 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

6.2.4 (03/12/2024)

------------------

- Possibilité d'ajouter des callbacks pour suivre en temps réel l'évolution des calculs
parent e0a41096
Loading
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
CHANGELOG
=========

6.2.4 (03/12/2024)
------------------

- Possibilité d'ajouter des callbacks pour suivre en temps réel l'évolution des calculs

6.2.3 (20/08/2024)
------------------

src/Event.php

0 → 100644
+23 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenTbl;

use UnicaenTbl\TableauBord;

class Event
{
    const CALCUL = 'calcul';
    const GET = 'get';
    const PROCESS = 'process';
    const SET = 'set';
    const FINISH = 'finish';
    const PROGRESS = 'progress';

    public TableauBord $tableauBord;

    public string $action;

    public ?int $progress = null;

    public ?int $total = null;
}
 No newline at end of file
+30 −32
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace UnicaenTbl\Service;

use phpDocumentor\Reflection\Types\Mixed_;
use UnicaenTbl\TableauBord;

/**
@@ -11,8 +12,14 @@ use UnicaenTbl\TableauBord;
 */
class TableauBordService
{
    /** @var array|TableauBord[] */
    protected array $tableauxBord = [];

    /**
     * @var callable|null
     */
    private mixed $onAction = null;



    /**
@@ -31,8 +38,10 @@ class TableauBordService
    }



    public function getTableauBord(string $name): TableauBord
    {
        $this->tableauxBord[$name]->setOnAction($this->getOnAction());
        return $this->tableauxBord[$name];
    }

@@ -49,6 +58,21 @@ class TableauBordService



    public function getOnAction(): ?callable
    {
        return $this->onAction;
    }



    public function setOnAction(?callable $onAction): TableauBordService
    {
        $this->onAction = $onAction;
        return $this;
    }



    public function calculer(string|TableauBord $tableauBord, array $params = []): self
    {
        if (is_string($tableauBord)) {
@@ -62,11 +86,7 @@ class TableauBordService



    /**
     * @return int
     * @throws \Doctrine\DBAL\DBALException
     */
    public function calculerTout(array $excludes = [], $beforeTrigger = null, $afterTrigger = null)
    public function calculerTout(array $excludes = []): bool
    {
        $tableauxBord = $this->getTableauxBord();

@@ -75,32 +95,10 @@ class TableauBordService
            if (in_array($tableauBord->getName(), $excludes)) {
                continue;
            }
            $begin = microtime(true);


            if ($beforeTrigger instanceof \Closure) {
                $beforeTrigger([
                    'tableau-bord' => $tableauBord->getName(),
                ]);
            }
            try {
                $tableauBord->setOnAction($this->getOnAction());
                $tableauBord->calculer([]);
                if ($afterTrigger instanceof \Closure) {
                    $afterTrigger([
                        'tableau-bord' => $tableauBord->getName(),
                        'result'       => true,
                        'duree'        => microtime(true) - $begin,
                    ]);
                }
            } catch (\Exception $e) {
                if ($afterTrigger instanceof \Closure) {
                    $afterTrigger([
                        'tableau-bord' => $tableauBord->getName(),
                        'result'       => false,
                        'exception'    => $e,
                        'duree'        => microtime(true) - $begin,
                    ]);
                }
                $result = false;
            }
        }
+52 −0
Original line number Diff line number Diff line
@@ -3,8 +3,10 @@
namespace UnicaenTbl;

use Psr\Container\ContainerInterface;
use UnicaenTbl\Event;
use UnicaenTbl\Process\ProcessInterface;


/**
 * Description of TableauBord
 *
@@ -18,6 +20,15 @@ class TableauBord

    protected ?ProcessInterface $process = null;

    private float $timingBegin = 0.0;

    protected float $timing = 0.0;

    /**
     * @var callable|null
     */
    private mixed $onAction = null;


    /** @var string[] */
    protected array $options = [];
@@ -40,6 +51,42 @@ class TableauBord



    public function getOnAction(): ?callable
    {
        return $this->onAction;
    }



    public function setOnAction(?callable $onAction): TableauBord
    {
        $this->onAction = $onAction;
        return $this;
    }



    public function getTiming(): float
    {
        return $this->timing;
    }


    public function onAction(string $action, ?int $progress = null, ?int $total = null)
    {
        if (null !== $this->onAction) {
            $event              = new Event();
            $event->tableauBord = $this;
            $event->action      = $action;
            $event->progress    = $progress;
            $event->total       = $total;

            call_user_func($this->onAction, $event);
        }
    }



    public function getOrder(): int
    {
        return $this->order;
@@ -131,7 +178,12 @@ class TableauBord

    public function calculer(array $params = [])
    {
        $this->timing = 0.0;
        $this->timingBegin = microtime(true);
        $this->onAction(Event::CALCUL);
        $this->getProcess()->run($this, $params);
        $this->timing = microtime(true) - $this->timingBegin;
        $this->onAction(Event::FINISH);
    }

}
 No newline at end of file