PHP code example of mariojgt / witchcraft

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

    

mariojgt / witchcraft example snippets


// Execute any workflow with one line
$result = witchcraft_trigger('FLOW_MY_WORKFLOW', ['data' => 'value']);

// Simple trigger
$result = witchcraft_trigger('FLOW_PROCESS_ORDER', [
    'order_id' => 123,
    'user_id' => 456
]);

// Safe trigger with error handling
$response = witchcraft_safe_trigger('FLOW_SEND_EMAIL', ['email' => '[email protected]']);
if ($response['success']) {
    echo "Email sent successfully!";
} else {
    echo "Error: " . $response['error'];
}

if (witchcraft_exists('FLOW_BACKUP_DATA')) {
    $result = witchcraft_trigger('FLOW_BACKUP_DATA');
}

$info = witchcraft_info('FLOW_GENERATE_REPORT');
echo "Workflow: {$info['name']} (Version {$info['version']})";

// Mark workflow as protected (cannot be deleted)
$workflow->update(['is_deletable' => false]);

// Latest version always uses clean trigger code
witchcraft_trigger('FLOW_MY_PROCESS', $data); // Always runs latest version

// Old versions get versioned codes automatically:
// V1: FLOW_MY_PROCESS_OLD_V1
// V2: FLOW_MY_PROCESS_OLD_V2
// V3: FLOW_MY_PROCESS (current/latest)

namespace App\Witchcraft\Handlers;

use Mariojgt\Witchcraft\Contracts\NodeHandlerInterface;

class ProcessPaymentNodeHandler implements NodeHandlerInterface
{
    public function getMetadata(): array
    {
        return [
            'type' => 'process-payment',
            'category' => 'Payments',
            'icon' => 'CreditCardIcon',        // Lucide icon
            'label' => 'Process Payment',
            'component' => 'ProcessPaymentNode',
            'initialData' => [
                'amount' => 0,
                'currency' => 'USD'
            ]
        ];
    }

    public function execute(array $nodeData, array $variables): array
    {
        // Your payment processing logic here
        $amount = $nodeData['amount'];

        // Process payment...

        return [
            'success' => true,
            'payment_id' => 'pay_123456',
            'status' => 'completed'
        ];
    }
}

use Mariojgt\Witchcraft\Traits\HasWitchcraftTriggers;

class Order extends Model
{
    use HasWitchcraftTriggers;

    // Workflows will automatically trigger on:
    // - created, updated, deleted events
    // - Any model changes
}

// When order is created, trigger workflow
witchcraft_trigger('FLOW_PROCESS_ORDER', [
    'order_id' => $order->id,
    'customer_email' => $order->customer->email,
    'total' => $order->total
]);

// Welcome new users
witchcraft_trigger('FLOW_USER_WELCOME', [
    'user_id' => $user->id,
    'email' => $user->email,
    'name' => $user->name
]);

// Daily backup workflow
witchcraft_trigger('FLOW_DAILY_BACKUP', [
    'date' => now()->toDateString(),
    'tables' => ['users', 'orders', 'products']
]);
bash
php artisan migrate
bash
php artisan witchcraft:make-node ProcessPayment --category=Payments