Skip to content

Commit 21a26e7

Browse files
committed
fix: Tweaks and bugfixes
1 parent fcba7bb commit 21a26e7

File tree

4 files changed

+97
-17
lines changed

4 files changed

+97
-17
lines changed

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Decorators/Handler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ class Handler extends Hook implements Can_Handle {
5959
/**
6060
* Container ID.
6161
*
62-
* @var string
62+
* @var ?string
6363
*/
64-
protected string $container_id;
64+
protected ?string $container_id;
6565

6666
/**
6767
* Is the handler hookable.

src/Handler_Factory.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace XWP\DI;
1010

11+
use ReflectionClass;
1112
use XWP\DI\Decorators\Handler;
1213
use XWP\DI\Interfaces\Can_Handle;
1314
use XWP\DI\Utils\Reflection;
@@ -18,6 +19,24 @@
1819
* @since 1.0.0
1920
*/
2021
class Handler_Factory {
22+
/**
23+
* Get a handler target classname from an instance.
24+
*
25+
* @param object $instance The handler instance.
26+
* @return string
27+
*/
28+
public static function get_target( object $instance ): string {
29+
if ( $instance instanceof Can_Handle ) {
30+
return $instance->classname;
31+
}
32+
33+
if ( $instance instanceof ReflectionClass ) {
34+
return $instance->getName();
35+
}
36+
37+
return $instance::class;
38+
}
39+
2140
/**
2241
* Create a handler from a classname.
2342
*
@@ -43,8 +62,8 @@ public static function from_classname( string $classname ): Can_Handle {
4362
*/
4463
public static function from_instance( object $instance, ?string $container = null ): Can_Handle {
4564
if ( Reflection::class_implements( $instance, Can_Handle::class ) ) {
46-
return $instance;
47-
}
65+
return $instance;
66+
}
4867

4968
$refl = Reflection::get_reflector( $instance );
5069
$handler = Reflection::get_decorator( $refl, Can_Handle::class )

src/Invoker.php

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
class Invoker {
2424
use Singleton_Ex;
2525

26+
/**
27+
* Is WP in debug mode.
28+
*
29+
* @var bool
30+
*/
31+
protected bool $debug = false;
32+
2633
/**
2734
* Handlers.
2835
*
@@ -31,6 +38,13 @@ class Invoker {
3138
*/
3239
protected array $handlers = array();
3340

41+
/**
42+
* Registered hooks.
43+
*
44+
* @var array<class-string,string>
45+
*/
46+
protected array $reg_hooks = array();
47+
3448
/**
3549
* Hooks.
3650
*
@@ -39,6 +53,22 @@ class Invoker {
3953
*/
4054
protected array $hooks = array();
4155

56+
/**
57+
* Constructor.
58+
*/
59+
protected function __construct() {
60+
$this->debug = \defined( 'WP_DEBUG' ) && WP_DEBUG;
61+
}
62+
63+
/**
64+
* Get debug info.
65+
*
66+
* @return array<class-string,string>
67+
*/
68+
public function debug_info(): array {
69+
return $this->reg_hooks;
70+
}
71+
4272
/**
4373
* Check if a handler is registered.
4474
*
@@ -61,6 +91,15 @@ public function get_handler( string $classname ): ?Can_Handle {
6191
return $this->has_handler( $classname ) ? $this->handlers[ $classname ] : null;
6292
}
6393

94+
/**
95+
* Get all handlers.
96+
*
97+
* @return array<class-string,Can_Handle<object>>
98+
*/
99+
public function all_handlers(): array {
100+
return $this->handlers;
101+
}
102+
64103
/**
65104
* Add a handler.
66105
*
@@ -70,10 +109,12 @@ public function get_handler( string $classname ): ?Can_Handle {
70109
* @return static
71110
*/
72111
public function add_handler( Can_Handle $handler, bool $clear = true ): static {
73-
$this->handlers[ $handler->classname ] = $handler;
112+
$cname = $handler->classname;
113+
114+
$this->handlers[ $cname ] = $handler;
74115

75116
if ( $clear ) {
76-
$this->hooks[ $handler->classname ] = array();
117+
$this->hooks[ $cname ] = array();
77118
}
78119

79120
return $this;
@@ -101,6 +142,15 @@ public function get_hooks( string $classname ): array {
101142
return $this->has_hooks( $classname ) ? $this->hooks[ $classname ] : array();
102143
}
103144

145+
/**
146+
* Get all hooks.
147+
*
148+
* @return array<class-string,array<string,array<int,Can_Invoke<object,Can_Handle<object>>>>>
149+
*/
150+
public function all_hooks(): array {
151+
return $this->hooks;
152+
}
153+
104154
/**
105155
* Register a module.
106156
*
@@ -170,12 +220,12 @@ public function register_handler( string $classname ): static {
170220
* @return static
171221
*/
172222
public function load_handler( object $instance, ?string $container = null ): static {
173-
$handler = Handler_Factory::from_instance( $instance, $container );
174-
175-
if ( $this->has_handler( $handler->classname ) ) {
223+
if ( $this->has_handler( Handler_Factory::get_target( $instance ) ) ) {
176224
return $this;
177225
}
178226

227+
$handler = Handler_Factory::from_instance( $instance, $container );
228+
179229
return $this
180230
->add_handler( $handler )
181231
->register_methods( $handler )
@@ -215,6 +265,10 @@ function () use ( $handler ) {
215265
protected function init_handler( Can_Handle $handler ): static {
216266
$handler->load();
217267

268+
if ( $this->debug ) {
269+
$this->reg_hooks[ $handler->classname ] ??= \current_action();
270+
}
271+
218272
return $this;
219273
}
220274

@@ -271,7 +325,14 @@ private function register_method( Can_Handle $handler, \ReflectionMethod $method
271325
*/
272326
protected function queue_methods( Can_Handle $handler ): static {
273327
if ( $handler->is_lazy() ) {
274-
\add_action( $handler->lazy_hook, array( $handler, 'lazy_load' ), -1, 0 );
328+
\add_action(
329+
$handler->lazy_hook,
330+
function () use ( $handler ) {
331+
$this->init_handler( $handler );
332+
},
333+
-1,
334+
0,
335+
);
275336
}
276337

277338
\add_action(

0 commit comments

Comments
 (0)