PHP code example of ovac / guardrails

1. Go to this page and download the library: Download ovac/guardrails 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/ */

    

ovac / guardrails example snippets


// config/app.php
OVAC\Guardrails\GuardrailsServiceProvider::class,

use OVAC\Guardrails\Concerns\Guardrail;
use OVAC\Guardrails\Services\Flow;

class Post extends Model
{
    use Guardrail;

    public function guardrailAttributes(): array
    {
        return ['published'];
    }

    public function guardrailApprovalDescription(array $dirty, string $event): string
    {
        return 'Publish flag changes 

$post->guardrails()
    ->description($request->input('approval_description'))
    ->meta(['reason_code' => $request->input('reason_code')]);

$post->fill($request->validated())->save();

$result = $this->guardrailIntercept($post, ['published' => true], [
    'description' => 'Editorial approval dBy(1, 'Editorial Approval'),
]);

// config/guardrails.php
'flows' => [
    'posts' => [
        'publish' => [
            [
                'name' => 'Editorial Approval',
                'threshold' => 1,
                'signers' => [
                    'guard' => 'web',
                    'permissions' => ['content.publish'],
                    'permissions_mode' => 'any',
                    'roles' => [],
                    'roles_mode' => 'all',
                ],
                'meta' => [
                    'issions' => ['content.publish']],
//     ],
// ],

// Controller
use OVAC\Guardrails\Services\Flow;

// Resolve "posts.publish" flow: config first, fallback to code, merge meta defaults.
$flow = $this->guardrailFlow(
    'posts.publish',
    Flow::make()->anyOfPermissions(['content.publish'])->includeInitiator(true, true)->signedBy(2, 'Editorial Approval')->build(),
    ['summary' => 'Publish request for '.$post->title]
);

$result = $this->guardrailIntercept($post, ['published' => true], [
    'description' => 'Editorial approval 

Flow::make()
  ->anyOfPermissions(['content.publish'])    // any editor with publish permission
  ->

Flow::make()
  ->anyOfRoles(['support_lead'])             // support lead approves first
  ->signedBy(1, 'Support Approval')
  ->anyOfRoles(['security_officer'])         // then security approves
  ->signedBy(1, 'Security Approval')
  ->build();

Flow::make()
  ->anyOfRoles(['finance_manager','ops_manager'])
  ->signedBy(1, 'Management Approval')
  ->build();

Flow::make()
  ->permissions(['settings.update'])       // list all ->samePermissionAsInitiator(true)        // peer must share at least one
  ->

Flow::make()
  ->anyOfPermissions(['ops.change'])
  ->cfo'])
  ->signedBy(1, 'Executive Sign‑off')
  ->build();

// app/Models/ApprovalRequest.php
namespace App\Models;

class ApprovalRequest extends \OVAC\Guardrails\Models\ApprovalRequest
{
    protected $casts = [
        'meta' => 'array',
        'reason' => 'string',
    ];

    public function workspace()
    {
        return $this->belongsTo(Workspace::class);
    }
}
bash
php artisan vendor:publish --provider="OVAC\\Guardrails\\GuardrailsServiceProvider" --tag=guardrails-config
php artisan vendor:publish --provider="OVAC\\Guardrails\\GuardrailsServiceProvider" --tag=guardrails-migrations
php artisan vendor:publish --provider="OVAC\\Guardrails\\GuardrailsServiceProvider" --tag=guardrails-views
php artisan vendor:publish --provider="OVAC\\Guardrails\\GuardrailsServiceProvider" --tag=guardrails-assets
php artisan vendor:publish --provider="OVAC\\Guardrails\\GuardrailsServiceProvider" --tag=guardrails-docs
bash
php artisan migrate