Skip to content

Commit 54f7d81

Browse files
committed
feat(Hook): Improved and standardized interfaces
1 parent e8a703a commit 54f7d81

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

Hook/Hookable.php

+10-13
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@
88

99
namespace XWP\Contracts\Hook;
1010

11+
use ReflectionClass;
12+
use ReflectionMethod;
13+
1114
/**
1215
* Hook decorator functionality.
1316
*
14-
* @template T
15-
*
1617
* ! Global properties shared among hooks and handlers
1718
*
19+
* @template THndlr of object
20+
* @template TRflct of ReflectionClass<THndlr>|ReflectionMethod
21+
*
1822
* @property-read string $tag Hook name. Can use the `vsprinf` format in combination with `$modifiers`.
1923
* @property-read array|int|string|callable $priority Hook priority. Can be a number, callable, or a string. Strings are treated as filters, which will be applied to the default priority.
2024
* @property-read int $context Context bitmask determining where the hook can be invoked.
21-
* @property-read string|false $requires Prerequisite hook that must be invoked before this hook. Handler classname, or Classname, method array.
25+
* @property-read string|array|false $requires Prerequisite hook that must be invoked before this hook. Handler classname, or Classname, method array.
2226
* @property-read string|array|false $modifiers Replacement pairs for the tag name.
2327
* @property-read int $real_priority Actual priority of the hook.
2428
* @property-read array|callable|false $conditional Hook conditional. Callable which will be invoked to determine if the hook should be invoked.
29+
* @property-read TRflct $reflector Reflector instance.
2530
*/
2631
interface Hookable {
2732
/**
@@ -83,16 +88,8 @@ interface Hookable {
8388
/**
8489
* Set the reflector
8590
*
86-
* @param \Reflector $reflector Reflector instance.
87-
* @return static
88-
*/
89-
public function set_reflector( \Reflector $reflector ): static;
90-
91-
/**
92-
* Set the hook target.
93-
*
94-
* @param array|T $target Hook target.
91+
* @param TRflct $reflector Reflector instance.
9592
* @return static
9693
*/
97-
public function set_target( array|object $target ): static;
94+
public function set_reflector( ReflectionClass|ReflectionMethod $reflector ): static;
9895
}

Hook/Initializable.php

+26-8
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,43 @@
88

99
namespace XWP\Contracts\Hook;
1010

11+
use ReflectionClass;
12+
1113
/**
1214
* Handler interface.
1315
*
14-
* @template<T>
16+
* @template THndlr of object
17+
*
18+
* @property-read Initialize $strategy Invocation strategy.
19+
* @property-read class-string<THndlr> $classname Handler classname.
20+
* @property-read bool $initialized Indicates if the handler has been initialized.
21+
* @property THndlr|null $target Handler target.
1522
*
16-
* @property-read Initialize $strategy Invocation strategy.
17-
* @property-read class-string>T> $classname Handler classname.
18-
* @property-read bool $initialized Indicates if the handler has been initialized.
19-
* @property-read T|null $target Handler target.
20-
* @property-read \ReflectionClass<T> $reflector Reflector instance.
23+
* @extends Hookable<THndlr,ReflectionClass<THndlr>>
2124
*/
2225
interface Initializable extends Hookable {
2326
/**
2427
* Set the classname.
2528
*
26-
* @param class-string<T> $classname Handler classname.
29+
* @param class-string<THndlr> $classname Handler classname.
2730
* @return static
2831
*/
29-
public function set_classname( string $classname ): static;
32+
public function with_classname( string $classname ): static;
33+
34+
/**
35+
* Set the handler instance.
36+
*
37+
* @param THndlr $instance Handler instance.
38+
* @return static
39+
*/
40+
public function with_target( object $instance ): static;
41+
42+
/**
43+
* Get the handler instance.
44+
*
45+
* @return THndlr
46+
*/
47+
public function get_target(): ?object;
3048

3149
/**
3250
* Initializes the handler.

Hook/Invokable.php

+19-8
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,36 @@
88

99
namespace XWP\Contracts\Hook;
1010

11+
use ReflectionMethod;
12+
1113
/**
1214
* Invokable interface. Used by actions and filters.
1315
*
14-
* @template T
16+
* @template THndlr of object
17+
*
18+
* @property-read Invoke $strategy Invocation strategy.
19+
* @property-read Initializable<THndlr> $handler Handler instance.
20+
* @property-read bool $invoked Flag indicating if the hook has been invoked.
21+
* @property-read int $args Number of arguments the hook accepts.
1522
*
16-
* @property-read Invoke $strategy Invocation strategy.
17-
* @property-read Initializable $handler Handler instance.
18-
* @property-read bool $invoked Flag indicating if the hook has been invoked.
19-
* @property-read int $args Number of arguments the hook accepts.
20-
* @property-read \ReflectionMethod<T> $reflector Reflector instance.
23+
* @extends Hookable<THndlr,ReflectionMethod>
2124
*/
2225
interface Invokable extends Hookable {
2326
/**
2427
* Set the hook handler.
2528
*
26-
* @param Initializable<T> $handler Handler instance.
29+
* @param Initializable<THndlr> $handler Handler instance.
30+
* @return static
31+
*/
32+
public function with_handler( Initializable $handler ): static;
33+
34+
/**
35+
* Set the hook target.
36+
*
37+
* @param string $method Method name.
2738
* @return static
2839
*/
29-
public function set_handler( Initializable $handler ): static;
40+
public function with_target( string $method ): static;
3041

3142
/**
3243
* Check if the hook can be invoked.

0 commit comments

Comments
 (0)