Skip to content

Commit

Permalink
Finalize and cleanup Normalizer (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jul 17, 2021
1 parent a6f0015 commit f3c96f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 38 deletions.
5 changes: 5 additions & 0 deletions src/Definition/DynamicReference.php
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Factory\Definition;

use Psr\Container\ContainerInterface;
use Yiisoft\Factory\Exception\InvalidConfigException;

/**
* Class DynamicReference allows us to define a dependency to a service not defined in the container.
Expand All @@ -31,6 +32,8 @@ class DynamicReference implements ReferenceInterface

/**
* @param mixed $definition
*
* @throws InvalidConfigException
*/
private function __construct($definition)
{
Expand All @@ -39,6 +42,8 @@ private function __construct($definition)

/**
* @see Normalizer
*
* @throws InvalidConfigException
*/
public static function to($id): ReferenceInterface
{
Expand Down
60 changes: 22 additions & 38 deletions src/Definition/Normalizer.php
Expand Up @@ -13,50 +13,34 @@
use function is_string;

/**
* Class Definition represents a definition in a container
* Normalizer definition from configuration to an instance of {@see DefinitionInterface}.
*
* @psalm-import-type ArrayDefinitionConfig from ArrayDefinition
*/
class Normalizer
final class Normalizer
{
/**
* Definition may be defined multiple ways.
* Interface name as string:
* Normalize definition to an instance of {@see DefinitionInterface}.
* Definition may be defined multiple ways:
* - class name,
* - string as reference to another class or alias,
* - instance of {@see ReferenceInterface},
* - callable,
* - array,
* - ready object.
*
* ```php
* $container->set('interface_name', EngineInterface::class);
* ```
* @param mixed $definition The definition for normalization.
* @param string $class The class name of the object to be defined (optional). It is used in two cases.
* - The definition is a string, and class name equals to definition. Returned `ArrayDefinition` with defined
* class.
* - The definition is an array without class name. Class name will be added to array and `ArrayDefinition`
* will be returned.
*
* A closure:
* @throws InvalidConfigException If configuration is not valid.
*
* ```php
* $container->set('closure', function($container) {
* return new MyClass($container->get('db'));
* });
* ```
*
* A callable array:
*
* ```php
* $container->set('static_call', [MyClass::class, 'create']);
* ```
*
* A definition array:
*
* ```php
* $container->set('full_definition', [
* 'class' => EngineMarkOne::class,
* '__construct()' => [42],
* '$argName' => 'value',
* 'setX()' => [42],
* ]);
* ```
*
* @param mixed $definition
*
* @throws InvalidConfigException
* @return DefinitionInterface Normalized definition as an object.
*/
public static function normalize($definition, string $id = null): DefinitionInterface
public static function normalize($definition, string $class = null): DefinitionInterface
{
// Reference
if ($definition instanceof ReferenceInterface) {
Expand All @@ -66,8 +50,8 @@ public static function normalize($definition, string $id = null): DefinitionInte
if (is_string($definition)) {
// Current class
if (
$id === $definition ||
($id === null && class_exists($definition))
$class === $definition ||
($class === null && class_exists($definition))
) {
/** @psalm-var class-string $definition */
return ArrayDefinition::fromPreparedData($definition);
Expand All @@ -86,7 +70,7 @@ public static function normalize($definition, string $id = null): DefinitionInte
if (is_array($definition)) {
$config = $definition;
if (!array_key_exists(ArrayDefinition::CLASS_NAME, $config)) {
$config[ArrayDefinition::CLASS_NAME] = $id;
$config[ArrayDefinition::CLASS_NAME] = $class;
}
/** @psalm-var ArrayDefinitionConfig $config */
return ArrayDefinition::fromConfig($config);
Expand Down

0 comments on commit f3c96f5

Please sign in to comment.