-
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.
Add Container::make() method that allows resolve service by its name.…
… It returns a new instance of service every time. - Add method's descriptions of Container::class. - Add new tests. - Updated documentation.
- Loading branch information
Showing
11 changed files
with
209 additions
and
37 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
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.1 | ||
* Version: 0.2.2 | ||
* Source Code: https://github.com/renakdup/simple-php-dic | ||
* | ||
* Licence: MIT License | ||
|
@@ -90,6 +90,9 @@ class Container implements ContainerInterface { | |
protected array $resolved = []; | ||
|
||
/** | ||
* Set service to the container. Allows to set configurable services | ||
* using factory "function () {}" as passed service. | ||
* | ||
* @param mixed $service | ||
*/ | ||
public function set( string $id, $service ): void { | ||
|
@@ -120,8 +123,25 @@ public function has( string $id ): bool { | |
} | ||
|
||
/** | ||
* @param string $id | ||
* Resolves service by its name. It returns a new instance of service every time, but the constructor's | ||
* dependencies will not instantiate every time. If dependencies were resolved before | ||
* then they will be passed as resolved dependencies. | ||
* | ||
* @throws ContainerExceptionInterface | ||
* @throws NotFoundExceptionInterface | ||
*/ | ||
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(); | ||
throw new ContainerException( $message ); | ||
} | ||
|
||
return $this->resolve_object( $id ); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
* @throws ContainerExceptionInterface | ||
* @throws NotFoundExceptionInterface | ||
|
@@ -159,7 +179,6 @@ protected function resolve( string $id ) { | |
protected function resolve_object( string $service ): object { | ||
try { | ||
$reflected_class = new ReflectionClass( $service ); | ||
|
||
$constructor = $reflected_class->getConstructor(); | ||
|
||
if ( ! $constructor ) { | ||
|
@@ -172,10 +191,10 @@ protected function resolve_object( string $service ): object { | |
return new $service(); | ||
} | ||
|
||
$constructor_args = []; | ||
$resolved_params = []; | ||
foreach ( $params as $param ) { | ||
if ( $param_class = $param->getClass() ) { | ||
$constructor_args[] = $this->get( $param_class->getName() ); | ||
$resolved_params[] = $this->get( $param_class->getName() ); | ||
continue; | ||
} | ||
|
||
|
@@ -188,18 +207,17 @@ protected function resolve_object( string $service ): object { | |
throw new ContainerException( $message ); | ||
} | ||
|
||
$constructor_args[] = $default_value; | ||
$resolved_params[] = $default_value; | ||
} | ||
} catch ( ReflectionException $e ) { | ||
throw new ContainerException( | ||
"Service '{$service}' could not be resolved due the reflection issue:\n '" . | ||
$e->getMessage() . "'\n" . | ||
"Service '{$service}' could not be resolved due the reflection issue: '" . $e->getMessage() . "'\n" . | ||
"Stack trace: \n" . | ||
$e->getTraceAsString() | ||
); | ||
} | ||
|
||
return new $service( ...$constructor_args ); | ||
return new $service( ...$resolved_params ); | ||
} | ||
|
||
protected function get_stack_trace(): string { | ||
|
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace PisarevskiiTests\SimpleDIC\Assets; | ||
|
||
abstract class AbstractClass { | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace PisarevskiiTests\SimpleDIC\Assets; | ||
|
||
class ChildClass extends ParentClass { | ||
|
||
} |
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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace PisarevskiiTests\SimpleDIC\Assets; | ||
|
||
class ClassWithConstructorSupertypes { | ||
|
||
private ParentClass $parent_class; | ||
private AbstractClass $abstract_class; | ||
private SomeInterface $some; | ||
|
||
public function __construct( ParentClass $parent_class, AbstractClass $abstract_class, SomeInterface $some ) { | ||
$this->parent_class = $parent_class; | ||
$this->abstract_class = $abstract_class; | ||
$this->some = $some; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace PisarevskiiTests\SimpleDIC\Assets; | ||
|
||
class ParentClass { | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace PisarevskiiTests\SimpleDIC\Assets; | ||
|
||
interface SomeInterface { | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace PisarevskiiTests\SimpleDIC\Assets; | ||
|
||
class UseAbstractClass extends AbstractClass { | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace PisarevskiiTests\SimpleDIC\Assets; | ||
|
||
class UseInterfaceClass implements SomeInterface { | ||
|
||
} |
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