Commit 8f266c21 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+3 −0
Original line number Diff line number Diff line
.idea/
vendor/
composer.lock

.gitlab-ci.yml

0 → 100644
+24 −0
Original line number Diff line number Diff line
image: registre.unicaen.fr:5000/unicaen-dev-php8.0-apache

stages:
- publish
#- tests

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
  - vendor/

#unit-tests:
#  stage: tests
#  script:
#    - composer install --no-interaction --no-suggest --no-progress
#    - php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-text=coverage.txt --colors=never
#  artifacts:
#    paths:
#      - coverage.txt

update-satis:
  stage: publish
  script:
    - curl https://gest.unicaen.fr/packagist/update

CHANGELOG.md

0 → 100644
+6 −0
Original line number Diff line number Diff line
CHANGELOG
=========

1.0.0
-----
- Première version (fonctionnalités extraites de SyGAL).

Module.php

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

namespace UnicaenShell;

use Laminas\Config\Factory as ConfigFactory;

class Module
{
    const NAME = __NAMESPACE__;

    public function getConfig()
    {
        $paths = array_merge(
            [__DIR__ . '/config/module.config.php'],
        );

        return ConfigFactory::fromFiles($paths);
    }

    public function getAutoloaderConfig(): array
    {
        return [
            'Laminas\Loader\StandardAutoloader' => [
                'namespaces' => [
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ],
            ],
        ];
    }
}

README.md

0 → 100644
+140 −0
Original line number Diff line number Diff line
Bibliothèque unicaen/shell
==========================
**Bibliothèque de création et lancement de commandes shell.**

*Si vous cherchez qqchose de plus carré et ambitieux, regardez du côté du composant Symfony 
[Process](https://symfony.com/doc/current/components/process.html).*


Classe de commande 
------------------

```php
use UnicaenShell\Command\ShellCommand;

final class NoopShellCommand extends ShellCommand
{
    protected string $executable = ':'; // no op

    public function getName(): string
    {
        return 'NoopShellCommand';
    }

    public function generateCommandLine()
    {
        $this->commandLine = $this->executable;
    }
}
```

Cf. la classe mère [ShellCommand](src/UnicaenShell/Command/ShellCommand.php). 


Config
------

Exemple :

```php
    'unicaen-shell' => [
        'commands' => [
            \UnicaenShell\Command\Example\NoopShellCommand::class => [
                //'executable' => ':', // écraserait {@see \UnicaenShell\Command\Example\NoopShellCommand::$executable}
            ],
        ],
    ],
```

Cette config est exploitée par la factory abstraite [ShellCommandAbstractFactory](src/UnicaenShell/Command/ShellCommandAbstractFactory.php).


Obtention via le service manager
--------------------------------

```php
/** @var \UnicaenShell\Command\Example\NoopShellCommand $command */
$command = $container->get(\UnicaenShell\Command\Example\NoopShellCommand::class);
```


Lancement classique
-------------------

```php
use UnicaenShell\Command\ShellCommandRunnerTrait;

class MonController extends \Laminas\Mvc\Controller\AbstractActionController
{
    public function oneAction() 
    {
        $result = $this->runShellCommand($this->command);
        // ...      
    }
}
```


Lancement avec temps d'exécution max (timeout)
----------------------------------------------

```php
use UnicaenShell\Command\ShellCommandRunnerTrait;

class MonService
{
    public function genererPdf() 
    {
        try {
            // Un timeout peut être appliqué au lancement de la commande.
            // Si ce timout est atteint, l'exécution de la commande est interrompue
            // et une exception TimedOutCommandException est levée.
            $result = $this->runShellCommand($this->command, '10s');
        } catch (TimedOutCommandException $toce) {
            //...
            // Exemple : lancer une commande en tâche de fond qui fait la même chose mais qui envoie le PDF par mail.
            //... 
        }
    }
}
```


Lancement en tâche de fond
--------------------------

```php
use UnicaenShell\Command\ShellCommandRunnerTrait;

class MonService
{
    public function genererPdf() 
    {
        // Lance une commande en arrière-plan (nohup + &).
        // Du coup, pas de collecte de résultat ici.
        $this->runShellCommandInBackground($this->command);
    }
}
```


Usage explicite
---------------

Il est bien-sûr possible de créer/manipuler directement commande et runner, exemple :

```php
// lancement de la commande de retraitement du fichier PDF en tâche de fond
$destinataires = $newFichierThese->getFichier()->getHistoModificateur()->getEmail();
$command = new RetraitementShellCommand();
$command->setDestinataires($destinataires);
$command->setFichierThese($fichierThese);
$command->generateCommandLine();
$runner = new ShellCommandRunner();
$runner->setCommand($command);
try {
    $runner->runCommandInBackground();
} catch (\UnicaenShell\Command\Exception\ShellCommandException $e) {
    throw new RuntimeException("Erreur survenue lors du lancement de la commande de retraitement", 0, $e);
}
```
Loading