caseyamcl / setterupper

A library to help you build setup workflows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setter-Upper

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

This is a library to simplify setup workflow for your applications. Features:

  • Every setup step is defined in its own class.
  • Supports dependencies between setup steps via dependsOn() and mustRunBefore()
  • Well-defined interfaces and simple reporting with the SetupStepResult() class
  • PSR-4 and PSR-12 compliant

Install

Via Composer

$ composer require caseyamcl/setterupper

Usage

The tl;dr version:

File: StepA.php

use SetterUpper\SetupStep;
use SetterUpper\SetupStepResult;

class StepA implements SetupStep
{
    public static function dependsOn(): iterable
    {
        return [];
    }

    public static function mustRunBefore(): iterable
    {
        return [];
    }
    
    public function __invoke(): SetupStepResult
    {
        $alreadySetup = false;
        
        if (! $alreadySetup) {
            // Do stuff here...
            return SetupStepResult::succeed('message here');                
        } else {
            return SetupStepResult::skip('already setup message');
        }
    }
    
    public function __toString() : string{
        return 'describe what StepA does';
    }
}

File: StepB.php

use SetterUpper\SetupStep;
use SetterUpper\SetupStepResult;

class StepB implements SetupStep
{
    public static function dependsOn(): iterable
    {
        return [StepA::class];
    }

    public static function mustRunBefore(): iterable
    {
        return [StepC::class];
    }
    
    public function __invoke(): SetupStepResult
    {
        $alreadySetup = false;
        $wasRequired = true;
                
        if (! $alreadySetup) {
            try {
                return SetupStepResult::succeed('message here');    
            } catch (\Throwable $e) {
                return SetupStepResult::fail('fail message here', $wasRequired);
            }
                            
        } else {
            return SetupStepResult::skip('already setup message');
        }
    }
    
    public function __toString() : string{
        return 'describe what StepB does';
    }
}

File: SetupRunner.php

use SetterUpper\SetterUpper;

$su = new SetterUpper();
$su->add(new StepA(), new StepB(), new StepC());
$report = $su->runAll();

In-depth

Naming

  • TODO: Mention that if you try to add the same class twice, a name collision exception will be thrown

Handing errors

  • TODO: Mention that this library does not handle exceptions thrown in SetupSteps

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email caseyamcl@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

A library to help you build setup workflows

License:MIT License


Languages

Language:PHP 100.0%