apricity / handler
一个PHP类,用于解析和执行各种类型的处理器,包括格式为 'Class@method' 的字符串,可调用的函数和包含类名和方法名的数组。
Requires
- php: >=8.0
- apricity/micro-cache: ^1.10
Requires (Dev)
- phpunit/phpunit: ^11.1
README
Handler
类负责触发和解析处理器。处理器可以是格式为 Class@method
的字符串,callable
函数,包含类名和方法名的数组或包含 callable
函数的数组。该类确保处理器有效且可以使用提供的变量执行。
安装
composer require apricity/handler
目录
Handler::simpleTrigger
使用提供的变量执行给定的处理器。
此方法可以处理多种类型的处理器:包含类和方法的数组,可调用的函数或字符串。它尝试使用提供的变量执行处理器并返回结果。如果处理器无效或执行失败,则抛出 HandlerException
。
public static function simpleTrigger(array|callable|string $handler, array $vars = []): mixed
参数
array|callable|string $handler
: 要执行的处理器。- 可以是:
- 一个可调用的/闭包函数。
- 表示函数名的字符串。
- 包含单个字符串元素的数组,表示函数名。
- 包含类和方法的数组。
- 包含类名和方法的字符串数组。
- 可以是:
array $vars
: [可选] 要传递给处理器的变量。默认为空数组。
返回: mixed
- 处理器执行的结果。
抛出: HandlerException
- 如果处理器无效或执行失败。
示例
namespace Apricity; use HandlerException; class ExampleClass { public function exampleMethod($param1, $param2) { return "Example method executed with $param1 and $param2."; } } // Define a standalone function to use as a handler function exampleFunction($param1, $param2) { return "Example function executed with $param1 and $param2."; } try { $result = Handler::simpleTrigger(function($a, $b) { return $a + $b; }, [5, 3]); echo $result; // Outputs: 8 $result = Handler::simpleTrigger('strtolower', ['HELLO']); echo $result; // Outputs: hello // Using callable array [Class, method] $result = Handler::simpleTrigger([ExampleClass::class, 'exampleMethod'], ['value1', 'value2']); echo $result; // Outputs: Example method executed with value1 and value2. // Using callable array ["function"] $result = Handler::simpleTrigger(['exampleFunction'], ['value1', 'value2']); echo $result; // Outputs: Example function executed with value1 and value2. } catch (HandlerException $e) { echo 'Error: ' . $e->getMessage(); }
Handler::trigger
触发给定处理器与提供的变量的执行。
此方法首先解析处理器以确保其格式正确,然后使用 simpleTrigger 方法执行它。处理器可以是字符串、可调用的或数组。它返回处理器执行的结果。
public static function trigger(array|callable|string $handler, array $vars = []): mixed
参数
array|callable|string $handler
: 要执行的处理器。- 可以是:
- 格式为 "Class@method" 的字符串。
- 表示函数名的字符串。
- 包含单个字符串元素的数组,表示函数名。
- 包含类和方法的数组。
- 包含类名和方法的字符串数组。
- 可以是:
array $vars
: [可选] 要传递给处理器的变量。默认为空数组。
返回: mixed
- 处理器执行的结果。
抛出: HandlerException
- 如果处理器无效或执行失败。
示例
namespace Apricity; use HandlerException; class ExampleClass { public function exampleMethod($param1, $param2) { return "Example method executed with $param1 and $param2."; } } // Define a standalone function to use as a handler function exampleFunction($param1, $param2) { return "Example function executed with $param1 and $param2."; } try { // Example 1: Using 'Class@method' string format $result1 = Handler::trigger('ExampleClass@exampleMethod', ['value1', 'value2']); echo $result1; // Outputs: Example method executed with value1 and value2. // Example 2: Using callable array [Class, method] $result2 = Handler::trigger([ExampleClass::class, 'exampleMethod'], ['value1', 'value2']); echo $result2; // Outputs: Example method executed with value1 and value2. // Example 3: Using callable "function" $result3 = Handler::trigger('exampleFunction', ['value1', 'value2']); echo $result3; // Outputs: Example function executed with value1 and value2. // Example 4: Using callable array ["function"] $result4 = Handler::trigger(['exampleFunction'], ['value1', 'value2']); echo $result4; // Outputs: Example function executed with value1 and value2. } catch (HandlerException $e) { echo 'Error: ' . $e->getMessage(); }
Handler::parse
将给定的处理器解析为标准数组格式。
此方法接受各种格式的处理器并返回数组。它处理字符串、可调用的和数组,并将结果缓存以供将来使用。如果处理器无效,则抛出 HandlerException
。
public static function parse(array|callable|string $handler): array
参数
array|callable|string $handler
: 要执行的处理器。- 可以是:
- 格式为 "Class@method" 的字符串。
- 表示函数名的字符串。
- 包含单个字符串元素的数组,表示函数名。
- 包含类和方法的数组。
- 包含类名和方法的字符串数组。
- 可以是:
返回: array
- 解析后的处理器数组。
抛出: HandlerException
- 如果处理器无效或未找到。
示例
namespace Apricity; use HandlerException; class ExampleClass { public function exampleMethod() { return "Example method executed."; } } // Define a standalone function to use as a handler function exampleFunction() { return "Example function executed."; } try { // Example 1: Using 'Class@method' string format $parsedHandler1 = Handler::parse('ExampleClass@exampleMethod'); print_r($parsedHandler1); // Outputs: Array ( [0] => ExampleClass [1] => exampleMethod ) // Example 2: Using callable array [Class, method] $parsedHandler2 = Handler::parse([ExampleClass::class, 'exampleMethod']); print_r($parsedHandler2); // Outputs: Array ( [0] => ExampleClass [1] => exampleMethod ) // Example 3: Using callable function $parsedHandler3 = Handler::parse('exampleFunction'); print_r($parsedHandler3); // Outputs: Array ( [0] => exampleFunction ) // Example 4: Using callable ["function"] $parsedHandler4 = Handler::parse(['exampleFunction']); print_r($parsedHandler4); // Outputs: Array ( [0] => exampleFunction ) } catch (HandlerException $e) { echo 'Error: ' . $e->getMessage(); }
运行测试
composer test
贡献
我们欢迎社区的贡献!有关贡献的指南,请参阅 CONTRIBUTING.md 文件。
许可协议
本项目采用 BSD 3-Clause 许可协议 - 有关详细信息,请参阅 LICENSE 文件。
存储库已从 GitLab 迁移。