1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace crmeb\utils;
- use think\helper\Str;
- class Hook
- {
-
- protected $namespace;
-
- protected $prefix;
-
- public function __construct(string $namespace, string $prefix = null)
- {
- $this->namespace = $namespace;
- if ($prefix) {
- $this->prefix = $prefix;
- }
- }
-
- public function listen(string $hookName, ...$arguments)
- {
- if (class_exists($this->namespace)) {
- $handle = app()->make($this->namespace);
- $hookName = Str::studly(($this->prefix ?: '') . ucfirst($hookName));
- if (method_exists($handle, $hookName)) {
- try {
- return call_user_func_array([$handle, $hookName], $arguments);
- } catch (\Throwable $e) {
- }
- }
- }
- return false;
- }
- }
|