From bb8adb96b021598e6ed5a8e9a59b8d5d7ef0ee87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez?= Date: Tue, 4 Jun 2024 13:11:17 +0200 Subject: [PATCH 1/2] refactor: dependency injection for hooks --- functions.php | 2 +- src/attributes/class-hook.php | 24 +++++------------------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/functions.php b/functions.php index 0ce9775..40c7e4f 100644 --- a/functions.php +++ b/functions.php @@ -27,7 +27,7 @@ $container = $builder->build(); // Register WordPress Hooks. -( new Hook( $container ) )->register_hooks(); +( $container->get( Hook::class ) )->register_hooks(); // Register CLI Commands. ( new CLI( $container ) )->register_commands(); diff --git a/src/attributes/class-hook.php b/src/attributes/class-hook.php index b4048eb..afec48b 100644 --- a/src/attributes/class-hook.php +++ b/src/attributes/class-hook.php @@ -7,6 +7,7 @@ namespace Somoscuatro\Starter_Theme\Attributes; +use DI\Attribute\Inject; use DI\Container; use ReflectionAttribute; @@ -22,32 +23,17 @@ class Hook { * * @var Container */ + #[Inject] private Container $container; - /** - * The Names of the Classes That Contain Hooks Handlers. - * - * @var array - */ - private array $hooked_classes = array(); - - /** - * Class Constructor. - * - * @param Container $container The PHP DI Container. - */ - public function __construct( Container $container ) { - $this->container = $container; - $this->hooked_classes = require __DIR__ . '/hooked-classes.php'; - } - /** * Registers Hooks. */ public function register_hooks(): void { - $instances = array(); + $hooked_classes = require __DIR__ . '/hooked-classes.php'; + $instances = array(); - foreach ( $this->hooked_classes as $class_name ) { + foreach ( $hooked_classes as $class_name ) { $reflection_class = new ReflectionClass( $class_name ); foreach ( $reflection_class->getMethods() as $method ) { From 4afab632d7b18aa7ae130793e6d2b145e929114b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez?= Date: Tue, 4 Jun 2024 13:12:01 +0200 Subject: [PATCH 2/2] refactor: dependency injection for cli commands --- functions.php | 2 +- src/cli/class-cli.php | 11 ++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/functions.php b/functions.php index 40c7e4f..9f6a151 100644 --- a/functions.php +++ b/functions.php @@ -30,4 +30,4 @@ ( $container->get( Hook::class ) )->register_hooks(); // Register CLI Commands. -( new CLI( $container ) )->register_commands(); +( $container->get( CLI::class ) )->register_commands(); diff --git a/src/cli/class-cli.php b/src/cli/class-cli.php index 32ac2e2..0c45e22 100644 --- a/src/cli/class-cli.php +++ b/src/cli/class-cli.php @@ -9,6 +9,7 @@ namespace Somoscuatro\Starter_Theme\CLI; +use DI\Attribute\Inject; use DI\Container; /** @@ -21,17 +22,9 @@ class CLI { * * @var Container */ + #[Inject] private Container $container; - /** - * Class Constructor. - * - * @param Container $container The PHP DI Container. - */ - public function __construct( Container $container ) { - $this->container = $container; - } - /** * Registers CLI Commands. */