-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It helps get rid of errors related to different $container instances after autowiring.
- Loading branch information
Showing
2 changed files
with
42 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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. | ||
|
@@ -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 ); | ||
} | ||
|
||
|
@@ -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 ); | ||
} | ||
|
||
|
@@ -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. | ||
// $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 {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters