1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Koded package. |
5
|
|
|
* |
6
|
|
|
* (c) Mihail Binev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Please view the LICENSE distributed with this source code |
9
|
|
|
* for the full copyright and license information. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Koded; |
14
|
|
|
|
15
|
|
|
use Psr\Container\{ContainerExceptionInterface, NotFoundExceptionInterface}; |
16
|
|
|
|
17
|
|
|
class DIException extends \LogicException implements ContainerExceptionInterface |
18
|
|
|
{ |
19
|
|
|
public const |
20
|
|
|
E_CIRCULAR_DEPENDENCY = 7001, |
21
|
|
|
E_NON_PUBLIC_METHOD = 7002, |
22
|
|
|
E_CANNOT_INSTANTIATE = 7003, |
23
|
|
|
E_INVALID_PARAMETER_NAME = 7004, |
24
|
|
|
E_INSTANCE_NOT_FOUND = 7005, |
25
|
|
|
E_MISSING_ARGUMENT = 7006, |
26
|
|
|
E_REFLECTION_ERROR = 7007, |
27
|
|
|
E_CANNOT_BIND_INTERFACE = 7008; |
28
|
|
|
|
29
|
|
|
protected array $messages = [ |
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', |
38
|
17 |
|
]; |
39
|
|
|
|
40
|
17 |
|
public function __construct(int $code, array $arguments = [], \Throwable $previous = null) |
41
|
17 |
|
{ |
42
|
|
|
parent::__construct( |
43
|
|
|
\strtr($this->messages[$code] ?? ':message', $arguments + [':message' => $this->message]), |
44
|
|
|
$code, |
45
|
17 |
|
$previous |
46
|
|
|
); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
public static function forCircularDependency(string $class): static |
50
|
|
|
{ |
51
|
|
|
return new static(static::E_CIRCULAR_DEPENDENCY, [ |
52
|
1 |
|
':class' => $class |
53
|
|
|
]); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
public static function forNonPublicMethod(string $class, string $method): static |
57
|
5 |
|
{ |
58
|
|
|
return new static(static::E_NON_PUBLIC_METHOD, [ |
59
|
5 |
|
':class' => $class, |
60
|
5 |
|
':method' => $method |
61
|
3 |
|
]); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
public static function cannotInstantiate(\ReflectionClass $dependency): static |
65
|
|
|
{ |
66
|
|
|
$type = match (true) { |
67
|
5 |
|
$dependency->isInterface() => 'interface', |
68
|
|
|
$dependency->isAbstract() => 'abstract class', |
69
|
|
|
$dependency->isTrait() => 'trait', |
70
|
7 |
|
// @codeCoverageIgnoreStart |
71
|
|
|
default => 'class', |
72
|
7 |
|
// @codeCoverageIgnoreEnd |
73
|
|
|
}; |
74
|
|
|
return new static(static::E_CANNOT_INSTANTIATE, [ |
75
|
1 |
|
':name' => $dependency->name, |
76
|
|
|
':type' => $type |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public static function forInvalidParameterName(string $name): static |
81
|
1 |
|
{ |
82
|
1 |
|
return new static(static::E_INVALID_PARAMETER_NAME, [ |
83
|
1 |
|
':name' => $name |
84
|
1 |
|
]); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
public static function forMissingArgument( |
88
|
|
|
string $name, |
89
|
|
|
\ReflectionParameter $parameter, |
90
|
1 |
|
\Throwable $previous = null): static |
91
|
|
|
{ |
92
|
1 |
|
return new static(static::E_MISSING_ARGUMENT, [ |
93
|
|
|
':name' => $name, |
94
|
|
|
':position' => $parameter->getPosition(), |
95
|
|
|
':function' => \join('::', \array_filter([ |
96
|
|
|
$parameter->getDeclaringClass()?->name, |
97
|
|
|
$parameter->getDeclaringFunction()?->name |
98
|
|
|
])) |
99
|
1 |
|
], $previous); |
100
|
|
|
} |
101
|
1 |
|
|
102
|
|
|
public static function forReflectionError(\ReflectionException $exception): static |
103
|
|
|
{ |
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
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
class DIInstanceNotFound extends DIException implements NotFoundExceptionInterface |
120
|
|
|
{ |
121
|
|
|
public static function for(string $id): NotFoundExceptionInterface |
122
|
|
|
{ |
123
|
|
|
return new static(static::E_INSTANCE_NOT_FOUND, [':id' => $id]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|