From 71b591366e9a0bb5381c39bc8c1d1f7a0823e874 Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Tue, 16 Mar 2021 16:40:44 +0100 Subject: [PATCH 01/16] add ServiceInterface as proposed in https://github.com/container-interop/service-provider/issues/53#issuecomment-800056146 --- src/ServiceInterface.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/ServiceInterface.php diff --git a/src/ServiceInterface.php b/src/ServiceInterface.php new file mode 100644 index 0000000..568d960 --- /dev/null +++ b/src/ServiceInterface.php @@ -0,0 +1,16 @@ + Date: Wed, 17 Mar 2021 11:19:49 +0100 Subject: [PATCH 02/16] add __invoke Add `__invoke`, per discussion here: https://github.com/container-interop/service-provider/issues/53#issuecomment-800933960 --- src/ServiceInterface.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ServiceInterface.php b/src/ServiceInterface.php index 568d960..dad4d97 100644 --- a/src/ServiceInterface.php +++ b/src/ServiceInterface.php @@ -13,4 +13,13 @@ interface ServiceInterface * @return string[] A list of strings each representing the key of a service. */ public function getDependencies(): array; + + /** + * Creates the entry, using the given container to resolve dependencies. + * + * @param ContainerInterface $container The container that should be used to resolve dependencies + * + * @return mixed The created entry + */ + public function __invoke(ContainerInterface $container); } From 41851405c45bcf62974a48bfff2e2fd7a0c5f079 Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Wed, 17 Mar 2021 12:17:23 +0100 Subject: [PATCH 03/16] add missing import --- src/ServiceInterface.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ServiceInterface.php b/src/ServiceInterface.php index dad4d97..d3a25cb 100644 --- a/src/ServiceInterface.php +++ b/src/ServiceInterface.php @@ -2,6 +2,8 @@ namespace Interop\Container; +use Psr\Container\ContainerInterface; + /** * A factory optionally implements this interface to reflect it's dependencies. */ From d3dbbd06657ca17a6df538cfb602efbb7526de4f Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Thu, 18 Mar 2021 14:30:42 +0100 Subject: [PATCH 04/16] separate factory/extension interfaces, with a common facet for getDependencies() - as discussed in #54 --- src/ExtensionDefinitionInterface.php | 21 +++++++++++++++++++ ...ace.php => FactoryDefinitionInterface.php} | 9 +------- src/ServiceDefinitionInterface.php | 16 ++++++++++++++ src/ServiceProviderInterface.php | 12 ++++++----- 4 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 src/ExtensionDefinitionInterface.php rename src/{ServiceInterface.php => FactoryDefinitionInterface.php} (66%) create mode 100644 src/ServiceDefinitionInterface.php diff --git a/src/ExtensionDefinitionInterface.php b/src/ExtensionDefinitionInterface.php new file mode 100644 index 0000000..abcfe2b --- /dev/null +++ b/src/ExtensionDefinitionInterface.php @@ -0,0 +1,21 @@ + Date: Sun, 17 Dec 2023 18:16:53 +0100 Subject: [PATCH 05/16] Update src/ServiceDefinitionInterface.php Co-authored-by: Anton Ukhanev --- src/ServiceDefinitionInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ServiceDefinitionInterface.php b/src/ServiceDefinitionInterface.php index ed58cf6..066e58f 100644 --- a/src/ServiceDefinitionInterface.php +++ b/src/ServiceDefinitionInterface.php @@ -8,7 +8,7 @@ interface ServiceDefinitionInterface { /** - * Retrieves the keys of known, dependent service keys. + * Retrieves the keys services that are known dependencies of this service.. * * @return string[] A list of strings each representing the key of a service. */ From e4ee8b5b0047a2d0991d27b738a62a915bad0f0e Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Sun, 17 Dec 2023 18:18:30 +0100 Subject: [PATCH 06/16] Update src/ServiceDefinitionInterface.php Co-authored-by: Anton Ukhanev --- src/ServiceDefinitionInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ServiceDefinitionInterface.php b/src/ServiceDefinitionInterface.php index 066e58f..edbd309 100644 --- a/src/ServiceDefinitionInterface.php +++ b/src/ServiceDefinitionInterface.php @@ -3,7 +3,7 @@ namespace Interop\Container; /** - * This interface defines the common facet of service factories and extensions. + * Represents a service definition. */ interface ServiceDefinitionInterface { From 1d476b94a94853781bd170229cb9d726b7083752 Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Sun, 17 Dec 2023 18:18:44 +0100 Subject: [PATCH 07/16] Update src/ServiceDefinitionInterface.php Co-authored-by: Anton Ukhanev --- src/ServiceDefinitionInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ServiceDefinitionInterface.php b/src/ServiceDefinitionInterface.php index edbd309..f5376a2 100644 --- a/src/ServiceDefinitionInterface.php +++ b/src/ServiceDefinitionInterface.php @@ -10,7 +10,7 @@ interface ServiceDefinitionInterface /** * Retrieves the keys services that are known dependencies of this service.. * - * @return string[] A list of strings each representing the key of a service. + * @return string[] A list of service keys. */ public function getDependencies(): array; } From 50975067d50890dd2556157fe55cdc11e775ad38 Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Sun, 17 Dec 2023 18:20:35 +0100 Subject: [PATCH 08/16] Update src/ExtensionDefinitionInterface.php Co-authored-by: Anton Ukhanev --- src/ExtensionDefinitionInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ExtensionDefinitionInterface.php b/src/ExtensionDefinitionInterface.php index abcfe2b..e153d97 100644 --- a/src/ExtensionDefinitionInterface.php +++ b/src/ExtensionDefinitionInterface.php @@ -10,7 +10,7 @@ interface ExtensionDefinitionInterface extends ServiceDefinitionInterface { /** - * Extends a given service/entry, using the given container to resolve dependencies. + * Extends a given service, using the given container to resolve dependencies. * * @param ContainerInterface $container The container that should be used to resolve dependencies * @param mixed $previous The previous service/entry From 47be510c2e333d7c66302797a3f528e4a0a3433d Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Sun, 17 Dec 2023 18:20:43 +0100 Subject: [PATCH 09/16] Update src/ExtensionDefinitionInterface.php Co-authored-by: Anton Ukhanev --- src/ExtensionDefinitionInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ExtensionDefinitionInterface.php b/src/ExtensionDefinitionInterface.php index e153d97..9b01b1a 100644 --- a/src/ExtensionDefinitionInterface.php +++ b/src/ExtensionDefinitionInterface.php @@ -13,7 +13,7 @@ interface ExtensionDefinitionInterface extends ServiceDefinitionInterface * Extends a given service, using the given container to resolve dependencies. * * @param ContainerInterface $container The container that should be used to resolve dependencies - * @param mixed $previous The previous service/entry + * @param mixed $previous The previous service * * @return mixed The extended service/entry */ From b6ef982f37b59ccf4b700de9b5cd8efc321cfa5d Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Sun, 17 Dec 2023 18:20:50 +0100 Subject: [PATCH 10/16] Update src/ExtensionDefinitionInterface.php Co-authored-by: Anton Ukhanev --- src/ExtensionDefinitionInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ExtensionDefinitionInterface.php b/src/ExtensionDefinitionInterface.php index 9b01b1a..822bc47 100644 --- a/src/ExtensionDefinitionInterface.php +++ b/src/ExtensionDefinitionInterface.php @@ -15,7 +15,7 @@ interface ExtensionDefinitionInterface extends ServiceDefinitionInterface * @param ContainerInterface $container The container that should be used to resolve dependencies * @param mixed $previous The previous service * - * @return mixed The extended service/entry + * @return mixed The extended service. */ public function __invoke(ContainerInterface $container, $previous); } From 8e5f40bbfd5e782a3b564f12af7ebdf925809491 Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Sun, 17 Dec 2023 18:26:19 +0100 Subject: [PATCH 11/16] language --- src/ServiceDefinitionInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ServiceDefinitionInterface.php b/src/ServiceDefinitionInterface.php index f5376a2..ce105ea 100644 --- a/src/ServiceDefinitionInterface.php +++ b/src/ServiceDefinitionInterface.php @@ -8,7 +8,7 @@ interface ServiceDefinitionInterface { /** - * Retrieves the keys services that are known dependencies of this service.. + * Retrieves the keys of known dependencies of this service. * * @return string[] A list of service keys. */ From 71e4eddea2dc323662d7aadfb9801b31daff61fc Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Sun, 17 Dec 2023 18:31:04 +0100 Subject: [PATCH 12/16] add missing types (require PHP 8) --- composer.json | 1 + src/ExtensionDefinitionInterface.php | 2 +- src/FactoryDefinitionInterface.php | 2 +- src/ServiceProviderInterface.php | 4 ++-- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 597df85..0b7dea7 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ } }, "require": { + "php": ">= 8.0.0", "psr/container": "^1.0" } } diff --git a/src/ExtensionDefinitionInterface.php b/src/ExtensionDefinitionInterface.php index 822bc47..d784242 100644 --- a/src/ExtensionDefinitionInterface.php +++ b/src/ExtensionDefinitionInterface.php @@ -17,5 +17,5 @@ interface ExtensionDefinitionInterface extends ServiceDefinitionInterface * * @return mixed The extended service. */ - public function __invoke(ContainerInterface $container, $previous); + public function __invoke(ContainerInterface $container, mixed $previous): mixed; } diff --git a/src/FactoryDefinitionInterface.php b/src/FactoryDefinitionInterface.php index 0d5b813..d0b2c0d 100644 --- a/src/FactoryDefinitionInterface.php +++ b/src/FactoryDefinitionInterface.php @@ -16,5 +16,5 @@ interface FactoryDefinitionInterface extends ServiceDefinitionInterface * * @return mixed The created entry */ - public function __invoke(ContainerInterface $container); + public function __invoke(ContainerInterface $container): mixed; } diff --git a/src/ServiceProviderInterface.php b/src/ServiceProviderInterface.php index 5fb4cc9..f5e82ff 100644 --- a/src/ServiceProviderInterface.php +++ b/src/ServiceProviderInterface.php @@ -19,7 +19,7 @@ interface ServiceProviderInterface * * @return (callable|FactoryDefinitionInterface)[] */ - public function getFactories(); + public function getFactories(): array; /** * Returns a list of all container entries extended by this service provider. @@ -39,5 +39,5 @@ public function getFactories(); * * @return (callable|ExtensionDefinitionInterface)[] */ - public function getExtensions(); + public function getExtensions(): array; } From 657e24bf7bad714a1a024c861475e17fe8c82e6c Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Tue, 19 Dec 2023 19:40:16 +0100 Subject: [PATCH 13/16] add callable types: these were tested with Psalm and PHPStan, and worked as expected with both - this provides static analysis with at least two popular tools (probably PHPStorm as well) and to the rest of the world they are just documentation. --- src/ServiceProviderInterface.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ServiceProviderInterface.php b/src/ServiceProviderInterface.php index f5e82ff..5f43f7a 100644 --- a/src/ServiceProviderInterface.php +++ b/src/ServiceProviderInterface.php @@ -2,6 +2,8 @@ namespace Interop\Container; +use Psr\Container\ContainerInterface; + /** * A service provider provides entries to a container. */ @@ -17,7 +19,7 @@ interface ServiceProviderInterface * * function(\Psr\Container\ContainerInterface $container) * - * @return (callable|FactoryDefinitionInterface)[] + * @return ((callable(ContainerInterface):mixed)|FactoryDefinitionInterface)[] */ public function getFactories(): array; @@ -37,7 +39,7 @@ public function getFactories(): array; * - the container (instance of `Psr\Container\ContainerInterface`) * - the entry to be extended. If the entry to be extended does not exist and the parameter is nullable, `null` will be passed. * - * @return (callable|ExtensionDefinitionInterface)[] + * @return ((callable(ContainerInterface,mixed):mixed)|ExtensionDefinitionInterface)[] */ public function getExtensions(): array; } From 1b3acc6ffa4162b49fe72d936baefa3a46e1f26b Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Wed, 10 Jan 2024 19:27:53 +0100 Subject: [PATCH 14/16] drop service definition interface --- src/ExtensionDefinitionInterface.php | 2 +- src/FactoryDefinitionInterface.php | 2 +- src/ServiceDefinitionInterface.php | 16 ---------------- 3 files changed, 2 insertions(+), 18 deletions(-) delete mode 100644 src/ServiceDefinitionInterface.php diff --git a/src/ExtensionDefinitionInterface.php b/src/ExtensionDefinitionInterface.php index d784242..54a1faf 100644 --- a/src/ExtensionDefinitionInterface.php +++ b/src/ExtensionDefinitionInterface.php @@ -7,7 +7,7 @@ /** * An extension optionally implements this interface to reflect it's dependencies. */ -interface ExtensionDefinitionInterface extends ServiceDefinitionInterface +interface ExtensionDefinitionInterface { /** * Extends a given service, using the given container to resolve dependencies. diff --git a/src/FactoryDefinitionInterface.php b/src/FactoryDefinitionInterface.php index d0b2c0d..07389e5 100644 --- a/src/FactoryDefinitionInterface.php +++ b/src/FactoryDefinitionInterface.php @@ -7,7 +7,7 @@ /** * A factory optionally implements this interface to reflect it's dependencies. */ -interface FactoryDefinitionInterface extends ServiceDefinitionInterface +interface FactoryDefinitionInterface { /** * Creates the entry, using the given container to resolve dependencies. diff --git a/src/ServiceDefinitionInterface.php b/src/ServiceDefinitionInterface.php deleted file mode 100644 index ce105ea..0000000 --- a/src/ServiceDefinitionInterface.php +++ /dev/null @@ -1,16 +0,0 @@ - Date: Wed, 10 Jan 2024 19:36:51 +0100 Subject: [PATCH 15/16] add service dependency interface --- src/ServiceDependencyInterface.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/ServiceDependencyInterface.php diff --git a/src/ServiceDependencyInterface.php b/src/ServiceDependencyInterface.php new file mode 100644 index 0000000..a0f4cd3 --- /dev/null +++ b/src/ServiceDependencyInterface.php @@ -0,0 +1,15 @@ + map where entry ID => list of dependency IDs + */ + public function getDependencies(): array; +} From 85ed40d05b7d7297f497989bf4112a7be8daf8d5 Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Wed, 10 Jan 2024 19:50:35 +0100 Subject: [PATCH 16/16] use generic array types to correctly type-hint arrays with string keys (works with PHPStan, Psalm and PHPStorm) --- src/ServiceProviderInterface.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ServiceProviderInterface.php b/src/ServiceProviderInterface.php index 5f43f7a..ccb31a5 100644 --- a/src/ServiceProviderInterface.php +++ b/src/ServiceProviderInterface.php @@ -19,7 +19,7 @@ interface ServiceProviderInterface * * function(\Psr\Container\ContainerInterface $container) * - * @return ((callable(ContainerInterface):mixed)|FactoryDefinitionInterface)[] + * @return array */ public function getFactories(): array; @@ -39,7 +39,7 @@ public function getFactories(): array; * - the container (instance of `Psr\Container\ContainerInterface`) * - the entry to be extended. If the entry to be extended does not exist and the parameter is nullable, `null` will be passed. * - * @return ((callable(ContainerInterface,mixed):mixed)|ExtensionDefinitionInterface)[] + * @return array */ public function getExtensions(): array; }