Skip to content

Commit ee83f61

Browse files
committed
- updates: cannot bind interface to interface
1 parent e0f9d92 commit ee83f61

1 file changed

Lines changed: 38 additions & 16 deletions

File tree

DIException.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ class DIException extends \LogicException implements ContainerExceptionInterface
2323
E_INVALID_PARAMETER_NAME = 7004,
2424
E_INSTANCE_NOT_FOUND = 7005,
2525
E_MISSING_ARGUMENT = 7006,
26-
E_REFLECTION_ERROR = 7007;
26+
E_REFLECTION_ERROR = 7007,
27+
E_CANNOT_BIND_INTERFACE = 7008;
2728

2829
protected array $messages = [
29-
DIException::E_CIRCULAR_DEPENDENCY => 'Circular dependency detected while creating an instance for :class',
30-
DIException::E_NON_PUBLIC_METHOD => 'Failed to create an instance, because the method ":class:::method" is not public',
31-
DIException::E_CANNOT_INSTANTIATE => 'Cannot instantiate :type :name',
32-
DIException::E_INVALID_PARAMETER_NAME => 'Provide a valid name for the global parameter: ":name"',
33-
DIException::E_INSTANCE_NOT_FOUND => 'The requested instance :id is not found in the container',
34-
DIException::E_MISSING_ARGUMENT => 'Required parameter ":name" is missing at position :position in :function()',
35-
DIException::E_REFLECTION_ERROR => ':message',
30+
self::E_CIRCULAR_DEPENDENCY => 'Circular dependency detected while creating an instance for :class',
31+
self::E_NON_PUBLIC_METHOD => 'Failed to create an instance, because the method ":class:::method" is not public',
32+
self::E_CANNOT_INSTANTIATE => 'Cannot instantiate :type :name',
33+
self::E_INVALID_PARAMETER_NAME => 'Provide a valid name for the global parameter: ":name"',
34+
self::E_INSTANCE_NOT_FOUND => 'The requested instance :id is not found in the container',
35+
self::E_MISSING_ARGUMENT => 'Required parameter ":name" is missing at position :position in :function()',
36+
self::E_CANNOT_BIND_INTERFACE => 'Only interface to class binding is allowed. Cannot bind interface ":dependency" to interface ":interface"',
37+
self::E_REFLECTION_ERROR => ':message',
3638
];
3739

3840
public function __construct(int $code, array $arguments = [], \Throwable $previous = null)
@@ -46,12 +48,17 @@ public function __construct(int $code, array $arguments = [], \Throwable $previo
4648

4749
public static function forCircularDependency(string $class): static
4850
{
49-
return new static(static::E_CIRCULAR_DEPENDENCY, [':class' => $class]);
51+
return new static(static::E_CIRCULAR_DEPENDENCY, [
52+
':class' => $class
53+
]);
5054
}
5155

5256
public static function forNonPublicMethod(string $class, string $method): static
5357
{
54-
return new static(static::E_NON_PUBLIC_METHOD, [':class' => $class, ':method' => $method]);
58+
return new static(static::E_NON_PUBLIC_METHOD, [
59+
':class' => $class,
60+
':method' => $method
61+
]);
5562
}
5663

5764
public static function cannotInstantiate(\ReflectionClass $dependency): static
@@ -64,18 +71,23 @@ public static function cannotInstantiate(\ReflectionClass $dependency): static
6471
default => 'class',
6572
// @codeCoverageIgnoreEnd
6673
};
67-
return new static(static::E_CANNOT_INSTANTIATE, [':name' => $dependency->name, ':type' => $type]);
74+
return new static(static::E_CANNOT_INSTANTIATE, [
75+
':name' => $dependency->name,
76+
':type' => $type
77+
]);
6878
}
6979

7080
public static function forInvalidParameterName(string $name): static
7181
{
72-
return new static(static::E_INVALID_PARAMETER_NAME, [':name' => $name]);
82+
return new static(static::E_INVALID_PARAMETER_NAME, [
83+
':name' => $name
84+
]);
7385
}
7486

7587
public static function forMissingArgument(
7688
string $name,
7789
\ReflectionParameter $parameter,
78-
\Throwable $e = null): static
90+
\Throwable $previous = null): static
7991
{
8092
return new static(static::E_MISSING_ARGUMENT, [
8193
':name' => $name,
@@ -84,12 +96,22 @@ public static function forMissingArgument(
8496
$parameter->getDeclaringClass()?->name,
8597
$parameter->getDeclaringFunction()?->name
8698
]))
87-
], $e);
99+
], $previous);
88100
}
89101

90-
public static function forReflectionError(\ReflectionException $e): static
102+
public static function forReflectionError(\ReflectionException $exception): static
91103
{
92-
return new static(static::E_REFLECTION_ERROR, [':message' => $e->getMessage()], $e);
104+
return new static(static::E_REFLECTION_ERROR, [
105+
':message' => $exception->getMessage()
106+
], $exception);
107+
}
108+
109+
public static function forInterfaceBinding(string $dependency, string $interface): static
110+
{
111+
return new static(static::E_CANNOT_BIND_INTERFACE, [
112+
':dependency' => $dependency,
113+
':interface' => $interface
114+
]);
93115
}
94116
}
95117

0 commit comments

Comments
 (0)