Last active
January 31, 2025 12:47
-
-
Save Sairahcaz/4116f87c3707ffbae607c3052f238c79 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// if you wanna use defer in a package where it may only be optional | |
// or you don't know or want to mess with if defer is already supported | |
// also swoole compatible if you get this error: | |
// Symfony\Component\ErrorHandler\Error\FatalError: Uncaught Swoole\Error: API must be called in the coroutine | |
if (! function_exists('safeDefer')) { | |
/** | |
* Safe Defer with fallback and swoole compatible. | |
*/ | |
function safeDefer(?callable $callback = null, ?string $name = null, bool $always = false): void | |
{ | |
if (function_exists('Illuminate\Support\defer')) { | |
Illuminate\Support\defer(...func_get_args()); | |
} else { | |
$callback(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment