-
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.
Merge pull request #4 from renakdup/php-dic-remove-interfaces
Package prepared for composer usage - Remove PSR11 interfaces. - Container returns just Exceptions. - Renamed namespace from Pisarevskii\ to Renakdup.
- Loading branch information
Showing
19 changed files
with
133 additions
and
181 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
|
||
d.run74: | ||
php.connect74: | ||
docker run --rm -it -v "${PWD}":/usr/src/myapp -w /usr/src/myapp pimlab/composer:2.0.0-alpha3-php7.4 sh | ||
|
||
|
||
d.run82: | ||
php.connect82: | ||
docker run --rm -it -v "${PWD}":/usr/src/myapp -w /usr/src/myapp shopware/development:8.2-composer-2 bash |
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
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 |
---|---|---|
@@ -1,25 +1,25 @@ | ||
<?php // phpcs:ignoreFile | ||
/** | ||
* Simple PHP DIC - DI Container in one file. | ||
* Simple PHP DIC - DI Container in one file for WordPress. | ||
* Supports autowiring and allows you to easily use it in your simple PHP applications and | ||
* especially convenient for WordPress plugins and themes. | ||
* | ||
* Author: Andrei Pisarevskii | ||
* Author Email: [email protected] | ||
* Author Site: https://wp-yoda.com/en/ | ||
* | ||
* Version: 0.2.6 | ||
* Version: 1.0 | ||
* Source Code: https://github.com/renakdup/simple-php-dic | ||
* | ||
* Licence: MIT License | ||
*/ | ||
|
||
declare( strict_types=1 ); | ||
|
||
namespace Pisarevskii\SimpleDIC; | ||
namespace Renakdup\SimpleDIC; | ||
|
||
use Closure; | ||
use InvalidArgumentException; | ||
use Exception; | ||
use ReflectionClass; | ||
use ReflectionException; | ||
use ReflectionNamedType; | ||
|
@@ -29,58 +29,7 @@ | |
use function class_exists; | ||
use function is_string; | ||
|
||
######## PSR11 2.0 interfaces ######### | ||
# If you want to support PSR11, then remove 3 interfaces below | ||
# (ContainerInterface, ContainerExceptionInterface, NotFoundExceptionInterface) | ||
# and import PSR11 interfaces in this file: | ||
# ----- | ||
# use Psr\Container\ContainerExceptionInterface; | ||
# use Psr\Container\ContainerInterface; | ||
# use Psr\Container\NotFoundExceptionInterface; | ||
############################### | ||
interface ContainerInterface { | ||
/** | ||
* Finds an entry of the container by its identifier and returns it. | ||
* | ||
* @param string $id Identifier of the entry to look for. | ||
* | ||
* @return mixed Entry. | ||
* | ||
* @throws ContainerExceptionInterface Error while retrieving the entry. | ||
* @throws NotFoundExceptionInterface No entry was found for **this** identifier. | ||
*/ | ||
public function get( string $id ); | ||
|
||
/** | ||
* Returns true if the container can return an entry for the given identifier. | ||
* Returns false otherwise. | ||
* | ||
* `has($id)` returning true does not mean that `get($id)` will not throw an exception. | ||
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. | ||
* | ||
* @param string $id Identifier of the entry to look for. | ||
* | ||
* @return bool | ||
*/ | ||
public function has( string $id ): bool; | ||
} | ||
|
||
/** | ||
* Base interface representing a generic exception in a container. | ||
*/ | ||
interface ContainerExceptionInterface extends \Throwable {} | ||
|
||
/** | ||
* No entry was found in the container. | ||
*/ | ||
interface NotFoundExceptionInterface extends ContainerExceptionInterface {} | ||
######## PSR11 interfaces - END ######### | ||
|
||
|
||
############################### | ||
# Simple DIC code | ||
############################### | ||
class Container implements ContainerInterface { | ||
class Container { | ||
/** | ||
* @var mixed[] | ||
*/ | ||
|
@@ -99,8 +48,7 @@ class Container implements ContainerInterface { | |
public function __construct() { | ||
// Auto-register the container | ||
$this->resolved = [ | ||
self::class => $this, | ||
ContainerInterface::class => $this, | ||
self::class => $this, | ||
]; | ||
} | ||
|
||
|
@@ -117,7 +65,14 @@ public function set( string $id, $service ): void { | |
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* Finds an entry of the container by its identifier and returns it. | ||
* | ||
* @param string $id Identifier of the entry to look for. | ||
* | ||
* @return mixed Entry. | ||
* | ||
* @throws Exception Error while retrieving the entry. | ||
* || No entry was found for **this** identifier. | ||
*/ | ||
public function get( string $id ) { | ||
if ( isset( $this->resolved[ $id ] ) || array_key_exists( $id, $this->resolved ) ) { | ||
|
@@ -132,7 +87,15 @@ public function get( string $id ) { | |
} | ||
|
||
/** | ||
* @inheritdoc | ||
* Returns true if the container can return an entry for the given identifier. | ||
* Returns false otherwise. | ||
* | ||
* `has($id)` returning true does not mean that `get($id)` will not throw an exception. | ||
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. | ||
* | ||
* @param string $id Identifier of the entry to look for. | ||
* | ||
* @return bool | ||
*/ | ||
public function has( string $id ): bool { | ||
return array_key_exists( $id, $this->services ); | ||
|
@@ -143,22 +106,20 @@ public function has( string $id ): bool { | |
* dependencies will not instantiate every time. If dependencies were resolved before | ||
* then they will be passed as resolved dependencies. | ||
* | ||
* @throws ContainerExceptionInterface | ||
* @throws NotFoundExceptionInterface | ||
* @throws Exception | ||
*/ | ||
public function make( string $id ): object { | ||
if ( ! class_exists( $id ) ) { | ||
$message = "Service `{$id}` could not be resolved because class not exist."; | ||
throw new ContainerException( $message ); | ||
throw new Exception( $message ); | ||
} | ||
|
||
return $this->resolve_object( $id ); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
* @throws ContainerExceptionInterface | ||
* @throws NotFoundExceptionInterface | ||
* @throws Exception | ||
*/ | ||
protected function resolve( string $id ) { | ||
if ( $this->has( $id ) ) { | ||
|
@@ -177,15 +138,14 @@ protected function resolve( string $id ) { | |
return $this->resolve_object( $id ); | ||
} | ||
|
||
throw new ContainerNotFoundException( "Service `{$id}` not found in the Container." ); | ||
throw new Exception( "Service `{$id}` not found in the Container." ); | ||
} | ||
|
||
/** | ||
* @param class-string $service | ||
* | ||
* @return object | ||
* @throws ContainerExceptionInterface | ||
* @throws NotFoundExceptionInterface | ||
* @throws Exception | ||
*/ | ||
protected function resolve_object( string $service ): object { | ||
try { | ||
|
@@ -206,7 +166,7 @@ protected function resolve_object( string $service ): object { | |
$resolved_params = $this->resolve_parameters( $params ); | ||
|
||
} catch ( ReflectionException $e ) { | ||
throw new ContainerException( | ||
throw new Exception( | ||
"Service `{$service}` could not be resolved due the reflection issue: `{$e->getMessage()}`" | ||
); | ||
} | ||
|
@@ -218,7 +178,7 @@ protected function resolve_object( string $service ): object { | |
* @param ReflectionParameter[] $params | ||
* | ||
* @return mixed[] | ||
* @throws ContainerExceptionInterface | ||
* @throws Exception | ||
* @throws ReflectionException | ||
*/ | ||
protected function resolve_parameters( array $params ): array { | ||
|
@@ -234,7 +194,7 @@ protected function resolve_parameters( array $params ): array { | |
* @param ReflectionParameter $param | ||
* | ||
* @return mixed|object | ||
* @throws ContainerExceptionInterface | ||
* @throws Exception | ||
* @throws ReflectionException | ||
*/ | ||
protected function resolve_parameter( ReflectionParameter $param ) { | ||
|
@@ -250,7 +210,7 @@ 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()}` can't be resolved."; | ||
throw new ContainerException( $message ); | ||
throw new Exception( $message ); | ||
} | ||
|
||
protected function get_stack_trace(): string { | ||
|
@@ -274,8 +234,3 @@ protected function get_stack_trace(): string { | |
return $stackTraceString; | ||
} | ||
} | ||
|
||
class ContainerNotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface {} | ||
|
||
class ContainerException extends InvalidArgumentException implements ContainerExceptionInterface {} | ||
|
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
Oops, something went wrong.