Skip to content

Commit

Permalink
Add auto-register the container.
Browse files Browse the repository at this point in the history
It helps get rid of errors related to different $container instances after autowiring.
  • Loading branch information
renakdup committed Mar 18, 2024
1 parent f75877d commit 7246acd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
62 changes: 32 additions & 30 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Author Email: [email protected]
* Author Site: https://wp-yoda.com/en/
*
* Version: 0.2.3
* Version: 0.2.4
* Source Code: https://github.com/renakdup/simple-php-dic
*
* Licence: MIT License
Expand Down Expand Up @@ -95,6 +95,14 @@ class Container implements ContainerInterface {
*/
protected array $reflection_cache = [];

public function __construct() {
// Auto-register the container
$this->resolved = [
self::class => $this,
ContainerInterface::class => $this,
];
}

/**
* Set service to the container. Allows to set configurable services
* using factory "function () {}" as passed service.
Expand Down Expand Up @@ -139,9 +147,7 @@ public function has( string $id ): bool {
*/
public function make( string $id ): object {
if ( ! class_exists( $id ) ) {
$message = "Service '{$id}' could not be resolved because class not exist.\n"
. "Stack trace: \n"
. $this->get_stack_trace();
$message = "Service '{$id}' could not be resolved because class not exist.";
throw new ContainerException( $message );
}

Expand Down Expand Up @@ -170,9 +176,7 @@ protected function resolve( string $id ) {
return $this->resolve_object( $id );
}

$message = "Service '{$id}' not found in the Container.\n"
. "Stack trace: \n"
. $this->get_stack_trace();
$message = "Service '{$id}' not found in the Container.";
throw new ContainerNotFoundException( $message );
}

Expand Down Expand Up @@ -245,32 +249,30 @@ protected function resolve_parameter( ReflectionParameter $param ) {
}

// @phpstan-ignore-next-line - Cannot call method getName() on ReflectionClass|null.
$message = "Parameter '{$param->getName()}' of '{$param->getDeclaringClass()->getName()}' cannot be resolved.\n" .
"Stack trace: \n" .
$this->get_stack_trace();
$message = "Parameter '{$param->getName()}' of '{$param->getDeclaringClass()->getName()}' cannot be resolved.";
throw new ContainerException( $message );
}

protected function get_stack_trace(): string {
$stackTraceArray = debug_backtrace();
$stackTraceString = '';

foreach ( $stackTraceArray as $item ) {
$file = $item['file'] ?? '[internal function]';
$line = $item['line'] ?? '';
$function = $item['function'] ?? ''; // @phpstan-ignore-line - 'function' on array always exists and is not nullable.
$class = $item['class'] ?? '';
$type = $item['type'] ?? '';

$stackTraceString .= "{$file}({$line}): ";
if ( ! empty( $class ) ) {
$stackTraceString .= "{$class}{$type}";
}
$stackTraceString .= "{$function}()\n";
}

return $stackTraceString;
}
// protected function get_stack_trace(): string {
// $stackTraceArray = debug_backtrace();
// $stackTraceString = '';
//
// foreach ( $stackTraceArray as $item ) {
// $file = $item['file'] ?? '[internal function]';
// $line = $item['line'] ?? '';
// $function = $item['function'] ?? ''; // @phpstan-ignore-line - 'function' on array always exists and is not nullable.

Check failure on line 263 in src/Container.php

View workflow job for this annotation

GitHub Actions / php-ci

No error to ignore is reported on line 263.
// $class = $item['class'] ?? '';
// $type = $item['type'] ?? '';
//
// $stackTraceString .= "{$file}({$line}): ";
// if ( ! empty( $class ) ) {
// $stackTraceString .= "{$class}{$type}";
// }
// $stackTraceString .= "{$function}()\n";
// }
//
// return $stackTraceString;
// }
}

class ContainerNotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface {}
Expand Down
12 changes: 10 additions & 2 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ public function test_get__create_not_bound_service() {
self::assertEquals( new SimpleClass(), $this->container->get( SimpleClass::class ) );
}

public function test_get__check_singleton_for_not_bounded() {
public function test_get__singleton_check_for_not_bounded() {
self::assertSame(
$this->container->get( SimpleClass::class ),
$this->container->get( SimpleClass::class )
);
}

public function test_get__check_changing_singleton_property() {
public function test_get__singleton_check_changing_property() {
$this->container->set( $name = 'service', function () {
$obj = new stdClass();
$obj->title = 'first title';
Expand All @@ -130,6 +130,14 @@ public function test_get__singleton_for_resolved_child_dependencies() {
);
}

public function test_get__singleton_autoregister_container() {
$obj = $this->container->get( Container::class );
$obj2 = $this->container->get( ContainerInterface::class );

self::assertSame( $this->container, $obj );
self::assertSame( $this->container, $obj2 );
}

public function test_get__autowiring() {
$expected = new SimpleClass();
$this->container->set( $name = SimpleClass::class, SimpleClass::class );
Expand Down

0 comments on commit 7246acd

Please sign in to comment.