/* |-------------------------------------------------------------------------- | CB CRM EVENT ENGINE (CORE) |-------------------------------------------------------------------------- */ function cb_event(string $event, array $payload = []): void { global $cb_actions; if (!isset($cb_actions[$event])) { return; } foreach ($cb_actions[$event] as $callback) { if (is_callable($callback)) { call_user_func($callback, $payload); } } } function cb_on(string $event, callable $callback): void { global $cb_actions; $cb_actions[$event][] = $callback; } function cb_trigger(string $event, array $payload = []): void { cb_event($event, $payload); } /* |-------------------------------------------------------------------------- | CB AUTOMATION FLOW ENGINE |-------------------------------------------------------------------------- */ function cb_flow(string $flow, array $data = []): void { $flows = cb_flows(); if (!isset($flows[$flow])) { return; } foreach ($flows[$flow] as $step) { if (is_callable($step)) { $data = $step($data) ?? $data; } } } function cb_flows(): array { return [ 'invoice.created' => [ function ($data) { cb_event('log', [ 'message' => 'Invoice created', 'data' => $data ]); return $data; } ], 'payment.completed' => [ function ($data) { cb_event('log', [ 'message' => 'Payment completed', 'data' => $data ]); return $data; } ], 'hosting.updated' => [ function ($data) { cb_event('log', [ 'message' => 'Hosting updated', 'data' => $data ]); return $data; } ], ]; } function cb_log(string $message, array $data = []): void { $line = '[CB CRM] ' . $message . ' ' . json_encode($data) . PHP_EOL; error_log($line); } global $cb_events; if (!isset($cb_events)) { $cb_events = []; } function cb_on(string $event, callable $callback): void { global $cb_events; $cb_events[$event][] = $callback; } function cb_event(string $event, array $payload = []): void { global $cb_events; if (empty($cb_events[$event])) { return; } foreach ($cb_events[$event] as $callback) { if (is_callable($callback)) { $callback($payload); } } } global $cb_flows; if (!isset($cb_flows)) { $cb_flows = []; } function cb_flow_register(string $flow, callable $step): void { global $cb_flows; $cb_flows[$flow][] = $step; } function cb_flow_run(string $flow, array $data = []): array { global $cb_flows; if (empty($cb_flows[$flow])) { return $data; } foreach ($cb_flows[$flow] as $step) { if (is_callable($step)) { $data = $step($data) ?? $data; } } return $data; } function cb_trigger_flow_from_event(string $event, array $data = []): void { cb_flow_run($event, $data); } function cb_event_and_flow(string $event, array $data = []): void { cb_event($event, $data); cb_flow_run($event, $data); } cb_flow_register('invoice.created', function ($data) { cb_log('INVOICE CREATED', $data); cb_event('invoice.created', $data); return $data; }); cb_flow_register('invoice.paid', function ($data) { cb_log('INVOICE PAID', $data); cb_event('invoice.paid', $data); return $data; }); function cb_invoice_created(array $data): void { cb_flow_run('invoice.created', $data); } function cb_invoice_paid(array $data): void { cb_flow_run('invoice.paid', $data); } function cb_dispatch(string $event, array $data = []): void { // 1. log event cb_log('DISPATCH', ['event' => $event, 'data' => $data]); // 2. run event listeners cb_event($event, $data); // 3. run automation flow cb_flow_run($event, $data); } function cb_system_check(): array { return [ 'events' => function_exists('cb_event'), 'flows' => function_exists('cb_flow_run'), 'log' => function_exists('cb_log'), 'dispatch' => function_exists('cb_dispatch'), ]; } function cb_fire(string $event, array $data = []): void { cb_log('FIRE', ['event' => $event, 'data' => $data]); cb_event($event, $data); cb_flow_run($event, $data); } /* |-------------------------------------------------------------------------- | CB CRM PUBLIC API CONTRACT |-------------------------------------------------------------------------- */ function cb(string $event, array $data = []): void { cb_log('CB CALL', ['event' => $event, 'data' => $data]); cb_event($event, $data); cb_flow_run($event, $data); } /* |-------------------------------------------------------------------------- | CB CRM SAFETY GUARDS |-------------------------------------------------------------------------- */ function cb_safe_call(callable $fn, array $data = []) { try { return $fn($data); } catch (\Throwable $e) { cb_log('ERROR', [ 'message' => $e->getMessage() ]); return $data; } } /* |-------------------------------------------------------------------------- | CB CRM FINAL CORE WRAPPER |-------------------------------------------------------------------------- */ function cb_run(string $event, array $data = []) { cb_log('RUN', ['event' => $event, 'data' => $data]); return cb_safe_call(function ($payload) use ($event) { cb_event($event, $payload); cb_flow_run($event, $payload); return $payload; }, $data); } /* |-------------------------------------------------------------------------- | CB CRM EVENT ALIASES |-------------------------------------------------------------------------- */ function cb_emit(string $event, array $data = []) { return cb_run($event, $data); } function cb_exec(string $event, array $data = []) { return cb_run($event, $data); } /* |-------------------------------------------------------------------------- | CB CRM DEFAULT FLOWS |-------------------------------------------------------------------------- */ cb_flow_register('log', function ($data) { cb_log('FLOW LOG', $data); return $data; }); cb_flow_register('error', function ($data) { cb_log('FLOW ERROR', $data); return $data; }); /* |-------------------------------------------------------------------------- | CB CRM BOOT STRAP FINAL HOOK |-------------------------------------------------------------------------- */ function cb_bootstrap_actions(): void { // ensure core flows exist cb_flow_register('system.boot', function ($data) { cb_log('SYSTEM BOOT', $data); return $data; }); cb_flow_run('system.boot', [ 'status' => 'ok' ]); } // auto-run on include cb_bootstrap_actions(); /* |-------------------------------------------------------------------------- | CB CRM HARDENING LAYER |-------------------------------------------------------------------------- */ global $cb_booted; if (!isset($cb_booted)) { $cb_booted = false; } function cb_boot_once(): void { global $cb_booted; if ($cb_booted) { return; } $cb_booted = true; cb_flow_run('system.boot', [ 'status' => 'booted' ]); } // override bootstrap to safe version cb_boot_once(); /* |-------------------------------------------------------------------------- | CB CRM NORMALIZED ENTRY FLOW |-------------------------------------------------------------------------- */ function cb_init(): void { cb_boot_once(); cb_log('INIT', [ 'status' => 'initialized' ]); } // auto-init on include cb_init(); /* |-------------------------------------------------------------------------- | CB CRM FINAL PUBLIC API |-------------------------------------------------------------------------- */ function cb_api(string $event, array $data = []) { cb_log('API CALL', [ 'event' => $event, 'data' => $data ]); cb_run($event, $data); } /* |-------------------------------------------------------------------------- | CB CRM EVENT TRACE LAYER |-------------------------------------------------------------------------- */ function cb_trace(string $label, array $data = []): void { cb_log('TRACE: ' . $label, $data); } /* |-------------------------------------------------------------------------- | CB CRM FINAL RUNTIME LOCK |-------------------------------------------------------------------------- */ function cb_runtime(): array { return [ 'booted' => true, 'events' => function_exists('cb_event'), 'flows' => function_exists('cb_flow_run'), 'api' => function_exists('cb_api'), ]; } /* |-------------------------------------------------------------------------- | CB CRM FINAL ENTRY CONTRACT |-------------------------------------------------------------------------- */ function cb_call(string $event, array $data = []) { cb_trace('CALL', ['event' => $event]); return cb_run($event, $data); } /* |-------------------------------------------------------------------------- | CB CRM STABILITY FREEZE LAYER |-------------------------------------------------------------------------- */ function cb_freeze(): void { static $frozen = false; if ($frozen) { return; } $frozen = true; cb_trace('FREEZE', [ 'status' => 'runtime locked' ]); } cb_freeze(); /* |-------------------------------------------------------------------------- | CB CRM SAFE PUBLIC WRAPPER |-------------------------------------------------------------------------- */ function cb_fire_event(string $event, array $data = []) { cb_call($event, $data); } function cb_action(string $event, array $data = []) { cb_call($event, $data); } /* |-------------------------------------------------------------------------- | CB CRM SELF DIAGNOSTIC LAYER |-------------------------------------------------------------------------- */ function cb_self_check(): void { $status = [ 'log' => function_exists('cb_log'), 'event' => function_exists('cb_event'), 'flow' => function_exists('cb_flow_run'), 'api' => function_exists('cb_api'), 'runtime' => function_exists('cb_runtime'), ]; cb_trace('SELF CHECK', $status); } cb_self_check(); /* |-------------------------------------------------------------------------- | CB CRM FINAL PUBLIC SURFACE (LOCKED API) |-------------------------------------------------------------------------- */ function cb(string $event, array $data = []) { cb_trace('CB', ['event' => $event]); return cb_call($event, $data); } /* |-------------------------------------------------------------------------- | CB CRM FINAL STABILIZATION LAYER |-------------------------------------------------------------------------- */ global $cb_initialized; if (!isset($cb_initialized)) { $cb_initialized = false; } function cb_initialize(): void { global $cb_initialized; if ($cb_initialized) { return; } $cb_initialized = true; cb_trace('INITIALIZE', [ 'status' => 'ok' ]); } cb_initialize(); /* |-------------------------------------------------------------------------- | CB CRM CORE LOCK (FINAL SAFETY LAYER) |-------------------------------------------------------------------------- */ function cb_lock(): void { static $locked = false; if ($locked) { return; } $locked = true; cb_trace('LOCK', [ 'status' => 'runtime locked' ]); } cb_lock(); CB CRM
Loading...