From 2461af92c743ac50a5bff3f54dabe3fa1d196221 Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Mon, 4 Aug 2025 13:49:35 +0200 Subject: [PATCH 1/7] Update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 832c559..dcdfc9a 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ or recursively instantiating them with *DependencyResolver* itself. ## Features - Based on PSR-11 -- PHP 8.0 ready (supports union type hints; see examples below) +- Supports PHP 8 (8.0, 8.1, 8.2, 8.3 and 8.4) — full support of union type hints, and other modern features. - PHP 7.1+ compatible - Recursive dependencies autowiring - Semver @@ -39,7 +39,7 @@ final class MyFancyService } } -// Construct service instance, providing dependencies in-place: +// Construct a service instance, providing dependencies in-place: $resolver = new DependencyResolver(); $service = $resolver->construct(MyFancyService::class, [ 'log' => function (string $priority, string $message) { From 98075dfeb2f13cd5ef1a173743bb08126b682295 Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Mon, 4 Aug 2025 14:19:06 +0200 Subject: [PATCH 2/7] Raise minimum PHP version to 8.0+ --- .github/workflows/test.yml | 2 +- README.md | 3 +-- composer.json | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4a39a96..90608f0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,7 +6,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: [ '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ] + php: [ '8.0', '8.1', '8.2', '8.3', '8.4' ] steps: - uses: actions/checkout@master diff --git a/README.md b/README.md index dcdfc9a..cace7de 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,7 @@ or recursively instantiating them with *DependencyResolver* itself. ## Features - Based on PSR-11 -- Supports PHP 8 (8.0, 8.1, 8.2, 8.3 and 8.4) — full support of union type hints, and other modern features. -- PHP 7.1+ compatible +- Supports modern PHP 8 features (up to PHP 8.4) — full support of union type hints, and others. - Recursive dependencies autowiring - Semver - Tests diff --git a/composer.json b/composer.json index a9b48c1..64b61b8 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "auto-wiring" ], "require": { - "php": "^7.1|^8.0", + "php": "^8.0", "psr/container": "^1.0|^2.0", "technically/null-container": "^1.0", "technically/callable-reflection": "^0.4.0" From 067e9fbdaa18ebd2824de9569bdb5680418e595d Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Mon, 4 Aug 2025 14:26:14 +0200 Subject: [PATCH 3/7] Update dependencies --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 64b61b8..00739ad 100644 --- a/composer.json +++ b/composer.json @@ -10,13 +10,13 @@ ], "require": { "php": "^8.0", - "psr/container": "^1.0|^2.0", - "technically/null-container": "^1.0", - "technically/callable-reflection": "^0.4.0" + "psr/container": "^2.0", + "technically/null-container": "^2.0", + "technically/callable-reflection": "^0.4.2" }, "require-dev": { "peridot-php/peridot": "^1.19", - "technically/array-container": "^1.0" + "technically/array-container": "^2.0" }, "license": "MIT", "authors": [ From 8d127628066a8638869a3303540055e9584e3903 Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Mon, 4 Aug 2025 14:26:24 +0200 Subject: [PATCH 4/7] Upgrade code to rely on PHP8 features --- src/DependencyResolver.php | 16 +++++++--------- src/Exceptions/CannotAutowireArgument.php | 2 +- .../CannotAutowireDependencyArgument.php | 4 ++-- src/Exceptions/ClassCannotBeInstantiated.php | 6 +++--- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/DependencyResolver.php b/src/DependencyResolver.php index 87aa19d..4125d37 100644 --- a/src/DependencyResolver.php +++ b/src/DependencyResolver.php @@ -16,10 +16,7 @@ final class DependencyResolver { - /** - * @var ContainerInterface - */ - private $container; + private ContainerInterface $container; public function __construct(?ContainerInterface $container = null) { @@ -28,14 +25,14 @@ public function __construct(?ContainerInterface $container = null) /** * @param string $className - * @return mixed|object|void + * @return mixed * * @throws InvalidArgumentException If class does not exist. * @throws ContainerExceptionInterface If error occurs while retrieving the existing entry from the container. * @throws ClassCannotBeInstantiated If class cannot be instantiated. * @throws CannotAutowireDependencyArgument If a dependency (of any nesting level) cannot be resolved. */ - public function resolve(string $className) + public function resolve(string $className): mixed { if (! class_exists($className) && ! interface_exists($className)) { throw new InvalidArgumentException("`{$className}` is not a valid class name."); @@ -51,11 +48,12 @@ public function resolve(string $className) /** * @param string $className * @param array $bindings + * @return mixed * * @throws ClassCannotBeInstantiated * @throws CannotAutowireDependencyArgument */ - public function construct(string $className, array $bindings = []) + public function construct(string $className, array $bindings = []): mixed { if (! class_exists($className) && ! interface_exists($className)) { throw new InvalidArgumentException("`{$className}` is not a valid class name."); @@ -84,7 +82,7 @@ public function construct(string $className, array $bindings = []) * @throws ArgumentCountError * @throws CannotAutowireArgument */ - public function call(callable $callable, array $bindings = []) + public function call(callable $callable, array $bindings = []): mixed { $reflection = CallableReflection::fromCallable($callable); @@ -130,7 +128,7 @@ private function resolveParameters(array $parameters, array $bindings = []): arr * * @throws CannotAutowireArgument */ - private function resolveParameter(ParameterReflection $parameter) + private function resolveParameter(ParameterReflection $parameter): mixed { foreach ($parameter->getTypes() as $type) { if ($type->isClassRequirement() && $this->container->has($class = $type->getClassRequirement())) { diff --git a/src/Exceptions/CannotAutowireArgument.php b/src/Exceptions/CannotAutowireArgument.php index 7e6e673..510c340 100644 --- a/src/Exceptions/CannotAutowireArgument.php +++ b/src/Exceptions/CannotAutowireArgument.php @@ -9,7 +9,7 @@ final class CannotAutowireArgument extends DependencyResolutionException /** * @var string */ - private $argumentName; + private string $argumentName; /** * @param string $argumentName diff --git a/src/Exceptions/CannotAutowireDependencyArgument.php b/src/Exceptions/CannotAutowireDependencyArgument.php index 6ef1f85..c1af508 100644 --- a/src/Exceptions/CannotAutowireDependencyArgument.php +++ b/src/Exceptions/CannotAutowireDependencyArgument.php @@ -9,12 +9,12 @@ final class CannotAutowireDependencyArgument extends DependencyResolutionExcepti /** * @var string */ - private $dependencyName; + private string $dependencyName; /** * @var string */ - private $argumentName; + private string $argumentName; /** * @param string $dependencyName diff --git a/src/Exceptions/ClassCannotBeInstantiated.php b/src/Exceptions/ClassCannotBeInstantiated.php index 0b11c90..ab485cb 100644 --- a/src/Exceptions/ClassCannotBeInstantiated.php +++ b/src/Exceptions/ClassCannotBeInstantiated.php @@ -5,9 +5,9 @@ final class ClassCannotBeInstantiated extends DependencyResolutionException { /** - * @var string + * @var class-string */ - private $className; + private string $className; public function __construct(string $className) { @@ -17,7 +17,7 @@ public function __construct(string $className) } /** - * @return string + * @return class-string */ public function getClassName(): string { From 216f37a5bdac887d4eb7e0bd988b67cb8684c36a Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Mon, 4 Aug 2025 14:39:27 +0200 Subject: [PATCH 5/7] Introduce DependencyResolver interface Useful for outer libraries to rely on the interface rather than on the concrete implementation itself, leaving the door open for user-land customizations. --- src/Contracts/DependencyResolver.php | 57 ++++++++++++++++++++++++++++ src/DependencyResolver.php | 18 +++++---- 2 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 src/Contracts/DependencyResolver.php diff --git a/src/Contracts/DependencyResolver.php b/src/Contracts/DependencyResolver.php new file mode 100644 index 0000000..7582f3b --- /dev/null +++ b/src/Contracts/DependencyResolver.php @@ -0,0 +1,57 @@ + $bindings + * @return mixed + * + * @throws ClassCannotBeInstantiated + * @throws CannotAutowireDependencyArgument + */ + public function construct(string $className, array $bindings = []): mixed; + + /** + * Call the given callable with its arguments automatically wired using the container state. + * + * @template T + * @param callable():T $callable + * @param array $bindings + * @return T + * + * @throws ArgumentCountError + * @throws CannotAutowireArgument + */ + public function call(callable $callable, array $bindings = []): mixed; +} \ No newline at end of file diff --git a/src/DependencyResolver.php b/src/DependencyResolver.php index 4125d37..9ec9f8b 100644 --- a/src/DependencyResolver.php +++ b/src/DependencyResolver.php @@ -8,13 +8,14 @@ use Psr\Container\ContainerInterface; use Technically\CallableReflection\CallableReflection; use Technically\CallableReflection\Parameters\ParameterReflection; +use Technically\DependencyResolver\Contracts\DependencyResolver as DependencyResolverInterface; use Technically\DependencyResolver\Exceptions\CannotAutowireArgument; use Technically\DependencyResolver\Exceptions\CannotAutowireDependencyArgument; use Technically\DependencyResolver\Exceptions\ClassCannotBeInstantiated; use Technically\DependencyResolver\Exceptions\DependencyResolutionException; use Technically\NullContainer\NullContainer; -final class DependencyResolver +final class DependencyResolver implements DependencyResolverInterface { private ContainerInterface $container; @@ -24,7 +25,7 @@ public function __construct(?ContainerInterface $container = null) } /** - * @param string $className + * @param class-string $className * @return mixed * * @throws InvalidArgumentException If class does not exist. @@ -46,8 +47,8 @@ public function resolve(string $className): mixed } /** - * @param string $className - * @param array $bindings + * @param class-string $className + * @param array $bindings * @return mixed * * @throws ClassCannotBeInstantiated @@ -75,9 +76,10 @@ public function construct(string $className, array $bindings = []): mixed } /** - * @param callable $callable - * @param array $bindings - * @return mixed + * @template T + * @param callable():T $callable + * @param array $bindings + * @return T * * @throws ArgumentCountError * @throws CannotAutowireArgument @@ -93,7 +95,7 @@ public function call(callable $callable, array $bindings = []): mixed /** * @param ParameterReflection[] $parameters - * @param array $bindings + * @param array $bindings * @return array * * @throws CannotAutowireArgument From 04743e5ad4a34e1b4abf03e86ae9c6fc69ab9b7a Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Mon, 4 Aug 2025 14:41:56 +0200 Subject: [PATCH 6/7] Update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 421a471..4a28f59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.0.0] - 2025-08-04 +### Added +- `DependencyResolver` interface +### Changed +- Minimal supported PHP version to 8.0 +- Minimal PSR-11 container interface version is 2.0 + ## [0.7.0] - 2021-04-21 ### Added - Implement typed variadic parameter resolution support. From 87e7af67a4cd2f5b19dbf3100f2458318d6ce571 Mon Sep 17 00:00:00 2001 From: Ivan Voskoboinyk Date: Mon, 4 Aug 2025 14:42:33 +0200 Subject: [PATCH 7/7] Ignore the .idea folder --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6ee3b99..f8111ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea/ /vendor/ composer.lock composer.phar \ No newline at end of file