mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-03-19 13:24:51 +08:00
Add skip helper function for calling events
This commit is contained in:
parent
c27ed8b0b4
commit
a709221223
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace StaticPHP\DI;
|
||||
|
||||
use DI\Container;
|
||||
use StaticPHP\Exception\SkipException;
|
||||
use StaticPHP\Exception\SPCInternalException;
|
||||
|
||||
/**
|
||||
@ -92,7 +93,12 @@ readonly class CallbackInvoker
|
||||
);
|
||||
}
|
||||
|
||||
return $callback(...$args);
|
||||
try {
|
||||
return $callback(...$args);
|
||||
} catch (SkipException $e) {
|
||||
logger()->debug("Skipped invocation: {$e->getMessage()}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
7
src/StaticPHP/Exception/SkipException.php
Normal file
7
src/StaticPHP/Exception/SkipException.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace StaticPHP\Exception;
|
||||
|
||||
class SkipException extends SPCException {}
|
||||
@ -331,6 +331,20 @@ function spc_container(): DI\Container
|
||||
return \StaticPHP\DI\ApplicationContext::getContainer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Skip the current operation if the condition is true.
|
||||
* You should ALWAYS use this function inside an attribute callback.
|
||||
*
|
||||
* @param bool $condition Condition to evaluate
|
||||
* @param string $message Optional message for the skip exception
|
||||
*/
|
||||
function spc_skip_if(bool $condition, string $message = ''): void
|
||||
{
|
||||
if ($condition) {
|
||||
throw new StaticPHP\Exception\SkipException($message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse extension list from string, replace alias and filter internal extensions.
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user