Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Application CLI avec Silly

Application CLI avec Silly

Lightening Talk du sfPot Décembre 2015

Alexandre Balmes

December 22, 2015
Tweet

More Decks by Alexandre Balmes

Other Decks in Programming

Transcript

  1. APPLICATION CLI AVEC SILLY

    View Slide

  2. PRÉSENTATION GÉNÉRALE

    View Slide

  3. PRÉSENTATION
    ➤ Micro-Framework basé sur le composant “symfony/console”
    ➤ Premier commit en Février 2015
    ➤ Actuellement en version 1.1.1
    ➤ http://mnapoli.fr/silly/ (du coup c’est lyonnais, vive les quenelles)
    $ composer require mnapoli/silly

    View Slide

  4. LA BASE : L’APPLICATION
    ➤ Le fonctionnement est super simple
    ➤ Silly\Application étends Symfony\Console\Application
    $app = new Silly\Application();
    // do stuff
    $app->run();

    View Slide

  5. LA BASE : LA COMMANDE
    ➤ La définition d’une commande est toujours aussi simple
    ➤ Une commande est un callable // Closure
    $app->command('foo', function () {
    // ...
    });
    // A function
    $app->command('foo', ‘someFunction');
    // An object implementing __invoke()
    $app->command('foo', new InvokableObject());
    // An object method
    $app->command('foo', [$object, 'method']);
    // A static class method
    $app->command('foo', ['MyClass', 'method']);

    View Slide

  6. LES COMMANDES
    Nom greet / demo:greet
    Argument obligatoire greet name
    Argument optionnel greet [name]
    Tableau avec 0-n arguments greet [name]*
    Tableau avec 1-n arguments greet name*
    Option type “flag” (boolean) greet [--yell]
    Option obligatoire greet [--yell=]
    Tableau d’options 0-n greet [--iterations=]*
    Option avec raccourci greet [-y|--yell]

    View Slide

  7. CONSOLE HELPERS
    ➤ La même chose qu’avec Symfony
    $app = new Silly\Application();
    $app->command('greet', function ($input, $output) use ($app) {
    $helper = $app->getHelperSet()->get('question');
    $question = new ConfirmationQuestion('Are you sure?', false);
    if ($helper->ask($input, $output, $question)) {
    $output->writeln('Hello!');
    }
    });

    View Slide

  8. DEPENDENCY INJECTION
    ➤ Silly est basé sur Container Interop : https://github.com/container-interop/
    container-interop
    ➤ Donc c’est aussi super bien (il y a deux éditions avec Pimple et PHP-DI built-in)
    Acclimate: Adapters for Aura.Di, Laravel, Nette DI, Pimple, Symfony DI, ZF2 Service manager, ZF2 Dependency injection and any container using ArrayAccess
    Aura.Di
    dcp-di
    Disco
    League/Container
    Mouf
    Njasm Container
    PHP-DI
    PimpleInterop
    Pimple3-ContainerInterop (using Pimple v3)
    SitePoint Container
    Ultra-Lite Container
    Unbox
    XStatic
    Zend\ServiceManager

    View Slide

  9. PROJET #1
    Tower-generator

    View Slide

  10. TOWER GENERATOR
    ➤ Projet client avec Ansible + Tower
    ➤ Tower ne possède pas de client PHP pour son API
    ➤ Mais Tower possède un client CLI pour son API
    ➤ Une premiere application (Zend Diactoros + Aura.router) récupère des hooks
    Bitbucket
    ➤ L’application “Hook” execute son traitement et délègue à l’application “Cli”
    ➤ L’application Cli interagit avec le CLI Tower

    View Slide

  11. TOWER GENERATOR
    require_once(__DIR__ . '/../vendor/autoload.php');
    $builder = new DI\ContainerBuilder();
    $builder->addDefinitions(__DIR__ . ‘/../app/config/config.php'); // Credentials Tower
    $app = new Silly\Edition\PhpDi\Application('Tower-generator', '1.0', $builder->build());
    $app->getContainer();
    $app->command('status', new \Tasks\Status()); // Est-ce que le serveur réponds ?
    $app->command('pr-merged project', new \Tasks\JobLaunch()); // Playbook pour déployer
    $app->command('push project', new \Tasks\JobLaunch()); // Playbook pour déployer
    $app->command('instance-created project ip target [--repository]', new \Tasks\InstanceCreated()); // Nouveau projet
    $app->run();

    View Slide

  12. TOWER GENERATOR
    namespace Tasks;
    use Process\Process;
    use Symfony\Component\Console\Output\ConsoleOutputInterface;
    /**
    * Class Status
    */
    class Status
    {
    protected $output;
    public function __invoke(ConsoleOutputInterface $output)
    {
    $process = new Process(
    sprintf('tower-cli version’), // Un call API
    $output
    );
    $process->run();
    }
    }

    View Slide

  13. PROJET #2
    Mailer

    View Slide

  14. MAILER
    ➤ Projet client pour “envoyer des mails”
    ➤ Les applications existantes sont désastreuses à ce niveau et dans de multiples
    languages
    ➤ Il est impossible de s’intégrer à toutes les applications existantes
    ➤ Vive les grosses boites avec des multiples prestas
    Application principale RabbitMQ Application Mailer
    Application principale
    Application principale

    View Slide

  15. CONCLUSION
    C’EST FAIT PAR UN LYONNAIS DONC C’EST COOL !

    View Slide

  16. POURQUOI C’EST COOL ?
    ➤ Parce que c’est super simple d’utilisation
    ➤ Parce que ça peux scaller du point de vue code (via les callables)
    ➤ Parce que c’est isolé et ça permet de répondre à un besoin unique
    ➤ Parce que la stack est “simple”

    View Slide

  17. POURQUOI C’EST PAS COOL ?
    ➤ Si vous trouvez le composant Console tout pourri, forcément c’est pas cool
    ➤ Sinon, slide précédent

    View Slide

  18. MERCI !
    twitter.com/pockystar

    View Slide