Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor dependency injector #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
( $container->get( CLI::class ) )->register_commands();
24 changes: 5 additions & 19 deletions src/attributes/class-hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Somoscuatro\Starter_Theme\Attributes;

use DI\Attribute\Inject;
use DI\Container;

use ReflectionAttribute;
Expand All @@ -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 ) {
Expand Down
11 changes: 2 additions & 9 deletions src/cli/class-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Somoscuatro\Starter_Theme\CLI;

use DI\Attribute\Inject;
use DI\Container;

/**
Expand All @@ -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.
*/
Expand Down
Loading