2323class 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