Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional factory/extension interfaces and dependency inspection #54

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
},
"require": {
"php": ">= 8.0.0",
mindplay-dk marked this conversation as resolved.
Show resolved Hide resolved
"psr/container": "^1.0 || ^2.0"
}
}
21 changes: 21 additions & 0 deletions src/ExtensionDefinitionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Interop\Container;

use Psr\Container\ContainerInterface;

/**
* An extension optionally implements this interface to reflect it's dependencies.
*/
interface ExtensionDefinitionInterface
{
/**
* 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
*
* @return mixed The extended service.
*/
public function __invoke(ContainerInterface $container, mixed $previous): mixed;
}
20 changes: 20 additions & 0 deletions src/FactoryDefinitionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Interop\Container;

use Psr\Container\ContainerInterface;

/**
* A factory optionally implements this interface to reflect it's dependencies.
*/
interface FactoryDefinitionInterface
{
/**
* 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): mixed;
}
15 changes: 15 additions & 0 deletions src/ServiceDependencyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Interop\Container;

/**
* A `ServiceProviderInterface` implementation may optionally implements this interface,
* which provides a means of reflecting the dependencies of the provided services.
*/
interface ServiceDependencyInterface
{
/**
* @return array<string,string[]> map where entry ID => list of dependency IDs
*/
public function getDependencies(): array;
}
18 changes: 11 additions & 7 deletions src/ServiceProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Interop\Container;

use Psr\Container\ContainerInterface;

/**
* A service provider provides entries to a container.
*/
Expand All @@ -13,20 +15,22 @@ interface ServiceProviderInterface
* - the key is the entry name
* - the value is a callable that will return the entry, aka the **factory**
*
* Factories have the following signature:
* function(\Psr\Container\ContainerInterface $container)
* A factory is an instance of {@see FactoryDefinitionInterface}, or a `callable` with the following signature:
*
* function(\Psr\Container\ContainerInterface $container)
mindplay-dk marked this conversation as resolved.
Show resolved Hide resolved
*
* @return callable[]
* @return array<string,((callable(ContainerInterface):mixed)|FactoryDefinitionInterface)>
*/
public function getFactories();
public function getFactories(): array;

/**
* Returns a list of all container entries extended by this service provider.
*
* - the key is the entry name
* - the value is a callable that will return the modified entry
*
* Callables have the following signature:
* An extension is an instance of {@see ExtensionDefinitionInterface}, or a `callable` with the following signature:
*
mindplay-dk marked this conversation as resolved.
Show resolved Hide resolved
* function(Psr\Container\ContainerInterface $container, $previous)
* or function(Psr\Container\ContainerInterface $container, $previous = null)
*
Expand All @@ -35,7 +39,7 @@ public function getFactories();
* - 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[]
* @return array<string,((callable(ContainerInterface,mixed):mixed)|ExtensionDefinitionInterface)[]>
*/
public function getExtensions();
public function getExtensions(): array;
}