Skip to content

Commit

Permalink
Add optional factory/extension interfaces and dependency inspection (#54
Browse files Browse the repository at this point in the history
)

* add dependency enumeration feature.
* add optional interfaces for factories/extensions.
* 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.
  • Loading branch information
mindplay-dk committed Jan 10, 2024
1 parent a55e717 commit 7dcd502
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
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",
"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)
*
* @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:
*
* 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;
}

0 comments on commit 7dcd502

Please sign in to comment.