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 13 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 ServiceDefinitionInterface
mindplay-dk marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* 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 extends ServiceDefinitionInterface
{
/**
* 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;
}
16 changes: 16 additions & 0 deletions src/ServiceDefinitionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Interop\Container;

/**
* Represents a service definition.
*/
interface ServiceDefinitionInterface
{
/**
* Retrieves the keys of known dependencies of this service.
*
* @return string[] A list of service keys.
*/
public function getDependencies(): array;
}
mindplay-dk marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 9 additions & 7 deletions src/ServiceProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,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 (callable|FactoryDefinitionInterface)[]
mindplay-dk marked this conversation as resolved.
Show resolved Hide resolved
*/
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 +37,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 (callable|ExtensionDefinitionInterface)[]
*/
public function getExtensions();
public function getExtensions(): array;
}