-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIException.php
133 lines (118 loc) · 4.36 KB
/
DIException.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <[email protected]>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded;
use Psr\Container\{ContainerExceptionInterface, NotFoundExceptionInterface};
use LogicException;
use ReflectionClass;
use ReflectionException;
use ReflectionParameter;
use Throwable;
use function array_filter;
use function join;
use function strtr;
class DIException extends LogicException implements ContainerExceptionInterface
{
public const
E_CIRCULAR_DEPENDENCY = 7001,
E_NON_PUBLIC_METHOD = 7002,
E_CANNOT_INSTANTIATE = 7003,
E_INVALID_PARAMETER_NAME = 7004,
E_INSTANCE_NOT_FOUND = 7005,
E_MISSING_ARGUMENT = 7006,
E_REFLECTION_ERROR = 7007,
E_CANNOT_BIND_INTERFACE = 7008;
protected array $messages = [
self::E_CIRCULAR_DEPENDENCY => 'Circular dependency detected while creating an instance for :class',
self::E_NON_PUBLIC_METHOD => 'Failed to create an instance, because the method ":class:::method" is not public',
self::E_CANNOT_INSTANTIATE => 'Cannot instantiate :type :name',
self::E_INVALID_PARAMETER_NAME => 'Provide a valid name for the global parameter: ":name"',
self::E_INSTANCE_NOT_FOUND => 'The requested instance :id is not found in the container',
self::E_MISSING_ARGUMENT => 'Required parameter ":name" is missing at position :position in :function()',
self::E_CANNOT_BIND_INTERFACE => 'Only interface to class binding is allowed. Cannot bind interface ":dependency" to interface ":interface"',
self::E_REFLECTION_ERROR => ':message',
];
public function __construct(int $code, array $arguments = [], Throwable $previous = null)
{
parent::__construct(
strtr($this->messages[$code] ?? ':message', $arguments + [':message' => $this->message]),
$code,
$previous
);
}
public static function forCircularDependency(string $class): static
{
return new static(static::E_CIRCULAR_DEPENDENCY, [
':class' => $class
]);
}
public static function forNonPublicMethod(string $class, string $method): static
{
return new static(static::E_NON_PUBLIC_METHOD, [
':class' => $class,
':method' => $method
]);
}
public static function cannotInstantiate(ReflectionClass $dependency): static
{
$type = match (true) {
$dependency->isInterface() => 'interface',
$dependency->isAbstract() => 'abstract class',
$dependency->isTrait() => 'trait',
// @codeCoverageIgnoreStart
default => 'class',
// @codeCoverageIgnoreEnd
};
return new static(static::E_CANNOT_INSTANTIATE, [
':name' => $dependency->name,
':type' => $type
]);
}
public static function forInvalidParameterName(string $name): static
{
return new static(static::E_INVALID_PARAMETER_NAME, [
':name' => $name
]);
}
public static function forMissingArgument(
string $name,
ReflectionParameter $parameter,
Throwable $previous = null): static
{
return new static(static::E_MISSING_ARGUMENT, [
':name' => $name,
':position' => $parameter->getPosition(),
':function' => join('::', array_filter([
$parameter->getDeclaringClass()?->name,
$parameter->getDeclaringFunction()?->name
]))
], $previous);
}
public static function forReflectionError(ReflectionException $exception): static
{
return new static(static::E_REFLECTION_ERROR, [
':message' => $exception->getMessage()
], $exception);
}
public static function forInterfaceBinding(string $dependency, string $interface): static
{
return new static(static::E_CANNOT_BIND_INTERFACE, [
':dependency' => $dependency,
':interface' => $interface
]);
}
}
class DIInstanceNotFound extends DIException implements NotFoundExceptionInterface
{
public static function for(string $id): NotFoundExceptionInterface
{
return new static(static::E_INSTANCE_NOT_FOUND, [':id' => $id]);
}
}