PHP code example of omnitech-solutions / flowlight
1. Go to this page and download the library: Download omnitech-solutions/flowlight library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
omnitech-solutions / flowlight example snippets
use Flowlight\Action;
use Flowlight\Context;
class CalculateDiscount extends Action
{
protected function perform(Context $ctx): void
{
$amount = $ctx->paramsArray()['amount'] ?? null;
if (!is_numeric($amount)) {
$ctx->withErrors(['amount' => 'must be numeric']);
$ctx->throwAndReturn('Validation failed'); // control flow unwinds
}
$ctx->withParams(['discount' => (float)$amount * 0.1]);
// completion is set internally when appropriate
}
}
use Flowlight\Organizer;
class CheckoutOrganizer extends Organizer
{
protected static function steps(): array
{
return [
\App\Actions\ValidateCheckout::class,
\App\Actions\CalculateDiscount::class,
\App\Actions\ChargePayment::class,
\App\Actions\SendReceipt::class,
];
}
}
$ctx->throwAndReturn('Unauthorized');
// or
$ctx->throwAndReturn('Upstream unavailable', ['error_code' => 502]);
use Flowlight\Traits\WithErrorHandler;
class ExternalCallService
{
use WithErrorHandler;
public function run(\Flowlight\Context $ctx): void
{
self::withErrorHandler($ctx, static function (\Flowlight\Context $c): void {
performExternalCall(); // may throw
}, rethrow: false);
}
}